Dialog

EchoServer

Dec 12th, 2014
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.72 KB | None | 0 0
  1. package tcplistener;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.io.PrintWriter;
  7. import java.net.ServerSocket;
  8. import java.net.Socket;
  9. import java.util.concurrent.Executor;
  10. import java.util.concurrent.Executors;
  11.  
  12. /**
  13.  * Handles connections on a thread pool and invokes the handle method as
  14.  * implemented by an instance of the ConnectionHandler interface when a
  15.  * connection is established
  16.  *
  17.  * @author Dialog
  18.  */
  19. public class EchoServer implements Runnable
  20. {
  21.  
  22.     /**
  23.      * Maximum number of threads to use
  24.      */
  25.     private static final int MAX_THREADS = 5;
  26.  
  27.     /**
  28.      * Port to listen on
  29.      */
  30.     private static final int PORT = 8080;
  31.  
  32.     /**
  33.      * Underlaying socket which is used for remote I/O operations
  34.      */
  35.     private final Socket SOCKET;
  36.  
  37.     /**
  38.      * Defines behavior for when a response from the server is expected
  39.      */
  40.     private final ConnectionHandler HANDLER;
  41.  
  42.     /**
  43.      * Simply initializes a new EchoServer object which echoes everything sent
  44.      * to us back
  45.      *
  46.      * @param socket Socket to read from and write to
  47.      * @param handler Defines behavior for when a response from the server is
  48.      * expected
  49.      */
  50.     public EchoServer(Socket socket, ConnectionHandler handler)
  51.     {
  52.         this.SOCKET = socket;
  53.         this.HANDLER = handler;
  54.     }
  55.  
  56.     @Override
  57.     public void run()
  58.     {
  59.         try (BufferedReader in = new BufferedReader(
  60.                 new InputStreamReader(SOCKET.getInputStream())))
  61.         {
  62.             String line;
  63.  
  64.             while ((line = in.readLine()) != null)
  65.             {
  66.                 HANDLER.handle(SOCKET, line);
  67.             }
  68.         } catch (IOException ex)
  69.         {
  70.             //Socket has most likely been closed
  71.         }
  72.     }
  73.  
  74.     /**
  75.      * Starts a thread pool and listens for incoming connections on the
  76.      * specified port, then handles all connections over seperate threads
  77.      *
  78.      * @param args the command line arguments
  79.      * @throws IOException Thrown if an I/O exception occurs while opening the
  80.      * socket or
  81.      */
  82.     public static void main(String[] args) throws IOException
  83.     {
  84.         ServerSocket listener = new ServerSocket(PORT);
  85.         Executor executor = Executors.newFixedThreadPool(MAX_THREADS);
  86.  
  87.         ConnectionHandler handler = new ConnectionHandler()
  88.         {
  89.  
  90.             @Override
  91.             public void handle(Socket socket, String content)
  92.             {
  93.                 try (PrintWriter out = new PrintWriter(socket.getOutputStream()))
  94.                 {
  95.                     out.write("Server says: " + content);
  96.  
  97.                     System.out.printf("Echoed \"%s\" to %s\n", content,
  98.                             socket.getRemoteSocketAddress().toString());
  99.                 } catch (IOException ex)
  100.                 {
  101.                     System.err.println("IOException: " + ex.getMessage());
  102.                 }
  103.             }
  104.         };
  105.  
  106.         System.out.printf("Accepting connections on port %d...\n", PORT);
  107.  
  108.         while (true)
  109.         {
  110.             Socket socket = listener.accept();
  111.             executor.execute(new EchoServer(socket, handler));
  112.         }
  113.     }
  114. }
  115.  
  116. /**
  117.  * Classes implementing this interface define behavior which can be called by
  118.  * EchoServer to handle an incoming connection
  119.  *
  120.  * @author Dialog
  121.  */
  122. interface ConnectionHandler
  123. {
  124.  
  125.     /**
  126.      * Defines behavior for when a response from the server is expected
  127.      *
  128.      * @param socket Socket to read from and write to
  129.      * @param handler Defines behavior for when a response from the server is
  130.      * expected
  131.      */
  132.     public void handle(Socket socket, String content);
  133. }
Advertisement
Add Comment
Please, Sign In to add comment