import java.io.*; import java.net.*; public class TestServer { public int PORT = 9000; public static void main(java.lang.String args[]) { new TestServer(); } public TestServer() { try { ServerSocket serverSock = new ServerSocket(PORT); Socket clientSock; BufferedReader in; // i/o for the server PrintWriter out; while (true) { System.out.println("Waiting for a client..."); clientSock = serverSock.accept( ); System.out.println("Client connection from " + clientSock.getInetAddress( ).getHostAddress( ) ); // Get I/O streams from the socket in = new BufferedReader( new InputStreamReader(clientSock.getInputStream( ) ) ); out = new PrintWriter( clientSock.getOutputStream( ), true ); processClient(in, out); // interact with a client // Close client connection //clientSock.close( ); //System.out.println("Client connection closed\n"); } } catch(Exception e) { System.out.println(e); } } private void processClient(BufferedReader in, PrintWriter out) { String line; boolean done = false; try { while (!done) { if((line = in.readLine( )) == null) done = true; else { System.out.println("Client msg: " + line); if (line.trim( ).equals("bye")) done = true; else doRequest(line, out); } } } catch(IOException e) { System.out.println(e); } } private void doRequest(String line, PrintWriter out) { out.println(line); } }