Package java.net |
![]() Previous |
![]() Java API |
![]() Index |
![]() Next |
public final class java.net.ServerSocket extends java.lang.Object { // Constructors public ServerSocket(int port); public ServerSocket(int port, int count); // Methods public Socket accept(); public void close(); public InetAddress getInetAddress(); public int getLocalPort(); public static void setSocketFactory(SocketImplFactory fac); public String toString(); }
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.
public ServerSocket(int port)throws IOExceptionCreates 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 (see Class SocketImpl) is created.
Parameter Description port the port number, or 0 to use any free port Throw:
If an IO error occurs when opening the socket.
public ServerSocket(int port, int count)throws IOExceptionCreates a server socket and binds it to the specified local port number. 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 count 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 (see Class SocketImpl) is created.
Parameter Description port the specified port, or 0 to use any free port count the maximum length of the queue Throw:
if an I/O error occurs when opening the socket.
public Socket accept()throws IOExceptionListens for a connection to be made to this socket and accepts it. The method blocks until a connection is made.
Throw:
If an I/O error occurs when waiting for a connection.
public void close()throws IOExceptionCloses this socket.
Throw:
If an I/O occurs error when closing the socket.
public InetAddress getInetAddress()Return Value:
Returns The address to which this socket is connected, or null if the socket is not yet connected.
public int getLocalPort()Return Value:
Returns the port number to which this socket is listening.
public static void setSocketFactory(SocketImplFactory fac)throws IOExceptionSets 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.
Parameter Description fac the desired factory Throw:
If the factory has already been defined.
Throw:
If an I/O error occurs when setting the socket factory.
public String toString()Return Value:
Returns a string representation of this socket.
Overrides:
toString in class Object .