Java - Server同时为多个客户端提供服务

时间:2017-10-03 12:36:12

标签: java multithreading sockets client-server

我写了一个程序,它实现了一个简单的服务器 - 客户端连接和通信,但它只适用于一个客户端。如果我启动第二个客户端,则它不会执行任何操作,只会将“Out:0”作为输出。 (服务器没有响应它)

我在服务器中使用Threads,但我无法弄清楚,为什么这个解决方案无效。

有什么想法吗?

(我知道空的catch-es并不漂亮,但它只是一个示例代码。)

Server.java:

public class Server implements Runnable {
final int port;

public Server(int port){
    this.port=port;
}

@Override
public void run(){
    while(true){
    try{
        ServerSocket ss = new ServerSocket(port);
        new ClientThread(ss).start();
    }catch(Exception e){}
    }
}

public static void main(String[] args){
   new Server(1995).run();
}

private class ClientThread extends Thread{
    Socket s;
    Scanner sc;
    PrintWriter pw;

    ClientThread(ServerSocket ss){
        try{
            this.s = ss.accept();
        }catch(Exception e){}
    }

    @Override
    public void start(){            
        try{
            String in,out;

            sc = new Scanner(s.getInputStream());
            pw = new PrintWriter(s.getOutputStream(), true);
            int i=0;
            do{
                in = sc.nextLine();
                System.out.println("In: " + in);
                int tmp = Integer.parseInt(in)+1;
                out = Integer.toString(tmp);
                pw.println(out);
                i++;
            }while(i<10);
        }catch(Exception e){}
    }
}
}

Client.java:

public class Client implements Runnable{
String host;
int port; 
Socket s;
Scanner sc;
PrintWriter pw;

public Client(String host, int port){
    this.host=host;
    this.port=port;
}

@Override
public void run(){
    try{
        s = new Socket(host, port);

        sc = new Scanner(s.getInputStream());
        pw = new PrintWriter(s.getOutputStream(), true);

        String in, out;
        out="0";
        int i=0;
        do{
            pw.println(out);
            System.out.println("Out: " + out);
            in = sc.nextLine();
           int  tmp = Integer.parseInt(in)+1;
           out=Integer.toString(tmp);
        }while(i<10);

    }catch(Exception e){}
}

public static void main(String[] args){
    new Client("localhost", 1995).run();
}
}

1 个答案:

答案 0 :(得分:0)

您不必覆盖start(),必须覆盖run()

private class ClientThread extends Thread{
    Socket s;
    Scanner sc;
    PrintWriter pw;

    ClientThread(Socket s){
        this.s = s;
    }

    @Override
    public void run() {
        try{
            String in,out;

            sc = new Scanner(s.getInputStream());
            pw = new PrintWriter(s.getOutputStream(), true);
            int i=0;
            do{
                in = sc.nextLine();
                System.out.println("In: " + in);
                int tmp = Integer.parseInt(in)+1;
                out = Integer.toString(tmp);
                pw.println(out);
                i++;
            }while(i<10);
        }catch(Exception e){}
    }
}

服务器主题:

@Override
public void run(){
    try{
        ServerSocket ss = new ServerSocket(port);
        while(true){
            Socket cl = ss.accept();
            new ClientThread(cl).start();
        }
    }catch(Exception e){}
}