Package java.net Previous
Previous
Java API
Java API
Index
Index
Next
Next

Class ServerSocket

Constructors , Methods

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.


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 , that factory's createSocketImpl method is called to create the actual socket implementation. Otherwise a "plain" socket (see Class SocketImpl) is created.

ParameterDescription
port the port number, or 0 to use any free port

Throw:

IOException

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 , that factory's createSocketImpl method is called to create the actual socket implementation. Otherwise a "plain" socket (see Class SocketImpl) is created.

ParameterDescription
port the specified port, or 0 to use any free port
count the maximum length of the queue

Throw:

IOException

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.

Throw:

IOException

If an I/O error occurs when waiting for a connection.


close

public void close() 
throws IOException 

Closes this socket.

Throw:

IOException

If an I/O occurs error when closing the socket.


getInetAddress

public InetAddress getInetAddress() 

Return Value:

Returns The address to which this socket is connected, or null if the socket is not yet connected.


getLocalPort

public int getLocalPort() 

Return Value:

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 is called to create the actual socket implementation.

ParameterDescription
fac the desired factory

Throw:

SocketException

If the factory has already been defined.

Throw:

IOException

If an I/O error occurs when setting the socket factory.


toString

public String toString() 

Return Value:

Returns a string representation of this socket.

Overrides:

toString in class Object .



Top© 1996 Sun Microsystems, Inc. All rights reserved.