Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package tcplistener;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.PrintWriter;
- import java.net.ServerSocket;
- import java.net.Socket;
- import java.util.concurrent.Executor;
- import java.util.concurrent.Executors;
- /**
- * Handles connections on a thread pool and invokes the handle method as
- * implemented by an instance of the ConnectionHandler interface when a
- * connection is established
- *
- * @author Dialog
- */
- public class EchoServer implements Runnable
- {
- /**
- * Maximum number of threads to use
- */
- private static final int MAX_THREADS = 5;
- /**
- * Port to listen on
- */
- private static final int PORT = 8080;
- /**
- * Underlaying socket which is used for remote I/O operations
- */
- private final Socket SOCKET;
- /**
- * Defines behavior for when a response from the server is expected
- */
- private final ConnectionHandler HANDLER;
- /**
- * Simply initializes a new EchoServer object which echoes everything sent
- * to us back
- *
- * @param socket Socket to read from and write to
- * @param handler Defines behavior for when a response from the server is
- * expected
- */
- public EchoServer(Socket socket, ConnectionHandler handler)
- {
- this.SOCKET = socket;
- this.HANDLER = handler;
- }
- @Override
- public void run()
- {
- try (BufferedReader in = new BufferedReader(
- new InputStreamReader(SOCKET.getInputStream())))
- {
- String line;
- while ((line = in.readLine()) != null)
- {
- HANDLER.handle(SOCKET, line);
- }
- } catch (IOException ex)
- {
- //Socket has most likely been closed
- }
- }
- /**
- * Starts a thread pool and listens for incoming connections on the
- * specified port, then handles all connections over seperate threads
- *
- * @param args the command line arguments
- * @throws IOException Thrown if an I/O exception occurs while opening the
- * socket or
- */
- public static void main(String[] args) throws IOException
- {
- ServerSocket listener = new ServerSocket(PORT);
- Executor executor = Executors.newFixedThreadPool(MAX_THREADS);
- ConnectionHandler handler = new ConnectionHandler()
- {
- @Override
- public void handle(Socket socket, String content)
- {
- try (PrintWriter out = new PrintWriter(socket.getOutputStream()))
- {
- out.write("Server says: " + content);
- System.out.printf("Echoed \"%s\" to %s\n", content,
- socket.getRemoteSocketAddress().toString());
- } catch (IOException ex)
- {
- System.err.println("IOException: " + ex.getMessage());
- }
- }
- };
- System.out.printf("Accepting connections on port %d...\n", PORT);
- while (true)
- {
- Socket socket = listener.accept();
- executor.execute(new EchoServer(socket, handler));
- }
- }
- }
- /**
- * Classes implementing this interface define behavior which can be called by
- * EchoServer to handle an incoming connection
- *
- * @author Dialog
- */
- interface ConnectionHandler
- {
- /**
- * Defines behavior for when a response from the server is expected
- *
- * @param socket Socket to read from and write to
- * @param handler Defines behavior for when a response from the server is
- * expected
- */
- public void handle(Socket socket, String content);
- }
Advertisement
Add Comment
Please, Sign In to add comment