//
//  TCPClient.java
//
//  Created by Ooi Wei Tsang on 26/01/06.
//
import java.util.*;
import java.io.*;
import java.net.*;

public class TCPClient {

	public static void main (String args[]) throws Exception {
		// throws Exception here because don't want to deal
		// with errors in the rest of the code for simplicity.
		// (note: NOT a good practice!)

		// Connect to the server process running at host
		// pnp182-150... and port 9000.
		Socket s = new Socket("pnp182-150.comp.nus.edu.sg", 9000);
	
		// The next 2 lines create a buffer reader that
		// reads from the standard input.
		InputStreamReader isrServer = new InputStreamReader(s.getInputStream());
		BufferedReader serverReader = new BufferedReader(isrServer);
		
		// The next 2 lines create a output stream we can
		// write to.
		OutputStream os = s.getOutputStream();
		DataOutputStream serverWriter = new DataOutputStream(os);
		
		// Send a hello message to server
		serverWriter.writeBytes("hello\n");

		// Server should convert to upper case and reply.
		// Read server's reply below and output to screen.
		String response = serverReader.readLine();
		System.out.println(response);
		
		// Send an empty line to server to end communication.
		serverWriter.writeBytes("\n");
		
		s.close();
	}
}

