Packages  This Package  Prev  Next  Index  

§4.5 Class ServerSocket

public  final  class  java.net.ServerSocket
    extends  java.lang.Object  (I-§1.12)
{
        // Constructors
    public ServerSocket(int  port);	§4.5.1
    public ServerSocket(int  port, int  count);	§4.5.2

        // Methods
    public Socket accept();	§4.5.3
    public void close();	§4.5.4
    public InetAddress getInetAddress();	§4.5.5
    public int getLocalPort();	§4.5.6
    public static void 	§4.5.7
        setSocketFactory(SocketImplFactory  fac);
    public String toString();	§4.5.8
}
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 (I-§4.7). An application can change the socket factory that creates the socket implementation (I-§4.5.7) to configure itself to create sockets appropriate to the local firewall.


Constructors

ServerSocket

public ServerSocket(int port) throws IOException
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 (I-§4.5.7), that factory's createSocketImpl method (I-§4.13.1) is called to create the actual socket implementation. Otherwise a "plain" socket (see I-§4.7) is created.

Parameters:
port - the port number, or 0 to use any free port
Throws
IOException (I-§2.29)
If an IO error occurs when opening the socket.

ServerSocket

public ServerSocket(int port, int count) throws IOException
Creates 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 (I-§4.5.7), that factory's createSocketImpl method (I-§4.13.1) is called to create the actual socket implementation. Otherwise a "plain" socket (see I-§4.7) is created.

Parameters:
port - the specified port, or 0 to use any free port
count - the maximum length of the queue
Throws
IOException (I-§2.29)
if an I/O error occurs when opening the socket.

Methods

accept

public Socket accept() throws IOException
Listens for a connection to be made to this socket and accepts it. The method blocks until a connection is made.
Throws
IOException (I-§2.29)
If an I/O error occurs when waiting for a connection.

close

public void close() throws IOException
Closes this socket.
Throws
IOException (I-§2.29)
If an I/O occurs error when closing the socket.

getInetAddress

public InetAddress getInetAddress()
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 number to which this socket is listening.

setSocketFactory

public static void setSocketFactory(SocketImplFactory fac) throws IOException
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 (I-§4.13.1) is called to create the actual socket implementation.

Parameters:
fac - the desired factory
Throws
SocketException (I-§4.17)
If the factory has already been defined.
Throws
IOException (I-§2.29)
If an I/O error occurs when setting the socket factory.

toString

public String toString()
Returns:
a string representation of this socket.
Overrides:
toString in class Object (I-§1.12.9).

Packages  This Package  Prev  Next  Index
Java API Document (HTML generated by dkramer on April 22, 1996)
Copyright © 1996 Sun Microsystems, Inc. All rights reserved
Please send any comments or corrections to doug.kramer@sun.com