Build 1.0_r1(from source)

java.net
Class ServerSocket

java.lang.Object
  extended by java.net.ServerSocket
Direct Known Subclasses:
SSLServerSocket

public class ServerSocket
extends Object

ServerSocket create connections between 'host' and 'client' machines. The ServerSocket listens on a well known port and upon a connection request, instantiates a 'host' sockets, which carries on future communication with the requesting 'client' socket, so that the server socket may continue listening for connection requests. They are passive objects, having no execution thread of their own to listen on.


Field Summary
(package private) static SocketImplFactory factory
           
(package private)  SocketImpl impl
           
 
Constructor Summary
  ServerSocket()
          Construct a ServerSocket, which is not bound to any port.
  ServerSocket(int aport)
          Construct a ServerSocket, bound to the nominated port on the default localhost.
  ServerSocket(int aport, int backlog)
          Construct a ServerSocket, bound to the nominated port on the default localhost.
  ServerSocket(int aport, int backlog, InetAddress localAddr)
          Construct a ServerSocket, bound to the nominated local host/port.
protected ServerSocket(SocketImpl impl)
           
 
Method Summary
 Socket accept()
          Retrieve the first connection request and answer the 'host' socket that will conduct further communications with the requesting 'client' socket.
 void bind(SocketAddress localAddr)
          Bind the ServerSocket to the nominated local host/port.
 void bind(SocketAddress localAddr, int backlog)
          Bind the ServerSocket to the nominated local host/port.
(package private)  void checkListen(int aPort)
          Check whether the server may listen for connection requests on aport.
 void close()
          Close this server socket.
(package private) static int defaultBacklog()
          Answer the default number of pending connections on a server socket.
 ServerSocketChannel getChannel()
          if ServerSocket is created by a ServerSocketChannel, returns the related ServerSocketChannel
 InetAddress getInetAddress()
          Answer the local IP address for this server socket.
 int getLocalPort()
          Answer the local port for this server socket.
 SocketAddress getLocalSocketAddress()
          Answer the local SocketAddress for this server socket, or null if the socket is not bound.
 int getReceiveBufferSize()
          Answer the socket receive buffer size (SO_RCVBUF).
 boolean getReuseAddress()
          Get the state of the SO_REUSEADDR socket option.
 int getSoTimeout()
          Answer the time-out period of this server socket.
protected  void implAccept(Socket aSocket)
          Invoke the server socket implementation to accept a connection on the newly created aSocket.
 boolean isBound()
          Return if the server socket is bound to a local address and port.
 boolean isClosed()
          Return if the server socket is closed.
 void setPerformancePreferences(int connectionTime, int latency, int bandwidth)
          sets performance preference for connectionTime,latency and bandwidth
 void setReceiveBufferSize(int size)
          Set the socket receive buffer size.
 void setReuseAddress(boolean reuse)
          Set the SO_REUSEADDR socket option.
static void setSocketFactory(SocketImplFactory aFactory)
          Set the server socket implementation factory.
 void setSoTimeout(int timeout)
          Set the listen time-out period for this server socket.
 String toString()
          Returns a string containing a concise, human-readable description of the server socket.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

impl

SocketImpl impl

factory

static SocketImplFactory factory
Constructor Detail

ServerSocket

public ServerSocket()
             throws IOException
Construct a ServerSocket, which is not bound to any port. The default number of pending connections may be backlogged.

Throws:
IOException
See Also:
Socket

ServerSocket

protected ServerSocket(SocketImpl impl)

ServerSocket

public ServerSocket(int aport)
             throws IOException
Construct a ServerSocket, bound to the nominated port on the default localhost. The default number of pending connections may be backlogged.

Parameters:
aport - the port number to listen for connection requests on
Throws:
IOException
See Also:
Socket

ServerSocket

public ServerSocket(int aport,
                    int backlog)
             throws IOException
Construct a ServerSocket, bound to the nominated port on the default localhost. The number of pending connections that may be backlogged is a specified.

Parameters:
aport - the port number to listen for connection requests on
backlog - the number of pending connection requests, before requests are rejected
Throws:
IOException
See Also:
Socket

ServerSocket

public ServerSocket(int aport,
                    int backlog,
                    InetAddress localAddr)
             throws IOException
Construct a ServerSocket, bound to the nominated local host/port. The number of pending connections that may be backlogged is a specified.

Parameters:
aport - the port number to listen for connection requests on
localAddr - the local machine address to bind on
backlog - the number of pending connection requests, before requests are rejected
Throws:
IOException
See Also:
Socket
Method Detail

accept

public Socket accept()
              throws IOException
Retrieve the first connection request and answer the 'host' socket that will conduct further communications with the requesting 'client' socket.

Returns:
Socket the 'host' socket
Throws:
IOException - if an error occurs while instantiating the 'host' socket

checkListen

void checkListen(int aPort)
Check whether the server may listen for connection requests on aport. Throw an exception if the port is outside the valid range or does not satisfy the security policy.

Parameters:
aPort - the candidate port to listen on

close

public void close()
           throws IOException
Close this server socket. Any attempt to connect to this socket thereafter will fail.

Throws:
IOException

defaultBacklog

static int defaultBacklog()
Answer the default number of pending connections on a server socket.

Returns:
int the default number of pending connection requests

getInetAddress

public InetAddress getInetAddress()
Answer the local IP address for this server socket. Return null if the socket is not bound. This is useful on multihomed hosts.

Returns:
InetAddress the local address

getLocalPort

public int getLocalPort()
Answer the local port for this server socket. Return -1 if the socket is not bound.

Returns:
int the local port the server is listening on

getSoTimeout

public int getSoTimeout()
                 throws IOException
Answer the time-out period of this server socket. This is the time the server will wait listening for connections, before exiting.

Returns:
int the listening timeout
Throws:
SocketException - thrown if option cannot be retrieved
IOException

implAccept

protected final void implAccept(Socket aSocket)
                         throws IOException
Invoke the server socket implementation to accept a connection on the newly created aSocket.

Parameters:
aSocket - the concrete socketImpl to accept the connection request on
Throws:
IOException - thrown if connection cannot be accepted

setSocketFactory

public static void setSocketFactory(SocketImplFactory aFactory)
                             throws IOException
Set the server socket implementation factory. This method may only be invoked with sufficient security and only once during the application lifetime.

Parameters:
aFactory - the streaming socket factory to be used for further socket instantiations
Throws:
IOException - thrown if the factory is already set

setSoTimeout

public void setSoTimeout(int timeout)
                  throws SocketException
Set the listen time-out period for this server socket.

Parameters:
timeout - the time to wait for a connection request
Throws:
SocketException - thrown if an error occurs during setting the option

toString

public String toString()
Returns a string containing a concise, human-readable description of the server socket. The port field is reported a zero, as there is no connection formed to the server.

Overrides:
toString in class Object
Returns:
String the description

bind

public void bind(SocketAddress localAddr)
          throws IOException
Bind the ServerSocket to the nominated local host/port. The default number of pending connections may be backlogged.

Parameters:
localAddr - the local machine address and port to bind on
Throws:
IllegalArgumentException - if the SocketAddress is not supported
IOException - if the socket is already bound, or a problem occurs during the bind

bind

public void bind(SocketAddress localAddr,
                 int backlog)
          throws IOException
Bind the ServerSocket to the nominated local host/port. The number of pending connections that may be backlogged is a specified.

Parameters:
localAddr - the local machine address and port to bind on
backlog - the number of pending connection requests, before requests are rejected
Throws:
IllegalArgumentException - if the SocketAddress is not supported
IOException - if the socket is already bound, or a problem occurs during the bind

getLocalSocketAddress

public SocketAddress getLocalSocketAddress()
Answer the local SocketAddress for this server socket, or null if the socket is not bound. This is useful on multihomed hosts.


isBound

public boolean isBound()
Return if the server socket is bound to a local address and port.


isClosed

public boolean isClosed()
Return if the server socket is closed.


setReuseAddress

public void setReuseAddress(boolean reuse)
                     throws SocketException
Set the SO_REUSEADDR socket option.

Parameters:
reuse - the socket SO_REUSEADDR option setting
Throws:
SocketException

getReuseAddress

public boolean getReuseAddress()
                        throws SocketException
Get the state of the SO_REUSEADDR socket option.

Throws:
SocketException

setReceiveBufferSize

public void setReceiveBufferSize(int size)
                          throws SocketException
Set the socket receive buffer size.

Parameters:
size - the buffer size, in bytes
Throws:
SocketException - If an error occurs while setting the size or the size is invalid.

getReceiveBufferSize

public int getReceiveBufferSize()
                         throws SocketException
Answer the socket receive buffer size (SO_RCVBUF).

Returns:
int socket receive buffer size
Throws:
SocketException

getChannel

public ServerSocketChannel getChannel()
if ServerSocket is created by a ServerSocketChannel, returns the related ServerSocketChannel

Returns:
the related ServerSocketChannel if any

setPerformancePreferences

public void setPerformancePreferences(int connectionTime,
                                      int latency,
                                      int bandwidth)
sets performance preference for connectionTime,latency and bandwidth

Parameters:
connectionTime - the importance of connect time
latency - the importance of latency
bandwidth - the importance of bandwidth

Build 1.0_r1(from source)

Please submit a feedback, bug or feature