java.net
Class ServerSocket

public class ServerSocket
This class implements server sockets. A server socket waits for requests to come in over the network. It performs some operation based on that request, and then possibly returns a result to the requester.

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.

Version:
1.42, 08/16/00
Author:
unascribed
Since:
JDK1.0
See Also:
java.net.SocketImpl
java.net.ServerSocket#setSocketFactory(java.net.SocketImplFactory)
Field Detail

impl

private SocketImpl impl
The implementation of this Socket.

factory

private static SocketImplFactory factory
The factory for all server sockets.

Constructor Detail

ServerSocket

private ServerSocket()
Creates an unconnected server socket. Note: this method should not be public.
Throws:
IOException - IO error when opening the socket.

ServerSocket

public ServerSocket(int port)
Creates a server socket on a specified port. A port of 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.

Parameters:
port - the port number, or 0 to use any free port.
Throws:
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.
See Also:
java.net.SocketImpl
java.net.SocketImplFactory#createSocketImpl()
java.net.ServerSocket#setSocketFactory(java.net.SocketImplFactory)
SecurityManager#checkListen

ServerSocket

public ServerSocket(int port,
                    int backlog)
Creates a server socket and binds it to the specified local port number, with the specified backlog. A port number of 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.

Parameters:
port - the specified port, or 0 to use any free port.
backlog - the maximum length of the queue.
Throws:
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.
See Also:
java.net.SocketImpl
java.net.SocketImplFactory#createSocketImpl()
java.net.ServerSocket#setSocketFactory(java.net.SocketImplFactory)
SecurityManager#checkListen

ServerSocket

public ServerSocket(int port,
                    int backlog,
                    InetAddress bindAddr)
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.

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.

Parameters:
port - the local TCP port
backlog - the listen backlog
bindAddr - the local InetAddress the server will bind to
Throws:
SecurityException - 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.
Since:
JDK1.1
See Also:
SocketOptions
SocketImpl
SecurityManager#checkListen

Method Detail

getInetAddress

public InetAddress getInetAddress()
Returns the local address of this server socket.
Returns:
the address to which this socket is connected, or null if the socket is not yet connected.

getLocalPort

public int getLocalPort()
Returns the port on which this socket is listening.
Returns:
the port number to which this socket is listening.

accept

public Socket accept()
Listens for a connection to be made to this socket and accepts it. The method blocks until a connection is made.

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.

Returns:
the new Socket
Throws:
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.
See Also:
SecurityManager#checkAccept

implAccept

protected final void implAccept(Socket s)
Subclasses of ServerSocket use this method to override accept() to return their own subclass of socket. So a FooServerSocket will typically hand this method an empty FooSocket. On return from implAccept the FooSocket will be connected to a client.
Parameters:
s - the Socket
Throws:
IOException - if an I/O error occurs when waiting for a connection.
Since:
JDK1.1

close

public void close()
Closes this socket.
Throws:
IOException - if an I/O error occurs when closing the socket.

setSoTimeout

public synchronized void setSoTimeout(int timeout)
Enable/disable SO_TIMEOUT with the specified timeout, in milliseconds. With this option set to a non-zero timeout, a call to accept() for this ServerSocket will block for only this amount of time. If the timeout expires, a java.io.InterruptedIOException is raised, though the ServerSocket is still valid. The option must be enabled prior to entering the blocking operation to have effect. The timeout must be > 0. A timeout of zero is interpreted as an infinite timeout.
Parameters:
timeout - the specified timeout, in milliseconds
Throws:
SocketException - if there is an error in the underlying protocol, such as a TCP error.
Since:
JDK1.1
See Also:
#getSoTimeout()

getSoTimeout

public synchronized int getSoTimeout()
Retrive setting for SO_TIMEOUT. 0 returns implies that the option is disabled (i.e., timeout of infinity).
Returns:
the SO_TIMEOUT value
Throws:
IOException - if an I/O error occurs
Since:
JDK1.1
See Also:
#setSoTimeout(int)

toString

public String toString()
Returns the implementation address and implementation port of this socket as a String.
Returns:
a string representation of this socket.

setSocketFactory

public static synchronized void setSocketFactory(SocketImplFactory fac)
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 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.

Parameters:
fac - the desired factory.
Throws:
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.
See Also:
java.net.SocketImplFactory#createSocketImpl()
SecurityManager#checkSetFactory