Improved TCPClient.java

As per our in-class discussion on 2002-02-22, Kurose and Ross's TCPClient.java program can be improved by reading the line in from the user before opening the TCP connection to the server. That way the single-threaded nature of the server won't create such a problem; a client with a slow user won't block other clients.


import java.io.*;
import java.net.*;
class TCPClient {
    public static void main(String argv[]) throws Exception
    {
        String sentence;
        String modifiedSentence;
        BufferedReader inFromUser =
            new BufferedReader(
                               new InputStreamReader(System.in));
        sentence = inFromUser.readLine();
        Socket clientSocket = new Socket("max.mcs.gac.edu.", 6789);
        DataOutputStream outToServer =
            new DataOutputStream(
                                 clientSocket.getOutputStream());
        BufferedReader inFromServer =
            new BufferedReader (new InputStreamReader
                (clientSocket.getInputStream()));
        outToServer.writeBytes(sentence + '\n');
        modifiedSentence = inFromServer.readLine();
        System.out.println("FROM SERVER: " + 
                           modifiedSentence);
        clientSocket.close();
    }
}