java.net.ServerSocket
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.
Known Direct Subclasses
Summary
Public Constructors
Protected Constructors
Public Methods
Protected Methods
clone,
equals,
finalize,
getClass,
hashCode,
notify,
notifyAll,
toString,
wait,
wait,
wait
Details
Public Constructors
public
ServerSocket()
Construct a ServerSocket, which is not bound to any port. The default
number of pending connections may be backlogged.
public
ServerSocket(int aport)
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 |
public
ServerSocket(int aport, int backlog)
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 |
public
ServerSocket(int aport, int backlog, InetAddress localAddr)
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 |
backlog
| the number of pending connection requests, before requests are
rejected |
localAddr
| the local machine address to bind on |
Protected Constructors
Public Methods
public
Socket
accept()
Retrieve the first connection request and answer the 'host' socket that
will conduct further communications with the requesting 'client' socket.
Throws
IOException
| if an error occurs while instantiating the 'host' socket
|
public
void
bind(SocketAddress localAddr)
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 |
public
void
bind(SocketAddress localAddr, int backlog)
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 |
public
void
close()
Close this server socket. Any attempt to connect to this socket
thereafter will fail.
if ServerSocket is created by a ServerSocketChannel, returns the related
ServerSocketChannel
Returns
- the related ServerSocketChannel if any
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
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
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.
public
int
getReceiveBufferSize()
Answer the socket receive buffer size (SO_RCVBUF).
Returns
- int socket receive buffer size
public
boolean
getReuseAddress()
Get the state of the SO_REUSEADDR socket option.
public
synchronized
int
getSoTimeout()
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
public
boolean
isBound()
Return if the server socket is bound to a local address and port.
public
boolean
isClosed()
Return if the server socket is closed.
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
|
public
void
setReceiveBufferSize(int size)
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.
|
public
void
setReuseAddress(boolean reuse)
Set the SO_REUSEADDR socket option.
Parameters
reuse
| the socket SO_REUSEADDR option setting
|
public
synchronized
void
setSoTimeout(int timeout)
Set the listen time-out period for this server socket.
Parameters
timeout
| the time to wait for a connection request |
public
static
synchronized
void
setSocketFactory(SocketImplFactory aFactory)
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 |
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.
Protected Methods
protected
final
void
implAccept(Socket aSocket)
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 |