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
source
share