Multithreading Java server Server.java
import java.io.*; import java.net.*; public class Server { public static void main(String argv[]) throws Exception { try { ServerSocket socket = new ServerSocket(2200, 100); while ( true ) new Service(socket.accept()).start(); } catch ( IOException e ) { System.err.println("IOException: " + e.getMessage()); } } } class Service extends Thread { Socket client; public Service(Socket client) { this.client = client; } public void run() { byte[] buf = new byte[1024]; int c, i = 0; try { InputStream in = client.getInputStream(); OutputStream out = client.getOutputStream(); do { c = in.read(); // read filename from socket buf[i++] = (byte)c; } while ( c != 10 ); String name = new String(buf, 0, i).trim(); FileInputStream file = new FileInputStream(name); do { i = 0; do { c = file.read(); // load buffer from file buf[i++] = (byte)c; } while ( i < 1023 && c != 10 && c != -1 ); out.write(buf, 0, i); // write buffer down socket } while ( c != -1 ); file.close(); out.flush(); client.close(); } catch ( IOException e ) { System.err.println("IOException: " + e.getMessage()); } } }