Package java.net
Class ServerSocket
java.lang.Object
java.net.ServerSocket
- All Implemented Interfaces:
Closeable,AutoCloseable
- Direct Known Subclasses:
SSLServerSocket
public class ServerSocket extends Object implements Closeable
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.-
Constructor Summary
Constructors Constructor Description ServerSocket()Constructs a new unboundServerSocket.ServerSocket(int port)Constructs a newServerSocketinstance bound to the givenport.ServerSocket(int port, int backlog)Constructs a newServerSocketinstance bound to the givenport.ServerSocket(int port, int backlog, InetAddress localAddress)Constructs a newServerSocketinstance bound to the givenlocalAddressandport. -
Method Summary
Modifier and Type Method Description Socketaccept()Waits for an incoming request and blocks until the connection is opened.voidbind(SocketAddress localAddr)Binds this server socket to the given local socket address with a maximum backlog of 50 unaccepted connections.voidbind(SocketAddress localAddr, int backlog)Binds this server socket to the given local socket address.voidclose()Closes this server socket and its implementation.ServerSocketChannelgetChannel()Returns this socket'sServerSocketChannel, if one exists.SocketImplgetImpl$()InetAddressgetInetAddress()Gets the local IP address of this server socket ornullif the socket is unbound.intgetLocalPort()Gets the local port of this server socket or-1if the socket is unbound.SocketAddressgetLocalSocketAddress()Gets the local socket address of this server socket ornullif the socket is unbound.intgetReceiveBufferSize()Returns this socket'sreceive buffer size.booleangetReuseAddress()Gets the value of the socket optionSocketOptions.SO_REUSEADDR.intgetSoTimeout()Gets the socketaccept timeout.protected voidimplAccept(Socket aSocket)Invokes the server socket implementation to accept a connection on the given socketaSocket.booleanisBound()Returns whether this server socket is bound to a local address and port or not.booleanisClosed()Returns whether this server socket is closed or not.voidsetPerformancePreferences(int connectionTime, int latency, int bandwidth)Sets performance preferences for connection time, latency and bandwidth.voidsetReceiveBufferSize(int size)Sets this socket'sreceive buffer size.voidsetReuseAddress(boolean reuse)Sets the value for the socket optionSocketOptions.SO_REUSEADDR.static voidsetSocketFactory(SocketImplFactory aFactory)Sets the server socket implementation factory of this instance.voidsetSoTimeout(int timeout)Sets theaccept timeoutin milliseconds for this socket.StringtoString()Returns a textual representation of this server socket including the address, port and the state.
-
Constructor Details
-
ServerSocket
Constructs a new unboundServerSocket.- Throws:
IOException- if an error occurs while creating the socket.
-
ServerSocket
Constructs a newServerSocketinstance bound to the givenport. The backlog is set to 50. Ifport == 0, a port will be assigned by the OS.- Throws:
IOException- if an error occurs while creating the socket.
-
ServerSocket
Constructs a newServerSocketinstance bound to the givenport. The backlog is set tobacklog. Ifport == 0, a port will be assigned by the OS.- Throws:
IOException- if an error occurs while creating the socket.
-
ServerSocket
Constructs a newServerSocketinstance bound to the givenlocalAddressandport. The backlog is set tobacklog. IflocalAddress == null, the ANY address is used. Ifport == 0, a port will be assigned by the OS.- Throws:
IOException- if an error occurs while creating the socket.
-
-
Method Details
-
getImpl$
-
accept
Waits for an incoming request and blocks until the connection is opened. This method returns a socket object representing the just opened connection.- Returns:
- the connection representing socket.
- Throws:
IOException- if an error occurs while accepting a new connection.
-
close
Closes this server socket and its implementation. Any attempt to connect to this socket thereafter will fail.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Throws:
IOException- if an error occurs while closing this socket.
-
getInetAddress
Gets the local IP address of this server socket ornullif the socket is unbound. This is useful for multihomed hosts.- Returns:
- the local address of this server socket.
-
getLocalPort
public int getLocalPort()Gets the local port of this server socket or-1if the socket is unbound.- Returns:
- the local port this server is listening on.
-
getSoTimeout
Gets the socketaccept timeout.- Throws:
IOException- if the option cannot be retrieved.
-
implAccept
Invokes the server socket implementation to accept a connection on the given socketaSocket.- Parameters:
aSocket- the concreteSocketImplto accept the connection request on.- Throws:
IOException- if the connection cannot be accepted.
-
setSocketFactory
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.- Parameters:
aFactory- the streaming socket factory to be used for further socket instantiations.- Throws:
IOException- if the factory could not be set or is already set.
-
setSoTimeout
Sets theaccept timeoutin milliseconds for this socket. This accept timeout defines the period the socket will block waiting to accept a connection before throwing anInterruptedIOException. The value0(default) is used to set an infinite timeout. To have effect this option must be set before the blocking method was called.- Parameters:
timeout- the timeout in milliseconds or 0 for no timeout.- Throws:
SocketException- if an error occurs while setting the option.
-
toString
Returns a textual representation of this server socket including the address, port and the state. The port field is set to0if there is no connection to the server socket. -
bind
Binds this server socket to the given local socket address with a maximum backlog of 50 unaccepted connections. If thelocalAddris set tonullthe socket will be bound to an available local address on any free port of the system.- Parameters:
localAddr- the local address and port to bind on.- Throws:
IllegalArgumentException- if theSocketAddressis not supported.IOException- if the socket is already bound or a problem occurs during binding.
-
bind
Binds this server socket to the given local socket address. If thelocalAddris set tonullthe socket will be bound to an available local address on any free port of the system.- Parameters:
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.- Throws:
IllegalArgumentException- if theSocketAddressis not supported.IOException- if the socket is already bound or a problem occurs during binding.
-
getLocalSocketAddress
Gets the local socket address of this server socket ornullif the socket is unbound. This is useful on multihomed hosts.- Returns:
- the local socket address and port this socket is bound to.
-
isBound
public boolean isBound()Returns whether this server socket is bound to a local address and port or not.- Returns:
trueif this socket is bound,falseotherwise.
-
isClosed
public boolean isClosed()Returns whether this server socket is closed or not.- Returns:
trueif this socket is closed,falseotherwise.
-
setReuseAddress
Sets the value for the socket optionSocketOptions.SO_REUSEADDR.- Parameters:
reuse- the socket option setting.- Throws:
SocketException- if an error occurs while setting the option value.
-
getReuseAddress
Gets the value of the socket optionSocketOptions.SO_REUSEADDR.- Returns:
trueif the option is enabled,falseotherwise.- Throws:
SocketException- if an error occurs while reading the option value.
-
setReceiveBufferSize
Sets this socket'sreceive buffer size.- Throws:
SocketException
-
getReceiveBufferSize
Returns this socket'sreceive buffer size.- Throws:
SocketException
-
getChannel
Returns this socket'sServerSocketChannel, 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 byServerSocketChannel.open(). -
setPerformancePreferences
public void setPerformancePreferences(int connectionTime, int latency, int bandwidth)Sets performance preferences for connection time, latency and bandwidth.This method does currently nothing.
- Parameters:
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.
-