The actual work of the server socket is performed by an instance
of the SocketImpl
class.
The ServerSocket
class defines convenience
methods to set and get several socket options. This class also
defines the setOption
and getOption
methods to set
and query socket options.
A ServerSocket
supports the following options:
Additional (implementation specific) options may also be supported.
Option Name Description SO_RCVBUF
The size of the socket receive buffer SO_REUSEADDR
Re-use address
java.net.SocketImpl
, java.nio.channels.ServerSocketChannel
Modifier and Type | Field and Description |
---|---|
private volatile boolean | |
private volatile boolean | |
private volatile boolean | |
private static volatile SocketImplFactory | factory
The factory for all server sockets. |
private final SocketImpl | |
private volatile Set | |
private final Object |
Access | Constructor and Description |
---|---|
private | |
protected | ServerSocket(SocketImpl
an instance of a SocketImpl to use on the ServerSocket. impl)Creates a server socket with a user-specified |
public | |
public | ServerSocket(int
the port number, or port)0 to use a port
number that is automatically allocated.Creates a server socket, bound to the specified port. |
public | ServerSocket(int
the port number, or port, int 0 to use a port
number that is automatically allocated.requested maximum length of the queue of incoming
connections. backlog)Creates a server socket and binds it to the specified local port number, with the specified backlog. |
public | ServerSocket(int
the port number, or port, int 0 to use a port
number that is automatically allocated.requested maximum length of the queue of incoming
connections. backlog, InetAddress the local InetAddress the server will bind to bindAddr)Create a server with the specified port, listen backlog, and local IP address to bind to. |
Modifier and Type | Method and Description |
---|---|
public Socket | |
public void | bind(SocketAddress
The IP address and port number to bind to. endpoint)Binds the |
public void | bind(SocketAddress
The IP address and port number to bind to. endpoint, int requested maximum length of the queue of
incoming connections. backlog)Binds the |
private static Void | |
public void | |
private static SocketImpl | |
private void | |
private void | ensureCompatible(SocketImpl si)
Throws IOException if the server SocketImpl and the given client SocketImpl are not both platform or custom SocketImpls. |
public ServerSocketChannel | Returns: the server-socket channel associated with this socket, ornull if this socket was not created
for a channelReturns the unique |
private SocketImpl | |
public InetAddress | Returns: the address to which this socket is bound, or the loopback address if denied by the security manager, ornull if the socket is unbound.Returns the local address of this server socket. |
public int | Returns: the port number to which this socket is listening or -1 if the socket is not bound yet.Returns the port number on which this socket is listening. |
public SocketAddress | Returns: aSocketAddress representing the local endpoint of
this socket, or a SocketAddress representing the
loopback address if denied by the security manager,
or null if the socket is not bound yet.Returns the address of the endpoint this socket is bound to. |
public < The type of the socket option value T> T | Returns: The value of the socket option.The socket option name)Returns the value of a socket option. |
public int | Returns: the value of theSO_RCVBUF option for this Socket .Gets the value of the |
public boolean | Returns: aboolean indicating whether or not
SO_REUSEADDR is enabled.Tests if |
public int | |
protected final void | implAccept(Socket
the Socket s)Subclasses of ServerSocket use this method to override accept() to return their own subclass of socket. |
private SocketImpl | |
private void | implAccept(SocketImpl si)
Accepts a new connection so that the given SocketImpl is connected to the peer. |
public boolean | Returns: true if the ServerSocket successfully bound to an addressReturns the binding state of the ServerSocket. |
public boolean | |
private SocketImpl | Returns: the new platform SocketImplAccepts a connection with a new platform SocketImpl. |
public < The type of the socket option value T> ServerSocket | Returns: this ServerSocketThe socket option name, T The value of the socket option. A value of value)null
may be valid for some options.Sets the value of a socket option. |
public void | setPerformancePreferences(int
An connectionTime, int int expressing the relative importance of a short
connection timeAn latency, int int expressing the relative importance of low
latencyAn bandwidth)int expressing the relative importance of high
bandwidthSets performance preferences for this ServerSocket. |
public void | setReceiveBufferSize(int
the size to which to set the receive buffer
size. This value must be greater than 0. size)Sets a default proposed value for the
|
public void | setReuseAddress(boolean
whether to enable or disable the socket option on)Enable/disable the |
public static synchronized void | setSocketFactory(SocketImplFactory
the desired factory. fac)
Deprecated
since 17. Use a
javax. and subclass ServerSocket
directly.
Sets the server socket implementation factory for the application. |
public void | setSoTimeout(int
the specified timeout, in milliseconds timeout)Enable/disable |
public Set | Returns: A set of the socket options supported by this socket. This set may be empty if the socket's SocketImpl cannot be created.Returns a set of the socket options supported by this server socket. |
public String | Returns: a string representation of this socket.Overrides java. Returns the implementation address and implementation port of
this socket as a |
bound | back to summary |
---|---|
private volatile boolean bound |
closed | back to summary |
---|---|
private volatile boolean closed |
created | back to summary |
---|---|
private volatile boolean created |
factory | back to summary |
---|---|
private static volatile SocketImplFactory factory The factory for all server sockets. |
impl | back to summary |
---|---|
private final SocketImpl impl |
options | back to summary |
---|---|
private volatile Set<SocketOption<?>> options |
socketLock | back to summary |
---|---|
private final Object socketLock |
ServerSocket | back to summary |
---|---|
private ServerSocket(Void unused, SocketImpl impl) Creates a server socket with the given |
ServerSocket | back to summary |
---|---|
protected ServerSocket(SocketImpl impl) Creates a server socket with a user-specified
|
ServerSocket | back to summary |
---|---|
public ServerSocket() throws IOException Creates an unbound server socket.
|
ServerSocket | back to summary |
---|---|
public ServerSocket(int port) throws IOException Creates a server socket, bound to the specified port. A port number
of
The maximum queue length for incoming connection indications (a
request to connect) is set to If the application has specified a server socket implementation factory, that factory's createSocketImpl method is called to create the actual socket implementation. Otherwise a system-default socket implementation is created.
If there is a security manager,
its
|
ServerSocket | back to summary |
---|---|
public ServerSocket(int port, int backlog) throws IOException Creates a server socket and binds it to the specified local port
number, with the specified backlog.
A port number of
The maximum queue length for incoming connection indications (a
request to connect) is set to the If the application has specified a server socket implementation factory, that factory's createSocketImpl method is called to create the actual socket implementation. Otherwise a system-default socket implementation is created.
If there is a security manager,
its
|
ServerSocket | back to summary |
---|---|
public ServerSocket(int port, int backlog, InetAddress bindAddr) throws IOException Create a server with the specified port, listen backlog, and
local IP address to bind to. The bindAddr argument
can be used on a multi-homed host for a ServerSocket that
will only accept connect requests to one of its addresses.
If bindAddr is null, it will default accepting
connections on any/all local addresses.
The port must be between 0 and 65535, inclusive.
A port number of If there is a security manager, this method
calls its
|
accept | back to summary |
---|---|
public Socket accept() throws IOException Listens for a connection to be made to this socket and accepts it. The method blocks until a connection is made. This method is interruptible in the following circumstances:
A new Socket Implementation Note An instance of this class using a system-default
|
bind | back to summary |
---|---|
public void bind(SocketAddress endpoint) throws IOException Binds the
If the address is
|
bind | back to summary |
---|---|
public void bind(SocketAddress endpoint, int backlog) throws IOException Binds the
If the address is
The
|
checkPermission | back to summary |
---|---|
private static Void checkPermission() |
close | back to summary |
---|---|
public void close() throws IOException Implements java. Closes this socket.
Any thread currently blocked in If this socket has an associated channel then the channel is closed as well.
|
createImpl | back to summary |
---|---|
private static SocketImpl createImpl() Create a SocketImpl for a server socket. The SocketImpl is created without an underlying socket. |
customImplAccept | back to summary |
---|---|
private void customImplAccept(SocketImpl si) throws IOException Accepts a new connection with the given custom SocketImpl. |
ensureCompatible | back to summary |
---|---|
private void ensureCompatible(SocketImpl si) throws IOException Throws IOException if the server SocketImpl and the given client SocketImpl are not both platform or custom SocketImpls. |
getChannel | back to summary |
---|---|
public ServerSocketChannel getChannel() Returns the unique A server socket will have a channel if, and only if, the channel
itself was created via the
|
getImpl | back to summary |
---|---|
private SocketImpl getImpl() throws SocketException Returns the
|
getInetAddress | back to summary |
---|---|
public InetAddress getInetAddress() Returns the local address of this server socket.
If the socket was bound prior to being
If there is a security manager set, its
|
getLocalPort | back to summary |
---|---|
public int getLocalPort() Returns the port number on which this socket is listening.
If the socket was bound prior to being
|
getLocalSocketAddress | back to summary |
---|---|
public SocketAddress getLocalSocketAddress() Returns the address of the endpoint this socket is bound to.
If the socket was bound prior to being
If there is a security manager set, its
|
getOption | back to summary |
---|---|
public <T> T getOption(SocketOption<T> name) throws IOException Returns the value of a socket option.
|
getReceiveBufferSize | back to summary |
---|---|
public int getReceiveBufferSize() throws SocketException Gets the value of the Note, the value actually set in the accepted socket is determined by
calling
|
getReuseAddress | back to summary |
---|---|
public boolean getReuseAddress() throws SocketException Tests if
|
getSoTimeout | back to summary |
---|---|
public int getSoTimeout() throws IOException Retrieve setting for
|
implAccept | back to summary |
---|---|
protected final void implAccept(Socket s) throws IOException Subclasses of ServerSocket use this method to override accept() to return their own subclass of socket. So a FooServerSocket will typically hand this method a newly created, unbound, FooSocket. On return from implAccept the FooSocket will be connected to a client. The behavior of this method is unspecified when invoked with a
socket that is not newly created and unbound. Any socket options set
on the given socket prior to invoking this method may or may not be
preserved when the connection is accepted. It may not be possible to
accept a connection when this socket has a Implementation Note An instance of this class using a system-default
|
implAccept | back to summary |
---|---|
private SocketImpl implAccept() throws IOException Accepts a connection with a new SocketImpl.
|
implAccept | back to summary |
---|---|
private void implAccept(SocketImpl si) throws IOException Accepts a new connection so that the given SocketImpl is connected to the peer. The SocketImpl and connection are closed if the connection is denied by the security manager.
|
isBound | back to summary |
---|---|
public boolean isBound() Returns the binding state of the ServerSocket.
If the socket was bound prior to being closed,
then this method will continue to return
|
isClosed | back to summary |
---|---|
public boolean isClosed() Returns the closed state of the ServerSocket.
|
platformImplAccept | back to summary |
---|---|
private SocketImpl platformImplAccept() throws IOException Accepts a connection with a new platform SocketImpl.
|
setOption | back to summary |
---|---|
public <T> ServerSocket setOption(SocketOption<T> name, T value) throws IOException Sets the value of a socket option.
|
setPerformancePreferences | back to summary |
---|---|
public void setPerformancePreferences(int connectionTime, int latency, int bandwidth) Sets performance preferences for this ServerSocket. Sockets use the TCP/IP protocol by default. Some implementations may offer alternative protocols which have different performance characteristics than TCP/IP. This method allows the application to express its own preferences as to how these tradeoffs should be made when the implementation chooses from the available protocols. Performance preferences are described by three integers
whose values indicate the relative importance of short connection time,
low latency, and high bandwidth. The absolute values of the integers
are irrelevant; in order to choose a protocol the values are simply
compared, with larger values indicating stronger preferences. If the
application prefers short connection time over both low latency and high
bandwidth, for example, then it could invoke this method with the values
Invoking this method after this socket has been bound will have no effect. This implies that in order to use this capability requires the socket to be created with the no-argument constructor.
|
setReceiveBufferSize | back to summary |
---|---|
public void setReceiveBufferSize(int size) throws SocketException Sets a default proposed value for the
The value of
It is possible to change the value subsequently, by calling
Failure to do this will not cause an error, and the buffer size may be set to the requested value but the TCP receive window in sockets accepted from this ServerSocket will be no larger than 64K bytes.
|
setReuseAddress | back to summary |
---|---|
public void setReuseAddress(boolean on) throws SocketException Enable/disable the
When a TCP connection is closed the connection may remain
in a timeout state for a period of time after the connection
is closed (typically known as the
Enabling
When a
The behaviour when
|
setSocketFactory | back to summary |
---|---|
public static synchronized void setSocketFactory(SocketImplFactory fac) throws IOException
Deprecated since 17. Use a Sets the server socket implementation factory for the application. The factory can be specified only once.
When an application creates a new server socket, the socket
implementation factory's
Passing
If there is a security manager, this method first calls
the security manager's
|
setSoTimeout | back to summary |
---|---|
public void setSoTimeout(int timeout) throws SocketException Enable/disable
|
supportedOptions | back to summary |
---|---|
public Set Returns a set of the socket options supported by this server socket. This method will continue to return the set of options even after the socket has been closed.
|
toString | back to summary |
---|---|
public String toString() Overrides java. Returns the implementation address and implementation port of
this socket as a
If there is a security manager set, and this socket is
bound, its
|