I've built simple client-server model using sockets. The server receives 1 type of request: 2 numbers from client, sums them, waits for 5 seconds and sends the response back to the client. I'm trying to send 20 asynchronous request from the client without waiting for response. The client should sums all the numbers from all the 20 Reponses from server. I'm trying to understand what should I use and how? Threads on the server, or the client and how? I've added my client and server classes. Server:
public class Server {
public static void main(String[] args) throws IOException {
try {
//Make a ServerSocket to listen for message
ServerSocket ss = new ServerSocket(7777);
while (true == true) {
//Accept input from socket
Socket s = ss.accept();
//Read input from socket
InputStreamReader streamReader = new InputStreamReader(s.getInputStream());
BufferedReader reader = new BufferedReader(streamReader);
String message = reader.readLine();
System.out.println(message);
//parse the json recieved from client and sum the 2 numbers
Object obj = new JSONParser().parse(String.valueOf(message));
JSONObject jo = (JSONObject) obj;
long num1 = (long) jo.get("num1");
long num2 = (long) jo.get("num2");
long sum = num1 + num2;
Thread.sleep(5000);
//putting response as json
JSONObject jsonResponse = new JSONObject();
jsonResponse.put("response", sum);
//get the message and write it to the socket as response
PrintWriter writer = new PrintWriter(s.getOutputStream());
writer.println(jsonResponse);
//System.out.println(df);
writer.close();
}
} catch (IOException | ParseException | InterruptedException ex) {
System.out.println(ex);
}
}
}
Client:
public class Client {
public static void main(String[] args) throws IOException {
try {
//this variable will sum all the responses from server
long sumOfAllResponses = 0;
for(int i = 0 ; i< 20; i++){
//Create a Socket with ip and port number
Socket s = new Socket("localhost", 7777);
Scanner in = new Scanner(System.in);
PrintWriter writer = new PrintWriter(s.getOutputStream());
InputStreamReader streamReader = new InputStreamReader(s.getInputStream());
BufferedReader reader = new BufferedReader(streamReader);
//Creating json with 2 numbers to be sent to server as a request
JSONObject jsonRequest = new JSONObject();
jsonRequest.put("num1", 1);
jsonRequest.put("num2", 1);
System.out.println(jsonRequest);
//Make a printWriter and write the message to the socket
writer.println(jsonRequest);
writer.flush();
//Get the response message from server
String responseMessage = reader.readLine();
//parse the response and add the result to the sum variable
Object obj = new JSONParser().parse(String.valueOf(responseMessage));
JSONObject jo = (JSONObject) obj;
sumOfAllResponses += (long) jo.get("response");
}
System.out.println(sumOfAllResponses);
}
catch (IOException | ParseException ex) {
ex.printStackTrace(); // (**)
}
}
}