import java.io.*; import java.net.*; public class EchoServer implements Runnable{ Socket conn; /* member variable, for run() */ public EchoServer(Socket s){ conn = s; } public static void main(String argv[]) throws IOException { /* run is a member method, must create object to call it */ ServerSocket server = new ServerSocket(Integer.valueOf(argv[0]).intValue()); /* provide the socket as argument to the constructor call */ while(true) new Thread(new EchoServer(server.accept())).start(); } public void run() { try{ /* run does not throw any exceptions so we must try and catch */ InputStream in = conn.getInputStream(); OutputStream out = conn.getOutputStream(); byte[] buffer = new byte[8192]; int n; while((n = in.read(buffer, 0, 8192))!=-1) out.write(buffer, 0, n); in.close(); out.close(); conn.close(); } /* handle exception */ catch(IOException e){ e.printStackTrace(); } } }