public class ServerSocket extends Object implements Closeable
ServerSocket handles the requests and sends back an
appropriate reply. The actual tasks that a server socket must accomplish are
implemented by an internal SocketImpl instance.| Constructor and Description |
|---|
ServerSocket()
Constructs a new unbound
ServerSocket. |
ServerSocket(int port)
Constructs a new
ServerSocket instance bound to the given port using a
wildcard address. |
ServerSocket(int port,
int backlog)
Constructs a new
ServerSocket instance bound to the given port using a
wildcard address. |
ServerSocket(int port,
int backlog,
InetAddress localAddress)
Constructs a new
ServerSocket instance bound to the given localAddress
and port. |
| Modifier and Type | Method and Description |
|---|---|
Socket |
accept()
Waits for an incoming request and blocks until the connection is opened.
|
void |
bind(SocketAddress localAddr)
Binds this server socket to the given local socket address with a maximum
backlog of 50 unaccepted connections.
|
void |
bind(SocketAddress localAddr,
int backlog)
Binds this server socket to the given local socket address.
|
void |
close()
Closes this server socket and its implementation.
|
ServerSocketChannel |
getChannel()
Returns this socket's
ServerSocketChannel, if one exists. |
SocketImpl |
getImpl$() |
InetAddress |
getInetAddress()
Gets the local IP address of this server socket if this socket has ever been bound,
null otherwise. |
int |
getLocalPort()
Gets the local port of this server socket or
-1 if the socket is not bound. |
SocketAddress |
getLocalSocketAddress()
Gets the local socket address of this server socket or
null if the socket is unbound. |
int |
getReceiveBufferSize()
Returns this socket's
receive buffer size. |
boolean |
getReuseAddress()
Gets the value of the socket option
SocketOptions.SO_REUSEADDR. |
int |
getSoTimeout()
Gets the socket
accept timeout. |
protected void |
implAccept(Socket aSocket)
Invokes the server socket implementation to accept a connection on the
given socket
aSocket. |
boolean |
isBound()
Returns whether this server socket is bound to a local address and port
or not.
|
boolean |
isClosed()
Returns whether this server socket is closed or not.
|
void |
setPerformancePreferences(int connectionTime,
int latency,
int bandwidth)
Sets performance preferences for connection time, latency and bandwidth.
|
void |
setReceiveBufferSize(int size)
Sets this socket's
receive buffer size. |
void |
setReuseAddress(boolean reuse)
Sets the value for the socket option
SocketOptions.SO_REUSEADDR. |
static void |
setSocketFactory(SocketImplFactory aFactory)
Sets the server socket implementation factory of this instance.
|
void |
setSoTimeout(int timeout)
Sets the
accept timeout in milliseconds for this socket. |
String |
toString()
Returns a textual representation of this server socket including the
address, port and the state.
|
public ServerSocket()
throws IOException
ServerSocket.IOException - if an error occurs while creating the socket.public ServerSocket(int port)
throws IOException
ServerSocket instance bound to the given port using a
wildcard address. The backlog is set to 50. If port == 0, a port will be assigned by
the OS.IOException - if an error occurs while creating the socket.public ServerSocket(int port,
int backlog)
throws IOException
ServerSocket instance bound to the given port using a
wildcard address. The backlog is set to backlog.
If port == 0, a port will be assigned by the OS.IOException - if an error occurs while creating the socket.public ServerSocket(int port,
int backlog,
InetAddress localAddress)
throws IOException
ServerSocket instance bound to the given localAddress
and port. The backlog is set to backlog.
If localAddress == null, the ANY address is used.
If port == 0, a port will be assigned by the OS.IOException - if an error occurs while creating the socket.public SocketImpl getImpl$()
public Socket accept() throws IOException
IOException - if an error occurs while accepting a new connection.public void close()
throws IOException
close in interface Closeableclose in interface AutoCloseableIOException - if an error occurs while closing this socket.public InetAddress getInetAddress()
null otherwise. This is useful for multihomed hosts.public int getLocalPort()
-1 if the socket is not bound.
If the socket has ever been bound this method will return the local port it was bound to,
even after it has been closed.public int getSoTimeout()
throws IOException
accept timeout.IOException - if the option cannot be retrieved.protected final void implAccept(Socket aSocket) throws IOException
aSocket.aSocket - the concrete SocketImpl to accept the connection
request on.IOException - if the connection cannot be accepted.public static void setSocketFactory(SocketImplFactory aFactory) throws IOException
aFactory - the streaming socket factory to be used for further socket
instantiations.IOException - if the factory could not be set or is already set.public void setSoTimeout(int timeout)
throws SocketException
accept timeout in milliseconds for this socket.
This accept timeout defines the period the socket will block waiting to
accept a connection before throwing an InterruptedIOException. The value
0 (default) is used to set an infinite timeout. To have effect
this option must be set before the blocking method was called.timeout - the timeout in milliseconds or 0 for no timeout.SocketException - if an error occurs while setting the option.public String toString()
0 if there
is no connection to the server socket.public void bind(SocketAddress localAddr) throws IOException
localAddr is set to
null the socket will be bound to an available local address on
any free port of the system.localAddr - the local address and port to bind on.IllegalArgumentException - if the SocketAddress is not supported.IOException - if the socket is already bound or a problem occurs during
binding.public void bind(SocketAddress localAddr, int backlog) throws IOException
localAddr is set to null the socket will be bound to an
available local address on any free port of the system.localAddr - the local machine address and port to bind on.backlog - the maximum number of unaccepted connections. Passing 0 or
a negative value yields the default backlog of 50.IllegalArgumentException - if the SocketAddress is not
supported.IOException - if the socket is already bound or a problem occurs
during binding.public SocketAddress getLocalSocketAddress()
null if the socket is unbound.
This is useful on multihomed hosts. If the socket has ever been bound this method will return
the local address it was bound to, even after it has been closed.public boolean isBound()
true if this socket is bound, false otherwise.public boolean isClosed()
true if this socket is closed, false otherwise.public void setReuseAddress(boolean reuse)
throws SocketException
SocketOptions.SO_REUSEADDR.reuse - the socket option setting.SocketException - if an error occurs while setting the option value.public boolean getReuseAddress()
throws SocketException
SocketOptions.SO_REUSEADDR.true if the option is enabled, false otherwise.SocketException - if an error occurs while reading the option value.public void setReceiveBufferSize(int size)
throws SocketException
receive buffer size.SocketExceptionpublic int getReceiveBufferSize()
throws SocketException
receive buffer size.SocketExceptionpublic ServerSocketChannel getChannel()
ServerSocketChannel, if one exists. A channel is
available only if this socket wraps a channel. (That is, you can go from a
channel to a socket and back again, but you can't go from an arbitrary socket to a channel.)
In practice, this means that the socket must have been created by
ServerSocketChannel.open().public void setPerformancePreferences(int connectionTime,
int latency,
int bandwidth)
This method does currently nothing.
connectionTime - the value representing the importance of a short connecting
time.latency - the value representing the importance of low latency.bandwidth - the value representing the importance of high bandwidth.