The server activates a new thread to process each incoming client request,

I have one to one server client application. I want to use multithreading so that the server must activate a new thread to process each incoming client request,

Server:

public class EchoServer {

    public static void main(String[] args) throws IOException {
         ServerSocket serverSocket = new ServerSocket(79)

         while (true) {
            Socket socket = serverSocket.accept();
            System.out.println("Accepted an echo request");
            System.out.println("... local socket address " + socket.getLocalSocketAddress());
            System.out.println("... remote socket address " + socket.getRemoteSocketAddress());

            InputStream input = socket.getInputStream();
            OutputStream output = socket.getOutputStream();
            while (true) {
               int b = input.read();
               if (b == -1) break;
               output.write(b);
            }
        socket.close();
     }
  }
}

Client side:

public class EchoClient {

    public static void main(String[] args) throws IOException {
    int b;      
    Socket socket = new Socket(args[0], 79);
    InputStream input = socket.getInputStream();
    OutputStream output = socket.getOutputStream();
    System.out.println("The socket is connected the server.");
    System.out.println("... local socket address is " + socket.getLocalSocketAddress());
    System.out.println("... remote socket address is " + socket.getRemoteSocketAddress());
    output.write(args[1].getBytes());
    socket.shutdownOutput();
    while (true) {
        b = input.read();
        if (b == -1) {
           break;
         }
         System.out.print((char) b);
      }
     System.out.println();
     socket.close();
   }
 }

What is the best way to change part of a server to get multithreading. Please, help

+4
source share
2 answers

After the accepted connection, you should transfer Socketto a new stream. A simple way is to use ExecutorServiceto process threads. Code example:

public static void main(String[] args) throws IOException {
    // Replace with suitable executor
    ExecutorService executor = Executors.newCachedThreadPool();
    ServerSocket serverSocket = new ServerSocket(79);

    while (true) {
        final Socket socket = serverSocket.accept();
        executor.execute(new Runnable() {

            public void run() {
                try {
                    handleSocket(socket);
                } catch (IOException e) {
                    // Handle exception
                }

            }

            private void handleSocket(final Socket socket) throws IOException {
                // Do stuff with your socket, same as original code
            }
        });
    }
}
+1
source

, , . :

public class EchoServer {

    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket = new ServerSocket(79);

        while (true) {
            final Socket socket = serverSocket.accept();
            new Thread(new Runnable() {
                public void run() {
                    System.out.println("Accepted an echo request");
                    System.out.println("... local socket address " + socket.getLocalSocketAddress());
                    System.out.println("... remote socket address " + socket.getRemoteSocketAddress());

                    try {
                        InputStream input = socket.getInputStream();
                        OutputStream output = socket.getOutputStream();
                        while (true) {
                            int b = input.read();
                            if (b == -1) break;
                            output.write(b);
                        }
                        socket.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        }
    }
}
0

All Articles