Index (Frames) | Index (No Frames) | Package | Package Tree | Tree
java.net

Class ServerSocket

java.lang.Object
|
+--java.net.ServerSocket


public class ServerSocket

extends 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.

Authors:

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, java.net.InetAddress bindAddr)

Creates a server socket and binds it to the specified port.

Method Summary

java.net.Socketaccept()

Accepts a new connection and returns a connected Socket instance representing that connection.
voidbind(java.net.SocketAddress endpoint)

Binds the server socket to a specified socket address
voidbind(java.net.SocketAddress endpoint, int backlog)

Binds the server socket to a specified socket address
voidclose()

Closes this socket and stops listening for connections
java.nio.channels.ServerSocketChannelgetChannel()

Returns the unique ServerSocketChannel object associated with this socket, if any.
java.net.InetAddressgetInetAddress()

This method returns the local address to which this socket is bound
intgetLocalPort()

This method returns the local port number to which this socket is bound
java.net.SocketAddressgetLocalSocketAddress()

Returns the local socket address
intgetReceiveBufferSize()

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.
booleangetReuseAddress()

Checks if the SO_REUSEADDR option is enabled
intgetSoTimeout()

Retrieves the current value of the SO_TIMEOUT setting.
voidimplAccept(java.net.Socket s)

This protected method is used to help subclasses override ServerSocket.accept().
booleanisBound()

Returns true then the socket is bound, otherwise false
booleanisClosed()

Returns true if the socket is closed, otherwise false
voidsetReceiveBufferSize(int size)

This method sets the value for the system level socket option SO_RCVBUF to the specified value.
voidsetReuseAddress(boolean on)

Enables/Disables the SO_REUSEADDR option
voidsetSoTimeout(int timeout)

Sets the value of SO_TIMEOUT.
static synchronized voidsetSocketFactory(java.net.SocketImplFactory fac)

Sets the SocketImplFactory for all ServerSocket's.
java.lang.StringtoString()

Returns the value of this socket as a String.

Constructor Details

ServerSocket

public ServerSocket()

Constructor that simply sets the implementation.

Throws:


ServerSocket

public ServerSocket(int port)

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.

Parameters:

Throws:


ServerSocket

public ServerSocket(int port, int backlog)

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.

Parameters:

Throws:


ServerSocket

public ServerSocket(int port, int backlog, java.net.InetAddress bindAddr)

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:Parameters:

Throws:


Method Details

accept

public Socket accept()

Accepts a new connection and returns a connected Socket instance representing that connection. This method will block until a connection is available.

Throws:


bind

public void bind(java.net.SocketAddress endpoint)

Binds the server socket to a specified socket address

Since:Parameters:

Throws:


bind

public void bind(java.net.SocketAddress endpoint, int backlog)

Binds the server socket to a specified socket address

Since:Parameters:

Throws:


close

public void close()

Closes this socket and stops listening for connections

Throws:


getChannel

public 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:

getInetAddress

public InetAddress getInetAddress()

This method returns the local address to which this socket is bound

Returns:


getLocalPort

public int getLocalPort()

This method returns the local port number to which this socket is bound

Returns:


getLocalSocketAddress

public SocketAddress getLocalSocketAddress()

Returns the local socket address

Since:

getReceiveBufferSize

public 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.

Since:Returns:

Throws:


getReuseAddress

public boolean getReuseAddress()

Checks if the SO_REUSEADDR option is enabled

Since:Throws:


getSoTimeout

public int getSoTimeout()

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:Returns:

Throws:


implAccept

protected final void implAccept(java.net.Socket s)

This protected method is used to help subclasses override ServerSocket.accept(). The passed in socket will be connected when this method returns.

Since:Parameters:

Throws:


isBound

public boolean isBound()

Returns true then the socket is bound, otherwise false

Since:

isClosed

public boolean isClosed()

Returns true if the socket is closed, otherwise false

Since:

setReceiveBufferSize

public void setReceiveBufferSize(int size)

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:Parameters:

Throws:


setReuseAddress

public void setReuseAddress(boolean on)

Enables/Disables the SO_REUSEADDR option

Since:Parameters:

Throws:


setSoTimeout

public void setSoTimeout(int timeout)

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:Parameters:

Throws:


setSocketFactory

public static synchronized void setSocketFactory(java.net.SocketImplFactory fac)

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

Parameters:

Throws:


toString

public String toString()

Returns the value of this socket as a String.

Returns: