shlist

share and manage lists between multiple people
Log | Files | Refs

Server.java (961B)


      1 package shlist;
      2 
      3 import java.io.IOException;
      4 import java.net.ServerSocket;
      5 import java.net.Socket;
      6 import java.net.SocketTimeoutException;
      7 import java.util.concurrent.ExecutorService;
      8 import java.util.concurrent.Executors;
      9 
     10 public class Server {
     11 	
     12 	public final static int THREAD_COUNT = 4;
     13 	public final static int PORT = 5437;
     14 	
     15 	public static void main(String[] args) {
     16 		
     17 		ExecutorService exec = Executors.newFixedThreadPool(THREAD_COUNT);
     18 		
     19 		ServerSocket sSock = null;
     20 		Socket sock = null;
     21 		
     22 		try {
     23 			sSock = new ServerSocket(PORT);
     24 		} catch (IOException e) {
     25 			System.out.println("IO Error1: " + e);
     26 		}
     27 		
     28 		// Add the runtime hook, for ctrl+c
     29 		Runtime r = Runtime.getRuntime();
     30 		r.addShutdownHook(new Hooker(sSock, exec));
     31 		
     32 		while (true) {
     33 			try {
     34 				sock = sSock.accept();
     35 				sock.setSoTimeout(5000);
     36 				exec.execute(new Worker(sock));
     37 			} catch (IOException e) {
     38 					System.out.println("IO Error: " + e);
     39 					break;
     40 			}
     41 		}
     42 	}
     43 }