The actual work of the server socket is performed by an instance of the SocketImpl
class. An application can change the socket factory that creates the socket implementation to configure itself to create sockets appropriate to the local firewall.
Field Detail |
private SocketImpl impl
private static SocketImplFactory factory
Constructor Detail |
private ServerSocket()
IOException
- IO error when opening the socket.public ServerSocket(int port)
0
creates a socket on any free port. The maximum queue length for incoming connection indications (a request to connect) is set to 50
. If a connection indication arrives when the queue is full, the connection is refused.
If the application has specified a server socket factory, that factory's createSocketImpl
method is called to create the actual socket implementation. Otherwise a "plain" socket is created.
If there is a security manager, its checkListen
method is called with the port
argument as its argument to ensure the operation is allowed. This could result in a SecurityException.
port
- the port number, or 0
to use any free port.IOException
- if an I/O error occurs when opening the socket.SecurityException
- if a security manager exists and its checkListen
method doesn't allow the operation.public ServerSocket(int port, int backlog)
0
creates a socket on any free port. The maximum queue length for incoming connection indications (a request to connect) is set to the backlog
parameter. If a connection indication arrives when the queue is full, the connection is refused.
If the application has specified a server socket factory, that factory's createSocketImpl
method is called to create the actual socket implementation. Otherwise a "plain" socket is created.
If there is a security manager, its checkListen
method is called with the port
argument as its argument to ensure the operation is allowed. This could result in a SecurityException.
port
- the specified port, or 0
to use any free port.backlog
- the maximum length of the queue.IOException
- if an I/O error occurs when opening the socket.SecurityException
- if a security manager exists and its checkListen
method doesn't allow the operation.public ServerSocket(int port, int backlog, InetAddress bindAddr)
If there is a security manager, this method calls its checkListen
method with the port
argument as its argument to ensure the operation is allowed. This could result in a SecurityException.
port
- the local TCP portbacklog
- the listen backlogbindAddr
- the local InetAddress the server will bind toSecurityException
- if a security manager exists and its checkListen
method doesn't allow the operation.IOException
- if an I/O error occurs when opening the socket.Method Detail |
public InetAddress getInetAddress()
null
if the socket is not yet connected.public int getLocalPort()
public Socket accept()
A new Socket s
is created and, if there is a security manager, the security manager's checkAccept
method is called with s.getInetAddress().getHostAddress()
and s.getPort()
as its arguments to ensure the operation is allowed. This could result in a SecurityException.
IOException
- if an I/O error occurs when waiting for a connection.SecurityException
- if a security manager exists and its checkListen
method doesn't allow the operation.protected final void implAccept(Socket s)
s
- the SocketIOException
- if an I/O error occurs when waiting for a connection.public void close()
IOException
- if an I/O error occurs when closing the socket.public synchronized void setSoTimeout(int timeout)
timeout
- the specified timeout, in millisecondsSocketException
- if there is an error in the underlying protocol, such as a TCP error.public synchronized int getSoTimeout()
IOException
- if an I/O error occurspublic String toString()
String
.public static synchronized void setSocketFactory(SocketImplFactory fac)
When an application creates a new server socket, the socket implementation factory's createSocketImpl
method is called to create the actual socket implementation.
If there is a security manager, this method first calls the security manager's checkSetFactory
method to ensure the operation is allowed. This could result in a SecurityException.
fac
- the desired factory.IOException
- if an I/O error occurs when setting the socket factory.SocketException
- if the factory has already been defined.SecurityException
- if a security manager exists and its checkSetFactory
method doesn't allow the operation.