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. AServerSockethandles the requests and sends back an appropriate reply. The actual tasks that a server socket must accomplish are implemented by an internalSocketImplinstance.
-
-
Constructor Summary
Constructors Constructor Description ServerSocket()Constructs a new unboundServerSocket.ServerSocket(int port)Constructs a newServerSocketinstance bound to the givenportusing a wildcard address.ServerSocket(int port, int backlog)Constructs a newServerSocketinstance bound to the givenportusing a wildcard address.ServerSocket(int port, int backlog, InetAddress localAddress)Constructs a newServerSocketinstance bound to the givenlocalAddressandport.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods 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 if this socket has ever been bound,nullotherwise.intgetLocalPort()Gets the local port of this server socket or-1if the socket is not bound.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 Detail
-
ServerSocket
public ServerSocket() throws IOExceptionConstructs a new unboundServerSocket.- Throws:
IOException- if an error occurs while creating the socket.
-
ServerSocket
public ServerSocket(int port) throws IOExceptionConstructs a newServerSocketinstance bound to the givenportusing a wildcard address. 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
public ServerSocket(int port, int backlog) throws IOExceptionConstructs a newServerSocketinstance bound to the givenportusing a wildcard address. 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
public ServerSocket(int port, int backlog, InetAddress localAddress) throws IOExceptionConstructs 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 Detail
-
getImpl$
public SocketImpl getImpl$()
-
accept
public Socket accept() throws IOException
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
public void close() throws IOExceptionCloses 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
public InetAddress getInetAddress()
Gets the local IP address of this server socket if this socket has ever been bound,nullotherwise. 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 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.- Returns:
- the local port this server is listening on.
-
getSoTimeout
public int getSoTimeout() throws IOExceptionGets the socketaccept timeout.- Throws:
IOException- if the option cannot be retrieved.
-
implAccept
protected final void implAccept(Socket aSocket) throws IOException
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
public static void setSocketFactory(SocketImplFactory aFactory) throws IOException
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
public void setSoTimeout(int timeout) throws SocketExceptionSets 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
public String 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
public void bind(SocketAddress localAddr) throws IOException
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
public void bind(SocketAddress localAddr, int backlog) throws IOException
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
public SocketAddress getLocalSocketAddress()
Gets the local socket address of this server socket ornullif 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:
- 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
public void setReuseAddress(boolean reuse) throws SocketExceptionSets 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
public boolean getReuseAddress() throws SocketExceptionGets 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
public void setReceiveBufferSize(int size) throws SocketExceptionSets this socket'sreceive buffer size.- Throws:
SocketException
-
getReceiveBufferSize
public int getReceiveBufferSize() throws SocketExceptionReturns this socket'sreceive buffer size.- Throws:
SocketException
-
getChannel
public ServerSocketChannel 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.
-
-