Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

java.net
Class ServerSocket  view ServerSocket download ServerSocket.java

java.lang.Object
  extended byjava.net.ServerSocket

public class ServerSocket
extends java.lang.Object

This class models server side sockets. The basic model is that the server socket is created and bound to some well known port. It then listens for and accepts connections. At that point the client and server sockets are ready to communicate with one another utilizing whatever application layer protocol they desire. As with the Socket class, most instance methods of this class simply redirect their calls to an implementation class.


Field Summary
private static SocketImplFactory factory
          This is the user defined SocketImplFactory, if one is supplied
private  SocketImpl impl
          This is the SocketImp object to which most instance methods in this class are redirected
private  InetSocketAddress local
          We need to retain the local address even after the socket is closed.
 
Constructor Summary
  ServerSocket()
          Constructor that simply sets the implementation.
  ServerSocket(int port)
          Creates a server socket and binds it to the specified port.
  ServerSocket(int port, int backlog)
          Creates a server socket and binds it to the specified port.
  ServerSocket(int port, int backlog, InetAddress bindAddr)
          Creates a server socket and binds it to the specified port.
(package private) ServerSocket(SocketImpl impl)
           
 
Method Summary
 Socket accept()
          Accepts a new connection and returns a connected Socket instance representing that connection.
 void bind(SocketAddress endpoint)
          Binds the server socket to a specified socket address
 void bind(SocketAddress endpoint, int backlog)
          Binds the server socket to a specified socket address
 void close()
          Closes this socket and stops listening for connections
 java.nio.channels.ServerSocketChannel getChannel()
          Returns the unique ServerSocketChannel object associated with this socket, if any.
(package private)  SocketImpl getImpl()
           
 InetAddress getInetAddress()
          This method returns the local address to which this socket is bound
 int getLocalPort()
          This method returns the local port number to which this socket is bound
 SocketAddress getLocalSocketAddress()
          Returns the local socket address
 int getReceiveBufferSize()
          This method returns the value of the system level socket option SO_RCVBUF, which is used by the operating system to tune buffer sizes for data transfers.
 boolean getReuseAddress()
          Checks if the SO_REUSEADDR option is enabled
 int getSoTimeout()
          Retrieves the current value of the SO_TIMEOUT setting.
protected  void implAccept(Socket socket)
          This protected method is used to help subclasses override ServerSocket.accept().
 boolean isBound()
          Returns true when the socket is bound, otherwise false
 boolean isClosed()
          Returns true if the socket is closed, otherwise false
 void setReceiveBufferSize(int size)
          This method sets the value for the system level socket option SO_RCVBUF to the specified value.
 void setReuseAddress(boolean on)
          Enables/Disables the SO_REUSEADDR option
static void setSocketFactory(SocketImplFactory fac)
          Sets the SocketImplFactory for all ServerSocket's.
 void setSoTimeout(int timeout)
          Sets the value of SO_TIMEOUT.
 java.lang.String toString()
          Returns the value of this socket as a String.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

factory

private static SocketImplFactory factory
This is the user defined SocketImplFactory, if one is supplied


impl

private SocketImpl impl
This is the SocketImp object to which most instance methods in this class are redirected


local

private InetSocketAddress local
We need to retain the local address even after the socket is closed.

Constructor Detail

ServerSocket

ServerSocket(SocketImpl impl)
       throws java.io.IOException

ServerSocket

public ServerSocket()
             throws java.io.IOException
Constructor that simply sets the implementation.


ServerSocket

public ServerSocket(int port)
             throws java.io.IOException
Creates a server socket and binds it to the specified port. If the port number is 0, a random free port will be chosen. The pending connection queue on this socket will be set to 50.


ServerSocket

public ServerSocket(int port,
                    int backlog)
             throws java.io.IOException
Creates a server socket and binds it to the specified port. If the port number is 0, a random free port will be chosen. The pending connection queue on this socket will be set to the value passed as arg2.


ServerSocket

public ServerSocket(int port,
                    int backlog,
                    InetAddress bindAddr)
             throws java.io.IOException
Creates a server socket and binds it to the specified port. If the port number is 0, a random free port will be chosen. The pending connection queue on this socket will be set to the value passed as backlog. The third argument specifies a particular local address to bind t or null to bind to all local address.

Since:
1.1
Method Detail

getImpl

SocketImpl getImpl()

bind

public void bind(SocketAddress endpoint)
          throws java.io.IOException
Binds the server socket to a specified socket address

Since:
1.4

bind

public void bind(SocketAddress endpoint,
                 int backlog)
          throws java.io.IOException
Binds the server socket to a specified socket address

Since:
1.4

getInetAddress

public InetAddress getInetAddress()
This method returns the local address to which this socket is bound


getLocalPort

public int getLocalPort()
This method returns the local port number to which this socket is bound


getLocalSocketAddress

public SocketAddress getLocalSocketAddress()
Returns the local socket address

Since:
1.4

accept

public Socket accept()
              throws java.io.IOException
Accepts a new connection and returns a connected Socket instance representing that connection. This method will block until a connection is available.


implAccept

protected final void implAccept(Socket socket)
                         throws java.io.IOException
This protected method is used to help subclasses override ServerSocket.accept(). The passed in socket will be connected when this method returns.

Since:
1.1

close

public void close()
           throws java.io.IOException
Closes this socket and stops listening for connections


getChannel

public java.nio.channels.ServerSocketChannel getChannel()
Returns the unique ServerSocketChannel object associated with this socket, if any.

The socket only has a ServerSocketChannel if its created by ServerSocketChannel.open().

Since:
1.4

isBound

public boolean isBound()
Returns true when the socket is bound, otherwise false

Since:
1.4

isClosed

public boolean isClosed()
Returns true if the socket is closed, otherwise false

Since:
1.4

setSoTimeout

public void setSoTimeout(int timeout)
                  throws SocketException
Sets the value of SO_TIMEOUT. A value of 0 implies that SO_TIMEOUT is disabled (ie, operations never time out). This is the number of milliseconds a socket operation can block before an InterruptedIOException is thrown.

Since:
1.1

getSoTimeout

public int getSoTimeout()
                 throws java.io.IOException
Retrieves the current value of the SO_TIMEOUT setting. A value of 0 implies that SO_TIMEOUT is disabled (ie, operations never time out). This is the number of milliseconds a socket operation can block before an InterruptedIOException is thrown.

Since:
1.1

setReuseAddress

public void setReuseAddress(boolean on)
                     throws SocketException
Enables/Disables the SO_REUSEADDR option

Since:
1.4

getReuseAddress

public boolean getReuseAddress()
                        throws SocketException
Checks if the SO_REUSEADDR option is enabled

Since:
1.4

setReceiveBufferSize

public void setReceiveBufferSize(int size)
                          throws SocketException
This method sets the value for the system level socket option SO_RCVBUF to the specified value. Note that valid values for this option are specific to a given operating system.

Since:
1.4

getReceiveBufferSize

public int getReceiveBufferSize()
                         throws SocketException
This method returns the value of the system level socket option SO_RCVBUF, which is used by the operating system to tune buffer sizes for data transfers.

Since:
1.4

toString

public java.lang.String toString()
Returns the value of this socket as a String.


setSocketFactory

public static void setSocketFactory(SocketImplFactory fac)
                             throws java.io.IOException
Sets the SocketImplFactory for all ServerSocket's. This may only be done once per virtual machine. Subsequent attempts will generate an exception. Note that a SecurityManager check is made prior to setting the factory. If insufficient privileges exist to set the factory, an exception will be thrown