public final class AsyncServerSocket extends AsyncChannel
java.nio.channels.ServerSocketChannel
, passing I/O
events back to an ServerSocketListener
instance.
Creating an opening a TCP service is similar to all eBus async channels:
server socket listener
.
An instance of this class handles the async service
callbacks.
The service socket listener has two callbacks: one to receive
accepted socket channels
and the other when the service socket closes.
ServerSocketListener.handleAccept(SocketChannel, AsyncServerSocket)
is expected to build a socket
and then
"open"
the socket with the accepted socket channel. This new async
TCP socket is then handled in the same manner as described
above in AsyncSocket
.
Having defined an instantiated an async service socket
listener, an async service socket may now be built. Assuming
the service socket listener is stored in a variable named
serviceListener
, the build code is:
final AsyncServerSocket.ServerBuilder builder = AsyncServerSocket.builder();
AsyncServerSocket serverSocket = builder.selector("srvSelector") // Optional. Defaults to AsyncChannel.defaultSelector().
.listener(serviceListener) // Required.
.build();
Opening an async service socket only requires knowing the
TCP address:
serverSocket.open(address);
.
Async server sockets immediately open because there is no
associated connection process. Once successfully opened, the
application should be ready to receive accepted socket
channels.
ServerSocketListener
Modifier and Type | Class and Description |
---|---|
static class |
AsyncServerSocket.ServerBuilder
Constructs an
AsyncServerSocket instance based on
the parameters set via this builder's API. |
AsyncChannel.BufferMode, AsyncChannel.ChannelBuilder<B extends AsyncChannel.ChannelBuilder<?>>, AsyncChannel.ConnectState
LOCALHOST, mChannel, mInputBuffer, mLocalAddress, mOutputBuffer, mOutputLock, mRemoteAddress, mSelectorThread
Modifier and Type | Method and Description |
---|---|
java.net.SocketAddress |
address()
Returns server socket address.
|
static AsyncServerSocket.ServerBuilder |
builder()
Returns the builder used to open new
AsyncServerSocket instances. |
void |
close()
Closes the
ServerSocketChannel immediately. |
void |
closeNow()
Closes the
ServerSocketChannel immediately. |
boolean |
isOpen()
Returns
true if the server socket is open and
false otherwise. |
void |
open(int port)
Opens the TCP server socket on the specified port using
the wildcard address.
|
void |
open(java.net.SocketAddress address)
Opens the TCP server socket on the specified address and
port.
|
createSelector, defaultSelectorName, getOption, inputBufferSize, isSelector, localSocketAddress, outputBufferSize, remoteSocketAddress, selectorNames, setOption, supportedOptions
public void close()
ServerSocketChannel
immediately.close
in class AsyncChannel
open(int)
,
closeNow()
public void closeNow()
ServerSocketChannel
immediately.closeNow
in class AsyncChannel
open(int)
,
close()
public boolean isOpen()
true
if the server socket is open and
false
otherwise.isOpen
in class AsyncChannel
true
if the server socket is open and
false
otherwise.AsyncChannel.close()
,
AsyncChannel.closeNow()
@Nullable public java.net.SocketAddress address()
null
if the
server socket is closed.public void open(int port) throws java.io.IOException
port
- open server socket on this port.java.lang.IllegalArgumentException
- if address
is not a valid TCP address.java.io.IOException
- if TCP server socket open fails.open(SocketAddress)
,
close()
public void open(java.net.SocketAddress address) throws java.io.IOException
public static AsyncServerSocket.ServerBuilder builder()
AsyncServerSocket
instances.AsyncServerSocket
builder.Copyright © 2001 - 2024. Charles W. Rapp. All rights reserved.