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
Modifier and TypeFieldDescriptionstatic final int
Default maximum incoming connection queue length (50).protected final Logger
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionprotected ServerSocket
Create the server's socket via which connections will be accepted.int
Get maximum connect backlog.Get address to listen on.int
Get maximum number of concurrent connections.int
getPort()
Get TCP port to listen on.protected abstract SocketHandler
getSocketHandler
(Socket socket) Get theSocketHandler
that will handle a new connection using the given socket.void
setBacklog
(int backlog) void
setInetAddress
(InetAddress address) void
setMaxConnections
(int maxConnections) void
setPort
(int port) void
start()
Verifies configuration and starts accepting incoming connections.void
stop()
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
SocketAcceptor
creates the socket and sets the "reuse address" flag. Subclasses may override.- Returns:
- server socket
- Throws:
IOException
- if an I/O error occurs
-
getSocketHandler
Get theSocketHandler
that will handle a new connection using the given socket.- Parameters:
socket
- connection socket- Returns:
- new handler, or
null
to disconnect the socket immediately - Throws:
IOException
- if an I/O error occurs
-