简体   繁体   中英

Java - Server serves multiple clients at the same time

I wrote a program, that implements a simple server-client connection and communication, but it only works with one client. If I start a second client, it doesn't do anything but gives "Out: 0" as output. (The server not respond to it)

I use Threads in my server, but i can't figure out, why this solution is not working.

Any idea?

(I know that the empty catch-es are not pretty, but it's just an example code.)

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();
}
}

You do not have to overwrite start() , you must overwrite 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){}
    }
}

Server thread:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM