加载中...
The Wayback Machine - https://sup1a9wrlpyh5li9ro.vcoronado.top/web/20090228220543/http://developer.android.com:80/reference/java/net/ServerSocket.html
public class

ServerSocket

extends Object
java.lang.Object
   ↳ java.net.ServerSocket
Known Direct Subclasses

Class Overview

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.

Summary

Public Constructors
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.
Public Methods
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.
void close()
Close this 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.
synchronized int getSoTimeout()
Answer the time-out period of this server socket.
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.
synchronized void setSoTimeout(int timeout)
Set the listen time-out period for this server socket.
synchronized static void setSocketFactory(SocketImplFactory aFactory)
Set the server socket implementation factory.
String toString()
Returns a string containing a concise, human-readable description of the server socket.
Protected Methods
final void implAccept(Socket aSocket)
Invoke the server socket implementation to accept a connection on the newly created aSocket.
[Expand]
Inherited Methods
From class java.lang.Object

Public Constructors

public ServerSocket ()

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

Throws
IOException
See Also

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
Throws
IOException
See Also

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
Throws
IOException
See Also

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
Throws
IOException
See Also

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.

Returns
  • Socket the 'host' 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
Throws
IllegalArgumentException if the SocketAddress is not supported
IOException if the socket is already bound, or a problem occurs during the bind

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
Throws
IllegalArgumentException if the SocketAddress is not supported
IOException if the socket is already bound, or a problem occurs during the bind

public void close ()

Close this server socket. Any attempt to connect to this socket thereafter will fail.

Throws
IOException

public ServerSocketChannel getChannel ()

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
Throws
SocketException thrown if option cannot be retrieved
IOException

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
Throws
SocketException thrown if an error occurs during setting the option

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
Throws
IOException thrown if the factory is already set

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.

Returns
  • String the description

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
Throws
IOException thrown if connection cannot be accepted