Package org.dellroad.stuff.net
Class SocketAcceptor
java.lang.Object
org.dellroad.stuff.net.SocketAcceptor
Spring bean that listens for connections on a TCP socket and spawns a child thread to handle each new connection.
Subclasses must implement
getSocketHandler(java.net.Socket). Only the port property is required to be set.-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intDefault maximum incoming connection queue length (50).protected final Logger -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected ServerSocketCreate the server's socket via which connections will be accepted.intGet maximum connect backlog.Get address to listen on.intGet maximum number of concurrent connections.intgetPort()Get TCP port to listen on.protected abstract SocketHandlergetSocketHandler(Socket socket) Get theSocketHandlerthat will handle a new connection using the given socket.voidsetBacklog(int backlog) voidsetInetAddress(InetAddress address) voidsetMaxConnections(int maxConnections) voidsetPort(int port) voidstart()Verifies configuration and starts accepting incoming connections.voidstop()Stop accepting connections.
-
Field Details
-
DEFAULT_BACKLOG
public static final int DEFAULT_BACKLOGDefault maximum incoming connection queue length (50).- See Also:
-
log
-
-
Constructor Details
-
SocketAcceptor
public SocketAcceptor()
-
-
Method Details
-
getInetAddress
Get address to listen on.- Returns:
- address to listen on, or null for any
-
setInetAddress
-
getBacklog
public int getBacklog()Get maximum connect backlog.- Returns:
- maximum backlogged connections
-
setBacklog
public void setBacklog(int backlog) -
getMaxConnections
public int getMaxConnections()Get maximum number of concurrent connections.- Returns:
- max conncurrent connections, or zero for unlimited
-
setMaxConnections
public void setMaxConnections(int maxConnections) -
getPort
public int getPort()Get TCP port to listen on.- Returns:
- TCP listening port
-
setPort
public void setPort(int port) -
start
public void start()Verifies configuration and starts accepting incoming connections. Does nothing if already started.- Throws:
RuntimeException- if an I/O error occurs setting up the server socketIllegalArgumentException- if configuration is invalid
-
stop
public void stop()Stop accepting connections. Does nothing if already stopped.Any currently active connections are stopped via
SocketHandler.stop(), and this method waits until all such connections have returned fromSocketHandler.handleConnection()before returning. -
createServerSocket
Create the server's socket via which connections will be accepted.The implementation in
SocketAcceptorcreates the socket and sets the "reuse address" flag. Subclasses may override.- Returns:
- server socket
- Throws:
IOException- if an I/O error occurs
-
getSocketHandler
Get theSocketHandlerthat will handle a new connection using the given socket.- Parameters:
socket- connection socket- Returns:
- new handler, or
nullto disconnect the socket immediately - Throws:
IOException- if an I/O error occurs
-