Strumenti Utente

Strumenti Sito


lpr-b:nameserver
no way to compare when less than two revisions

Differenze

Queste sono le differenze tra la revisione selezionata e la versione attuale della pagina.


lpr-b:nameserver [13/11/2007 alle 18:53 (17 anni fa)] (versione attuale) – creata Marco Danelutto
Linea 1: Linea 1:
 +<code java>
 +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));
 +                }
 +        }
 +}
 +
 +
 +</code>
lpr-b/nameserver.txt · Ultima modifica: 13/11/2007 alle 18:53 (17 anni fa) da Marco Danelutto

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki