//SimpServer.java import java.net.*; import java.io.*; public class SimpServer { public static void main( String[] args ) throws IOException, ClassNotFoundException { //all the socket methods throw exceptions, so instead of cluttering //this example with try catches, use throws... ServerSocket server; Socket connection; ObjectInputStream input; ObjectOutputStream output; String message; //1. create a server socket. use netstat to see it. //bind the server to the TCP port server = new ServerSocket( 12345, 10 );//port, queue (max # waiting clients) do { System.out.println( "Server waiting..." ); //2. listen indefinitely (block) for a connection from a client connection = server.accept(); //returns when connection w/client established System.out.println( "Server got Connection from: " + connection.getInetAddress().getHostName() ); //3. get OutputStream and InputStream objects for byte send/receive. //output stream for objects for greater convenience. //sockets make network IO look like file IO. output = new ObjectOutputStream( connection.getOutputStream() ); output.flush(); //send header info? input = new ObjectInputStream( connection.getInputStream() ); //server will input first message = (String)input.readObject(); System.out.println( "Server received: " + message ); //then output message = "roger, over and out"; output.writeObject( message ); output.flush(); //? input.close(); output.close(); connection.close(); } while (true); } }