public class ServerSocket extends Object implements AutoCloseable
Socket
class, most instance methods of this class
simply redirect their calls to an implementation class.Constructor and Description |
---|
ServerSocket()
Constructor that simply sets the implementation.
|
ServerSocket(int port)
Creates a server socket and binds it to the specified port.
|
ServerSocket(int port,
int backlog)
Creates a server socket and binds it to the specified port.
|
ServerSocket(int port,
int backlog,
InetAddress bindAddr)
Creates a server socket and binds it to the specified port.
|
Modifier and Type | Method and Description |
---|---|
Socket |
accept()
Accepts a new connection and returns a connected
Socket instance
representing that connection. |
void |
bind(SocketAddress endpoint)
Binds the server socket to a specified socket address
|
void |
bind(SocketAddress endpoint,
int backlog)
Binds the server socket to a specified socket address
|
void |
close()
Closes this socket and stops listening for connections
|
InetAddress |
getInetAddress()
This method returns the local address to which this socket is bound
|
int |
getLocalPort()
This method returns the local port number to which this socket is bound
|
SocketAddress |
getLocalSocketAddress()
Returns the local socket address
|
int |
getReceiveBufferSize()
This method returns the value of the system level socket option SO_RCVBUF,
which is used by the operating system to tune buffer sizes for data
transfers.
|
boolean |
getReuseAddress()
Checks if the SO_REUSEADDR option is enabled
|
int |
getSoTimeout()
Retrieves the current value of the SO_TIMEOUT setting.
|
boolean |
isBound()
Returns true when the socket is bound, otherwise false
|
boolean |
isClosed()
Returns true if the socket is closed, otherwise false
|
void |
setReceiveBufferSize(int size)
This method sets the value for the system level socket option SO_RCVBUF to
the specified value.
|
void |
setReuseAddress(boolean on)
Enables/Disables the SO_REUSEADDR option
|
void |
setSoTimeout(int timeout)
Sets the value of SO_TIMEOUT.
|
String |
toString()
Returns the value of this socket as a
String . |
public ServerSocket() throws IOException
IOException
- If an error occurspublic ServerSocket(int port) throws IOException
port
- The port number to bind toIOException
- If an error occurspublic ServerSocket(int port, int backlog) throws IOException
port
- The port number to bind tobacklog
- The length of the pending connection queueIOException
- If an error occurspublic ServerSocket(int port, int backlog, InetAddress bindAddr) throws IOException
port
- The port number to bind tobacklog
- The length of the pending connection queuebindAddr
- The address to bind to, or null to bind to all addressesIOException
- If an error occurspublic Socket accept() throws IOException
Socket
instance
representing that connection. This method will block until a connection is
available.IOException
- If an error occursSocketTimeoutException
- If a timeout was previously set with setSoTimeout and the
timeout has been reachedpublic void bind(SocketAddress endpoint) throws IOException
endpoint
- The socket address to bind toIOException
- If an error occursIllegalArgumentException
- If address type is not supportedpublic void bind(SocketAddress endpoint, int backlog) throws IOException
endpoint
- The socket address to bind tobacklog
- The length of the pending connection queueIOException
- If an error occursIllegalArgumentException
- If address type is not supportedpublic void close() throws IOException
close
in interface AutoCloseable
IOException
- If an error occurspublic InetAddress getInetAddress()
public int getLocalPort()
public SocketAddress getLocalSocketAddress()
public int getReceiveBufferSize() throws SocketException
SocketException
- If an error occurs or Socket is not connectedpublic boolean getReuseAddress() throws SocketException
SocketException
- If an error occurspublic int getSoTimeout() throws IOException
IOException
- If an error occurspublic boolean isBound()
public boolean isClosed()
public void setReceiveBufferSize(int size) throws SocketException
size
- The new receive buffer size.SocketException
- If an error occurs or Socket is not connectedIllegalArgumentException
- If size is 0 or negativepublic void setReuseAddress(boolean on) throws SocketException
on
- true if SO_REUSEADDR should be enabled, false otherwiseSocketException
- If an error occurspublic void setSoTimeout(int timeout) throws SocketException
timeout
- The new SO_TIMEOUT valueSocketException
- If an error occurs