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

class TCPServer {

    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).
		
		 ServerSocket serverSocket = new ServerSocket(9000);
		 // waits for a new connection
		 while (true) 
		 {
			 System.out.println("waiting for connection at 9000");
			 Socket s = serverSocket.accept();
			 System.out.println("connection established from " + s.getInetAddress());
			 
			 // create a BufferedReader object to read strings from
			 // the socket.
			 BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
			 String input = br.readLine();
			 
			 DataOutputStream output = new DataOutputStream(s.getOutputStream());
			 
			 // keep repeating until an empty line is read.
			 while (input.compareTo("") != 0) {
				 // convert input to upper case and echo back to
				 // client.
				 output.writeBytes(input.toUpperCase() + "\n");
				 input = br.readLine();
			}
			// close current connection
			s.close();
		 }
    }
}
