package nslookup; import java.io.IOException; import java.net.InetAddress; import java.net.ServerSocket; import java.net.Socket; import java.util.Collections; import java.util.HashMap; import java.util.Map; import java.util.concurrent.Executor; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class NameServer { public static final int NAMESERVERPORT = 54321; public static final int MAXTHREADS = 4; /** * @param args */ public static void main(String[] args) { /** this is the cache, accessed through the host name ... */ HashMap uscache = new HashMap(); Map cache = Collections.synchronizedMap(uscache); /** this is the thread pool aimed at serving the requests ... */ ExecutorService tp = Executors.newFixedThreadPool(MAXTHREADS); /** prepare the server socket to accept request ... */ ServerSocket ss = null; try { ss = new ServerSocket(NAMESERVERPORT); } catch(IOException e) {e.printStackTrace(); return; } while (true) { /** main server loop */ System.out.println("NameServer: looping ... "); Socket s = null; try { s = ss.accept(); } catch (IOException e) {e.printStackTrace(); return; } System.out.println("Connessione accettata"); tp.execute(new ServerThread(cache,s)); } } }