import java.io.*;
import java.net.*;


class SocketClientTest
{
  public static void main(String[] args) throws IOException {

    Socket conn = null;
    PrintWriter out = null;
    BufferedReader in = null;

    try {
      conn = new Socket("localhost", 2000);
      out = new PrintWriter(conn.getOutputStream(), true);
      in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    } catch (UnknownHostException e) {
      System.err.println("Don't know about that server.");
      System.exit(1);
    } catch (IOException e) {
      System.err.println("Couldn't get I/O for the connection.");
      System.exit(1);
    }

    BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
    String fromServer;
    String fromUser;

    while (true) 
    { 
      System.out.print("Your text: ");
      fromUser = stdIn.readLine();
      if (fromUser != null) {
        System.out.println("Client: " + fromUser);
        out.println(fromUser);
      }
      fromServer = in.readLine();
      if (fromServer == null || "quit".equals(fromServer))
      {
        System.out.println("Server sent a quit.");
        break;
      }
      else 
      {
        System.out.println("Server: " + fromServer);
      }
    }

    out.close();
    in.close();
    stdIn.close();
    conn.close();
    }
  }
