Java 中的多线程 Web 服务器

Muti - threaded Web Server in Java

我正在尝试使用 Java 构建服务器。 我的问题是:如果我同时有多个用户怎么办?答案是:多线程。但我不知道该怎么做。

例如,如果有两个客户端同时连接,一个服务器(谁做2*number):如果客户端1对服务器说“50”,客户端2对服务器说“10”,服务器应该 return “100”给第一个客户,“20”给第二个客户。但我不确定我的代码是否有效。

服务器端:

public class Server {
public static void main(String[] args){
    ServerSocket socket;
    try {
    socket = new ServerSocket(4444);
    Thread t = new Thread(new Accept(socket));
    t.start();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

class Accept implements Runnable {
   private ServerSocket socketserver;
   private Socket socket;
   private int nbrclient = 1;
    public Accept(ServerSocket s){
        socketserver = s;
    }
    public void run() {
        try {
          socket = socketserver.accept();
          in = new BufferedReader (new InputStreamReader (socket.getInputStream()));
            String message = in.readLine();
            System.out.println(message);

            out = new PrintWriter(socket.getOutputStream());
            out.println("Pong");
            out.flush();
            socket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

客户端:

 public class Client {
    public static void main(String[] zero) {    
        Socket socket;
        BufferedReader in;
        PrintWriter out;
        try {
            socket = new Socket(InetAddress.getLocalHost(),4444);   
            out = new PrintWriter(socket.getOutputStream());
            out.println("Ping");
            out.flush();
            in = new BufferedReader (new InputStreamReader (socket.getInputStream()));
            String message = in.readLine();
            System.out.println(message);
            socket.close();
        }catch (UnknownHostException e) {
            e.printStackTrace();
        }catch (IOException e) {
            e.printStackTrace();
        }
    }

}

如果您有任何想法(如何进行多线程以及如何验证我的代码是否有效,例如 运行 两个 Clients.java 并检查多线程是否有效)

服务器端需要一个while循环:

public class Server {
public static void main(String[] args){
    ServerSocket socket;
    try {
        while(true){
            socket = new ServerSocket(4444);
            Socket socketInstance = socket.accept();
            Thread t = new Thread(new Accept(socketInstance));
            t.start();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

class Accept implements Runnable {
   private Socket socket;
   private int nbrclient = 1;
    public Accept(Socket s){
        socket = s;
    }
    public void run() {
        try {

            in = new BufferedReader (new InputStreamReader (socket.getInputStream()));
            String message = in.readLine();
            System.out.println(message);//this message should be your number
            Double number = Double.parseString(message);

            out = new PrintWriter(socket.getOutputStream());
            //out.println("Pong");
            out.println(2*number +"");
            out.flush();
            socket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

客户端看起来没问题。只需将 out.println("Ping"); 替换为 out.println("50"); 或任何你想要的。

您首先启动服务器,然后您可以启动多个客户端应用程序。如果您有任何错误,您可以在此处 post 它们并查看确切的场景。