Socket服务端接收一个用户,就开一个线程处理,求代码
多线程处理。最好客户端和服务器端的代码都要有 

解决方案 »

  1.   

    JDK 现成自带的东西,你来求什么啊!%JDK_HOME%/sample/nio/server看其中的 BN.java 或者 BP.javaBN 是新启用一个线程来处理
    BP 是从线程池中获取一个空闲的线程来处理
      

  2.   

    package client;import java.io.*;
    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();
    }}
      

  3.   

    package serice;import java.io.*;
    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();
    }
    }
      

  4.   

    建议你看一下,java这一方面的视频
      

  5.   

    import java.net.*;
    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)   }
    }