//SimpChatServer.java import java.net.*; import java.io.*; import javax.swing.*; public class SimpChatServer { 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 transMsg, recvMsg; //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 recvMsg = (String)input.readObject(); System.out.println(" Server received: " + recvMsg ); //then output transMsg = JOptionPane.showInputDialog("SERVER: Enter message"); output.writeObject( transMsg ); output.flush(); //? input.close(); output.close(); connection.close(); } while (true); } }