Commit 2ae34c4c authored by Michele Fiori's avatar Michele Fiori

Minor changes

parent 53b573d1
...@@ -8,7 +8,7 @@ class Client { ...@@ -8,7 +8,7 @@ class Client {
String sentence; String sentence;
String modifiedSentence; String modifiedSentence;
// input stream initialization (from user keyboard) /* Initialize input stream (from user keyboard) */
BufferedReader inFromUser = BufferedReader inFromUser =
new BufferedReader(new InputStreamReader(System.in)); new BufferedReader(new InputStreamReader(System.in));
...@@ -17,24 +17,26 @@ class Client { ...@@ -17,24 +17,26 @@ class Client {
6789: server service port number */ 6789: server service port number */
Socket clientSocket = new Socket("localhost", 6789); Socket clientSocket = new Socket("localhost", 6789);
// output stream towards socket initialization /* Initialize output stream towards the socket */
DataOutputStream outToServer = DataOutputStream outToServer =
new DataOutputStream(clientSocket.getOutputStream()); new DataOutputStream(clientSocket.getOutputStream());
// input stream from socket initialization /* Initialize input stream from the socket */
BufferedReader inFromServer = BufferedReader inFromServer =
new BufferedReader( new BufferedReader(
new InputStreamReader(clientSocket.getInputStream())); new InputStreamReader(clientSocket.getInputStream()));
// read a line from the user /* Read an input line */
sentence = inFromUser.readLine(); sentence = inFromUser.readLine();
// send the line to the server /* Send the line to the server*/
outToServer.writeBytes(sentence + '\n'); outToServer.writeBytes(sentence + '\n');
// read the response from the server /* Read response from the server (string ending with '\n') */
modifiedSentence = inFromServer.readLine(); modifiedSentence = inFromServer.readLine();
System.out.println("FROM SERVER: " + modifiedSentence); System.out.println("FROM SERVER: " + modifiedSentence);
/* Close the socket */
clientSocket.close(); clientSocket.close();
} }
} }
...@@ -8,34 +8,39 @@ class IterativeServer { ...@@ -8,34 +8,39 @@ class IterativeServer {
String clientSentence; String clientSentence;
String capitalizedSentence; String capitalizedSentence;
// create a "listening socket" on the specified port /* Create a "listening socket" on the specified port */
ServerSocket welcomeSocket = new ServerSocket(6789); ServerSocket welcomeSocket = new ServerSocket(6789);
while(true) { while(true) {
/* accept is a blocking call /*
once a new connection arrived, it creates * Call to accept function (blocking call).
a new "established socket" */ * When a new connection arrives a new "established socket" is created
*/
Socket connectionSocket = welcomeSocket.accept(); Socket connectionSocket = welcomeSocket.accept();
// input stream from the socket initialization /* Initialize input stream from the socket */
BufferedReader inFromClient = BufferedReader inFromClient =
new BufferedReader( new BufferedReader(
new InputStreamReader(connectionSocket.getInputStream())); new InputStreamReader(connectionSocket.getInputStream()));
// output stream to the socket initialization /* Initialize output stream towards the socket */
DataOutputStream outToClient = DataOutputStream outToClient =
new DataOutputStream(connectionSocket.getOutputStream()); new DataOutputStream(connectionSocket.getOutputStream());
// read a line (that terminates with \n) from the client /* Read a line (ending with '\n') from the client */
clientSentence = inFromClient.readLine(); clientSentence = inFromClient.readLine();
// wait for 10 seconds /* simulate a processing time of 10 seconds*/
//Thread.sleep(10000); //Thread.sleep(10000);
/* Build the response */
capitalizedSentence = clientSentence.toUpperCase() + '\n'; capitalizedSentence = clientSentence.toUpperCase() + '\n';
// send the response to the client /* Send the response to the client */
outToClient.writeBytes(capitalizedSentence); outToClient.writeBytes(capitalizedSentence);
/* Close the connection socket */
connectionSocket.close();
} }
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment