Strumenti Utente

Strumenti Sito


lpr-b-2007-2008:nameserver
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<String,InetAddress> uscache = new HashMap<String,InetAddress>();
                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));
                }
        }
}
lpr-b-2007-2008/nameserver.txt · Ultima modifica: 19/09/2008 alle 14:08 (11 anni fa) (modifica esterna)