Socket服务端接收一个用户,就开一个线程处理,求代码
多线程处理。最好客户端和服务器端的代码都要有
多线程处理。最好客户端和服务器端的代码都要有
解决方案 »
- java中可以创建文件,然后设定文件的长度吗
- 在java开发中 怎样更换标题栏默认的coffe图标
- 请问有没有关于使用JCreator的教程,谢谢!30分
- 谁能给一个上传下载的简单例子?
- 使用jfreechart发生的错误.解决后再100
- 一个Datagram Socket 能不能同时和多个目标地址通信?
- JDBC 数据库操作“无效的列索引”
- Java 实现音乐播放器 异常
- 弱弱地问,string的处理问题
- 如何在win2k advance server上配置jsdk&jre1.3?------急!!!
- 求解:关于多线程 实现Runnable 加上@Override 的问题
- 关于服务器和客户端不在同一局域网内套接字编程问题,求高手解释
BP 是从线程池中获取一个空闲的线程来处理
import java.net.*;public class Client extends Thread{
private Socket s;
private PrintStream ps;
private BufferedReader sbr;
public Client(){
try{
s=new Socket("192.168.10.94",10000);
ps=new PrintStream(s.getOutputStream());
System.out.println("please input");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String line="";
while((line=br.readLine())!=null){
System.out.println("began to send");
ps.println(line);
break;
}
System.out.println("end to send");
sbr=new BufferedReader(new InputStreamReader(s.getInputStream()));
//ps.close();
}catch(Exception e){
e.printStackTrace();
}
}
public void run(){
String myline="";
while(true){
try {
while((myline=sbr.readLine())!=null){
System.out.println(myline);
}
} catch (IOException e) {
e.printStackTrace();
}
try{
this.sleep(1000);
}catch(Exception ex){
ex.printStackTrace();
}
}
}
public static void main(String[] arg){
Client c=new Client();
c.start();
}}
import java.net.*;public class Serice extends Thread{
private Socket s;
private ServerSocket ss;
public Serice(){
try {
ss=new ServerSocket(10000);
} catch (IOException e) {
e.printStackTrace();
}
}
public void run(){
while(true){
try {
s=ss.accept();
System.out.println("recive");
BufferedReader br=new BufferedReader(new InputStreamReader(s.getInputStream()));
String line="";
System.out.println("recive2");
//line=br.readLine();
while((line=br.readLine())!=null){
System.out.println("client:"+line);
break;
}
System.out.println("send back");
PrintStream ps=new PrintStream(s.getOutputStream());
String myline="";
BufferedReader mybr=new BufferedReader(new InputStreamReader(System.in));
while((myline=mybr.readLine())!=null){
ps.println(myline);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] arg){
Serice service=new Serice();
service.start();
}
}
import java.io.*;class Server
{
public static void main(String[] args )throws Exception
{
ServerSocket ss=new ServerSocket(6666);//创建服务器端插座并指定端口号
while(true)
{
Socket s=ss.accept();//等待接收连接
System.out.println("a client is connect"); //连接完成打印一句话
DataInputStream dis=new DataInputStream(s.getInputStream());
DataOutputStream dos=new DataOutputStream(s.getOutputStream());
//用流来于客户端进行通信
dos.writeUTF("您好我服务器端");
System.out.println(dis.readUTF()); }
}}class Client
{
public static void main(String[] args)
{
try
{
Socket s=new Socket("127.0.0.1",6666);//建立插座指定指定服务器端IP 端口号
DataInputStream dis=new DataInputStream(s.getInPutStream());
//用流来通信
String s=dis.readUFF();
System.out.println(s);
DataOutputStream dos=new DataOutputStream(s.getOutputStream);
s.writeUTF("我是客户端");
}catch(Exception e) }
}