Class EchoServer
java.lang.Object
org.newsclub.net.unix.server.SocketServer<SocketAddress, Socket, ServerSocket>
org.newsclub.net.unix.demo.server.EchoServer
A multi-threaded unix socket server that simply echoes all input, byte per byte.
- Author:
- Christian Kohlschütter
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected voiddoServeSocket(Socket socket) Called when a socket is ready to be served.protected ServerSocketReturns a new server socket.protected voidonAfterServingSocket(Socket socket) Called after the socket has been served.protected voidonBeforeServingSocket(Socket socket) Called before serving the socket.protected voidCalled when an exception was thrown while listening on the server socket.protected voidonServerBound(SocketAddress address) Called when the server has been bound to a socket.protected voidonServerBusy(long busySince) Called when the server is busy / not ready to accept a new connection.protected voidonServerReady(int activeCount) Called when the server is ready to accept a new connection.protected voidCalled when the server is shutting down.protected voidCalled when the server is starting up.protected voidonServerStopped(ServerSocket theServerSocket) Called when the server has been stopped.protected voidonServingException(Socket socket, Throwable t) Called when a throwable was thrown while serving a socket.protected voidonSocketExceptionAfterAccept(Socket socket, SocketException e) Called when aSocketExceptionwas thrown during "accept".protected voidCalled when aSocketExceptionwas thrown during "accept".protected voidonSubmitted(Socket socket, Future<?> submit) Called when a socket gets submitted into the process queue.Methods inherited from class SocketServer
doSocketClose, getListenAddress, getMaxConcurrentConnections, getServerBusyTimeout, getServerTimeout, getSocketTimeout, isReady, isRunning, onListenException, onServingException, setMaxConcurrentConnections, setServerBusyTimeout, setServerTimeout, setSocketTimeout, start, startAndWaitToBecomeReady, startAndWaitToBecomeReady, startThenStopAfter, stopModifier and TypeMethodDescriptionprotected voiddoSocketClose(Socket socket) Called upon closing a socket after serving the connection.protected SocketAddressReturns the address the server listens to.intReturns the maximum number of concurrent connections.intReturns the server-busy timeout (in milliseconds).intReturns the server timeout (in milliseconds).intReturns the socket timeout (in milliseconds).booleanisReady()Checks if the server is running and accepting new connections.booleanChecks if the server is running.protected voidCalled when an exception was thrown while listening on the server socket.protected voidonServingException(Socket socket, Exception e) Deprecated.voidsetMaxConcurrentConnections(int maxConcurrentConnections) Sets the maximum number of concurrent connections.voidsetServerBusyTimeout(int timeout) Sets the server-busy timeout (in milliseconds).voidsetServerTimeout(int timeout) Sets the server timeout (in milliseconds).voidsetSocketTimeout(int timeout) Sets the socket timeout (in milliseconds).voidstart()Starts the server, and returns immediately.voidStarts the server and waits until it is ready or had to stop due to an error.booleanstartAndWaitToBecomeReady(long duration, TimeUnit unit) Starts the server and waits until it is ready or had to stop due to an error.startThenStopAfter(long delay, TimeUnit unit) Requests that the server will be stopped after the given time delay.voidstop()Stops the server.
-
Constructor Details
-
EchoServer
-
-
Method Details
-
doServeSocket
Description copied from class:SocketServerCalled when a socket is ready to be served.- Specified by:
doServeSocketin classSocketServer<SocketAddress, Socket, ServerSocket>- Parameters:
socket- The socket to serve.- Throws:
IOException- If there was an error.
-
onServerStarting
protected void onServerStarting()Description copied from class:SocketServerCalled when the server is starting up.- Overrides:
onServerStartingin classSocketServer<SocketAddress, Socket, ServerSocket>
-
onServerBound
Description copied from class:SocketServerCalled when the server has been bound to a socket. This is not called when you instantiated the server with a pre-bound socket.- Overrides:
onServerBoundin classSocketServer<SocketAddress, Socket, ServerSocket>- Parameters:
address- The bound address.
-
onServerBusy
protected void onServerBusy(long busySince) Description copied from class:SocketServerCalled when the server is busy / not ready to accept a new connection. The frequency on how often this method is called when the server is busy is determined bySocketServer.getServerBusyTimeout().- Overrides:
onServerBusyin classSocketServer<SocketAddress, Socket, ServerSocket>- Parameters:
busySince- The time stamp since the server became busy.
-
onServerReady
protected void onServerReady(int activeCount) Description copied from class:SocketServerCalled when the server is ready to accept a new connection.- Overrides:
onServerReadyin classSocketServer<SocketAddress, Socket, ServerSocket>- Parameters:
activeCount- The current number of active tasks (= serving sockets).
-
onServerStopped
Description copied from class:SocketServerCalled when the server has been stopped.- Overrides:
onServerStoppedin classSocketServer<SocketAddress, Socket, ServerSocket>- Parameters:
theServerSocket- The server's socket that stopped, ornull.
-
onSubmitted
Description copied from class:SocketServerCalled when a socket gets submitted into the process queue.- Overrides:
onSubmittedin classSocketServer<SocketAddress, Socket, ServerSocket>- Parameters:
socket- The socket.submit- TheFuturereferencing the submission; it's "done" after the socket has been served.
-
onBeforeServingSocket
Description copied from class:SocketServerCalled before serving the socket.- Overrides:
onBeforeServingSocketin classSocketServer<SocketAddress, Socket, ServerSocket>- Parameters:
socket- The socket.
-
onServerShuttingDown
protected void onServerShuttingDown()Description copied from class:SocketServerCalled when the server is shutting down.- Overrides:
onServerShuttingDownin classSocketServer<SocketAddress, Socket, ServerSocket>
-
onSocketExceptionDuringAccept
Description copied from class:SocketServerCalled when aSocketExceptionwas thrown during "accept".- Overrides:
onSocketExceptionDuringAcceptin classSocketServer<SocketAddress, Socket, ServerSocket>- Parameters:
e- The exception.
-
onSocketExceptionAfterAccept
Description copied from class:SocketServerCalled when aSocketExceptionwas thrown during "accept".- Overrides:
onSocketExceptionAfterAcceptin classSocketServer<SocketAddress, Socket, ServerSocket>- Parameters:
socket- The socket.e- The exception.
-
onServingException
Description copied from class:SocketServerCalled when a throwable was thrown while serving a socket.- Overrides:
onServingExceptionin classSocketServer<SocketAddress, Socket, ServerSocket>- Parameters:
socket- The socket.t- The throwable.
-
onAfterServingSocket
Description copied from class:SocketServerCalled after the socket has been served.- Overrides:
onAfterServingSocketin classSocketServer<SocketAddress, Socket, ServerSocket>- Parameters:
socket- The socket.
-
onListenException
Description copied from class:SocketServerCalled when an exception was thrown while listening on the server socket.- Overrides:
onListenExceptionin classSocketServer<SocketAddress, Socket, ServerSocket>- Parameters:
e- The exception.- See Also:
-
newServerSocket
Description copied from class:SocketServerReturns a new server socket.- Specified by:
newServerSocketin classSocketServer<SocketAddress, Socket, ServerSocket>- Returns:
- The new socket (an
AFServerSocketif the listen address is anAFSocketAddress). - Throws:
IOException- on error.
-
SocketServer.onServingException(Socket, Throwable)