//Importing the required classes. import java.io.*; import java.net.*; class TCPServer { public static void main(String argv[]) throws Exception { String clientSentence;//Variable to store characters received from client. String capitalizedSentence;//Variable to store the capitalized characters to be sent to client. //Instantiation of socket object on the server. Port number is 6789. ServerSocket welcomeSocket = new ServerSocket(6789); //Server continuously listens for requests from clients. while(true) { //Waiting for a request from client and instantiating a connection between server and client. Socket connectionSocket = welcomeSocket.accept(); //Instantiating BufferedReader and InputStreamReader classes to read strings from client. BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream())); //Instatiating object to send data back to client. DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream()); //Reading a line from client and storing it in the variable. clientSentence = inFromClient.readLine(); //Capitalizing the data read from client. capitalizedSentence = clientSentence.toUpperCase() + '\n'; //Displaying the client data on the server console. System.out.println(clientSentence); //Writing back the capitalized sentence back to client. outToClient.writeBytes(capitalizedSentence); //Closing the client reader. inFromClient.close(); //Closing the client writer. outToClient.close(); //Closing the connection. connectionSocket.close(); } } }