ServerSocket


ServerSocket java.net

Java 1.0

This class is used by servers to listen for connection requests from clients . Before you can use a ServerSocket , it must be bound to the local network address that it is to listen on. All of the ServerSocket( ) constructors except for the no-argument constructor create a server socket and bind it to the specified local port, optionally specifying a "connection backlog" value: this is the number of client connection attempts that may be queued up before subsequent connection attempts are rejected.

In Java 1.4 and later, the no-argument ServerSocket( ) constructor allows you to create an unbound socket. Doing this allows you to bind the socket using the bind( ) method which uses a SocketAddress object rather than a port number. It also allows you to call setReuseAddress( ) , which is only useful when done before the socket is bound. Call isBound( ) to determine whether a server socket has been bound. If it has, use getLocalSocketAddress( ) or getLocalPort( ) and getInetAddress( ) to obtain the local address it is bound to.

Once a ServerSocket has been bound, you can call the accept( ) method to listen on the specified port and block until the client requests a connection on the port. When this happens, accept( ) accepts the connection, creating and returning a Socket the server can use to communicate with the client. A typical server starts a new thread to handle the communication with the client and calls accept( ) again to listen for another connection.

ServerSocket defines several methods for setting socket options that affect the socket's behavior. setSoTimeout( ) specifies the number of milliseconds that accept( ) should block before throwing an InterruptedIOException . A value of 0 means that it should block forever. setReceiveBufferSize( ) is an advanced option that suggests the desired size for the internal receive buffer of the Socket objects returned by accept( ) . This is only a hint, and may be ignored by the system. setReuseAddress( ) is another advanced option; it specifies that a bind( ) operation should succeed even if the local bind address is still nominally in use by a socket that is in the process of shutting down.

Like all sockets, a ServerSocket should be closed with the close( ) method when it is no longer needed. Once closed, a ServerSocket should not be used, except to call the isClosed( ) method which returns TRue if it has been closed.

The getChannel( ) method is a link between this ServerSocket class and the New I/O java.nio.channels.ServerSocketChannel class. It returns the ServerSocketChannel associated with this ServerSocket if there is one. Note, however, that this method always returns null for sockets created with any of the ServerSocket( ) constructors. If you create a ServerSocketChannel object, and obtain a ServerSocket from it, however, then the getChannel( ) method provides a way to link back to the parent channel.

 public class  ServerSocket  {  // Public Constructors   1.4  public  ServerSocket  ( ) throws java.io.IOException;        public  ServerSocket  (int  port  ) throws java.io.IOException;        public  ServerSocket  (int  port  , int  backlog  )  throws java.io.IOException;  1.1  public  ServerSocket  (int  port  , int  backlog  , InetAddress  bindAddr  ) throws java.io.IOException;  // Public Class Methods  public static void  setSocketFactory  (SocketImplFactory  fac  )  throws java.io.IOException;  synchronized   // Public Instance Methods  public Socket  accept  ( ) throws java.io.IOException;  1.4  public void  bind  (SocketAddress  endpoint  ) throws java.io.IOException;  1.4  public void  bind  (SocketAddress  endpoint  , int  backlog  ) throws java.io.IOException;        public void  close  ( ) throws java.io.IOException;  1.4  public java.nio.channels.ServerSocketChannel  getChannel  ( );  constant default:null  public InetAddress  getInetAddress  ( );  default:null  public int  getLocalPort  ( );  default:-1   1.4  public SocketAddress  getLocalSocketAddress  ( );  default:null   1.4  public int  getReceiveBufferSize  ( ) throws SocketException;  synchronized default:43690   1.4  public boolean  getReuseAddress  ( ) throws SocketException;  default:true   1.1  public int  getSoTimeout  ( )  throws java.io.IOException;  synchronized default:0   1.4  public boolean  isBound  ( );  default:false   1.4  public boolean  isClosed  ( );  default:false   5.0  public void  setPerformancePreferences  (int  connectionTime  , int  latency  ,  int  bandwidth  );  empty   1.4  public void  setReceiveBufferSize  (int  size  )  throws SocketException;  synchronized   1.4  public void  setReuseAddress  (boolean  on  ) throws SocketException;  1.1  public void  setSoTimeout  (int  timeout  ) throws SocketException;  synchronized   // Public Methods Overriding Object  public String  toString  ( );  // Protected Instance Methods   1.1  protected final void  implAccept  (Socket  s  ) throws java.io.IOException;   } 

Subclasses

javax.net.ssl.SSLServerSocket

Returned By

java.nio.channels.ServerSocketChannel.socket( ) , javax.net.ServerSocketFactory.createServerSocket( )



Java In A Nutshell
Java In A Nutshell, 5th Edition
ISBN: 0596007736
EAN: 2147483647
Year: 2004
Pages: 1220

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net