java.lang.Object | |
↳ | java.net.ServerSocket |
Known Direct Subclasses |
This class represents a server-side socket that waits for incoming client
connections. A 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.
Public Constructors | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Constructs a new unbound
ServerSocket . | |||||||||||
Constructs a new
ServerSocket instance bound to the given port . | |||||||||||
Constructs a new
ServerSocket instance bound to the given port . | |||||||||||
Constructs a new
ServerSocket instance bound to the given localAddress
and port . |
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Waits for an incoming request and blocks until the connection is opened.
| |||||||||||
Binds this server socket to the given local socket address with a maximum
backlog of 50 unaccepted connections.
| |||||||||||
Binds this server socket to the given local socket address.
| |||||||||||
Closes this server socket and its implementation.
| |||||||||||
Returns this socket's
ServerSocketChannel , if one exists. | |||||||||||
Gets the local IP address of this server socket if this socket has ever been bound,
null otherwise. | |||||||||||
Gets the local port of this server socket or
-1 if the socket is not bound. | |||||||||||
Gets the local socket address of this server socket or
null if the socket is unbound. | |||||||||||
Returns this socket's
receive buffer size . | |||||||||||
Gets the value of the socket option
SocketOptions.SO_REUSEADDR . | |||||||||||
Gets the socket
accept timeout . | |||||||||||
Returns whether this server socket is bound to a local address and port
or not.
| |||||||||||
Returns whether this server socket is closed or not.
| |||||||||||
Sets performance preferences for connection time, latency and bandwidth.
| |||||||||||
Sets this socket's
receive buffer size . | |||||||||||
Sets the value for the socket option
SocketOptions.SO_REUSEADDR . | |||||||||||
Sets the
accept timeout in milliseconds for this socket. | |||||||||||
Sets the server socket implementation factory of this instance.
| |||||||||||
Returns a textual representation of this server socket including the
address, port and the state.
|
Protected Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Invokes the server socket implementation to accept a connection on the
given socket
aSocket . |
[Expand]
Inherited Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
From class
java.lang.Object
| |||||||||||
From interface
java.io.Closeable
| |||||||||||
From interface
java.lang.AutoCloseable
|
Constructs a new unbound ServerSocket
.
IOException | if an error occurs while creating the socket. |
---|
Constructs a new ServerSocket
instance bound to the given port
.
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. |
---|
Constructs a new ServerSocket
instance bound to the given port
.
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. |
---|
Constructs a new 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. |
---|
Waits for an incoming request and blocks until the connection is opened. This method returns a socket object representing the just opened connection.
IOException | if an error occurs while accepting a new connection. |
---|
Binds this server socket to the given local socket address with a maximum
backlog of 50 unaccepted connections. If the 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. |
Binds this server socket to the given local socket address. If the
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. |
Closes this server socket and its implementation. Any attempt to connect to this socket thereafter will fail.
IOException | if an error occurs while closing this socket. |
---|
Returns this socket's 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
open()
.
Gets the local IP address of this server socket if this socket has ever been bound,
null
otherwise. This is useful for multihomed hosts.
Gets the local port of this server socket or -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.
Gets the local socket address of this server socket or 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.
Returns this socket's receive buffer size
.
SocketException |
---|
Gets the value of the socket option SocketOptions.SO_REUSEADDR
.
true
if the option is enabled, false
otherwise.SocketException | if an error occurs while reading the option value. |
---|
Gets the socket accept timeout
.
IOException | if the option cannot be retrieved. |
---|
Returns whether this server socket is bound to a local address and port or not.
true
if this socket is bound, false
otherwise.
Returns whether this server socket is closed or not.
true
if this socket is closed, false
otherwise.
Sets performance preferences for connection time, latency and 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. |
Sets this socket's receive buffer size
.
SocketException |
---|
Sets the value for the socket option SocketOptions.SO_REUSEADDR
.
reuse | the socket option setting. |
---|
SocketException | if an error occurs while setting the option value. |
---|
Sets the 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. |
---|
Sets the server socket implementation factory of this instance. This method may only be invoked with sufficient security privilege and only once during the application lifetime.
aFactory | the streaming socket factory to be used for further socket instantiations. |
---|
IOException | if the factory could not be set or is already set. |
---|
Returns a textual representation of this server socket including the
address, port and the state. The port field is set to 0
if there
is no connection to the server socket.
Invokes the server socket implementation to accept a connection on the
given socket aSocket
.
aSocket | the concrete SocketImpl to accept the connection
request on. |
---|
IOException | if the connection cannot be accepted. |
---|