1	// Simple server - echoes whatever the client sends

    3	import java.io.*;
    4	import java.net.*;

    6	public class SimpleServer {

    8	    // Choose a port outside of the range 1-1024:
    9	    public static final int PORT = 8080;

   11	    public static void main(String[ ] args) throws IOException {
   12	        System.out.println("SERVER");
   13	        System.out.println();
   14	        ServerSocket s = new ServerSocket(PORT);
   15	        System.out.println("Started: " + s);
   16	        try {
   17	            // Blocks until a connection occurs:
   18	            Socket socket = s.accept();
   19	            try {
   20	                System.out.println(
   21	                    "Connection accepted: "+ socket);
   22	                BufferedReader in =
   23	                    new BufferedReader(
   24	                        new InputStreamReader(
   25	                            socket.getInputStream()));
   26	                // Output is flushed by PrintWriter:
   27	                PrintWriter out =
   28	                    new PrintWriter(
   29	                        new BufferedWriter(
   30	                            new OutputStreamWriter(
   31	                                socket.getOutputStream())), true);
   32	                while (true) {
   33	                    String str = in.readLine();
   34	                    System.out.println("Echoing: " + str);
   35	                    out.println(str);
   36	                    if (str.equals("END"))
   37	                        break;
   38	                }
   39	            // Always close the two sockets...
   40	            } finally {
   41	                System.out.println("Closing...");
   42	                socket.close();
   43	            }
   44	        } finally {
   45	            s.close();
   46	        }
   47	        System.out.println();
   48	        System.out.println("* END SERVER *");
   49	    }
   50	}

SERVER

Started: ServerSocket[addr=C64110-A/0.0.0.0,port=0,localport=8080]
Connection accepted: Socket[addr=localhost/127.0.0.1,port=1235,localport=8080]
Echoing: Howdy 0
Echoing: Howdy 1
Echoing: Howdy 2
Echoing: Howdy 3
Echoing: Howdy 4
Echoing: Howdy 5
Echoing: Howdy 6
Echoing: Howdy 7
Echoing: Howdy 8
Echoing: Howdy 9
Echoing: END
Closing...

* END SERVER *