public class ServerSocket 
 extends Object implements Closeable
| java.lang.Object | |
| java.net.ServerSocket | |
|   |  
      
这个类实现服务器套接字。 服务器套接字等待通过网络进入的请求。 它根据该请求执行一些操作,然后可能将结果返回给请求者。
服务器套接字的实际工作由SocketImpl类的实例执行。 应用程序可以更改创建套接字实现的套接字工厂,将其自身配置为创建适合本地防火墙的套接字。
Public constructors |  
      |
|---|---|
  ServerSocket()  创建一个未绑定的服务器套接字。  |  
      |
  ServerSocket(int port)  创建绑定到指定端口的服务器套接字。  |  
      |
  ServerSocket(int port, int backlog)  创建一个服务器套接字并将其绑定到指定的本地端口号,并指定backlog。  |  
      |
  ServerSocket(int port, int backlog, InetAddress bindAddr)  创建一个具有指定端口的服务器,侦听待办事项和本地IP地址以进行绑定。  |  
      |
公共方法(Public methods) |  
      |
|---|---|
 Socket  |  
         accept()  侦听对此套接字的连接并接受它。  |  
      
 void  |  
         bind(SocketAddress endpoint, int backlog)  将   |  
      
 void  |  
         bind(SocketAddress endpoint)  将   |  
      
 void  |  
         close()  关闭此插座。  |  
      
 ServerSocketChannel  |  
         getChannel()  返回与此套接字关联的唯一   |  
      
 InetAddress  |  
         getInetAddress()  返回此服务器套接字的本地地址。  |  
      
 int  |  
         getLocalPort()  返回此套接字正在侦听的端口号。  |  
      
 SocketAddress  |  
         getLocalSocketAddress()  返回此套接字绑定的端点的地址,如果尚未绑定,则   |  
      
 int  |  
         getReceiveBufferSize()  获取此 ServerSocket的SO_RCVBUF选项的值,即将用于从此 ServerSocket接受的套接字的建议缓冲区大小。  |  
      
 boolean  |  
         getReuseAddress()  测试SO_REUSEADDR是否启用。  |  
      
 int  |  
         getSoTimeout()  检索SO_TIMEOUT的设置。  |  
      
 boolean  |  
         isBound()  返回ServerSocket的绑定状态。  |  
      
 boolean  |  
         isClosed()  返回ServerSocket的关闭状态。  |  
      
 void  |  
         setPerformancePreferences(int connectionTime, int latency, int bandwidth)  为此ServerSocket设置性能首选项。  |  
      
 void  |  
         setReceiveBufferSize(int size)  为从此 ServerSocket接受的套接字设置SO_RCVBUF选项的默认建议值。  |  
      
 void  |  
         setReuseAddress(boolean on)  启用/禁用SO_REUSEADDR套接字选项。  |  
      
 void  |  
         setSoTimeout(int timeout)  使用指定的超时启用/禁用SO_TIMEOUT,以毫秒为单位。  |  
      
 static void  |  
         setSocketFactory(SocketImplFactory fac)  为应用程序设置服务器套接字实现工厂。  |  
      
 String  |  
         toString()  以   |  
      
Protected methods |  
      |
|---|---|
 final void  |  
         implAccept(Socket s)  ServerSocket的子类使用此方法来重写accept()以返回它们自己的套接字的子类。  |  
      
继承方法(Inherited methods) |  
      |
|---|---|
  java.lang.Object  
         |  
      |
  java.io.Closeable  
         |  
      |
  java.lang.AutoCloseable  
         |  
      |
ServerSocket ()
创建一个未绑定的服务器套接字。
| 抛出异常(Throws) | |
|---|---|
IOException |  
        IO error when opening the socket. | 
ServerSocket (int port)
创建绑定到指定端口的服务器套接字。 端口号0表示端口号是自动分配的,通常来自临时端口范围。 这个端口号可以通过调用getLocalPort来获取。
传入连接指示(连接请求)的最大队列长度设置为50 。 如果连接指示在队列满时到达,连接将被拒绝。
如果应用程序指定了一个服务器套接字工厂,那么将调用该工厂的createSocketImpl方法来创建实际的套接字实现。 否则,会创建一个“普通”套接字。
如果存在安全管理器,则使用port参数作为其参数调用其checkListen方法,以确保允许操作。 这可能会导致SecurityException。
| 参数(Parameters) | |
|---|---|
port |  
         int: the port number, or 0 to use a port number that is automatically allocated. |  
       
| 抛出异常(Throws) | |
|---|---|
IOException |  
        if an I/O error occurs when opening the socket. | 
SecurityException |  
        if a security manager exists and its checkListen method doesn't allow the operation. |  
       
IllegalArgumentException |  
        if the port parameter is outside the specified range of valid port values, which is between 0 and 65535, inclusive. | 
ServerSocket (int port, 
                int backlog) 
     创建一个服务器套接字并将其绑定到指定的本地端口号,并指定backlog。 端口号0表示端口号是自动分配的,通常来自临时端口范围。 这个端口号可以通过调用getLocalPort来获取。
传入连接指示(连接请求)的最大队列长度设置为backlog参数。 如果连接指示在队列满时到达,连接将被拒绝。
如果应用程序指定了服务器套接字工厂,那么将调用该工厂的createSocketImpl方法来创建实际的套接字实现。 否则,会创建一个“普通”套接字。
如果存在安全管理器,则使用port参数作为其参数调用其checkListen方法,以确保允许操作。 这可能会导致SecurityException。 backlog参数是请求的套接字上挂起连接的最大数量。 其确切的语义是特定于实现的。 特别是,实现可能会施加最大长度,或者可能选择忽略参数altogther。 提供的值应该大于0 。 如果它小于或等于0 ,那么将使用特定于实现的默认值。
| 参数(Parameters) | |
|---|---|
port |  
         int: the port number, or 0 to use a port number that is automatically allocated. |  
       
backlog |  
         int: requested maximum length of the queue of incoming connections. |  
       
| 抛出异常(Throws) | |
|---|---|
IOException |  
        if an I/O error occurs when opening the socket. | 
SecurityException |  
        if a security manager exists and its checkListen method doesn't allow the operation. |  
       
IllegalArgumentException |  
        if the port parameter is outside the specified range of valid port values, which is between 0 and 65535, inclusive. | 
ServerSocket (int port, 
                int backlog, 
                InetAddress bindAddr) 
     创建一个具有指定端口的服务器,侦听待办事项和本地IP地址以进行绑定。 bindAddr参数可用于多宿主主机上的ServerSocket,它只接受连接请求到其中一个地址。 如果bindAddr为空,它将默认接受任何/所有本地地址上的连接。 端口必须介于0和65535之间,包括0和65535。 端口号0表示端口号是自动分配的,通常来自临时端口范围。 此端口号可通过调用getLocalPort进行检索。
如果有安全管理器,则此方法使用port参数作为其参数来调用其checkListen方法,以确保允许操作。 这可能会导致SecurityException。 backlog参数是套接字上请求的最大挂起连接数。 其确切的语义是特定于实现的。 特别是,实现可能会施加最大长度,或者可能选择忽略参数altogther。 提供的值应该大于0 。 如果它小于或等于0 ,那么将使用特定于实现的默认值。
| 参数(Parameters) | |
|---|---|
port |  
         int: the port number, or 0 to use a port number that is automatically allocated. |  
       
backlog |  
         int: requested maximum length of the queue of incoming connections. |  
       
bindAddr |  
         InetAddress: the local InetAddress the server will bind to |  
       
| 抛出异常(Throws) | |
|---|---|
SecurityException |  
        if a security manager exists and its checkListen method doesn't allow the operation. |  
       
IOException |  
        if an I/O error occurs when opening the socket. | 
IllegalArgumentException |  
        if the port parameter is outside the specified range of valid port values, which is between 0 and 65535, inclusive. | 
Socket accept ()
侦听对此套接字的连接并接受它。 该方法会阻塞,直到建立连接。
将创建一个新的套接字s ,如果有安全管理器,则以s.getInetAddress().getHostAddress()和s.getPort()作为参数调用安全管理器的checkAccept方法,以确保允许执行操作。 这可能会导致SecurityException。
| 返回(Returns) | |
|---|---|
Socket |  
        the new Socket | 
| 抛出异常(Throws) | |
|---|---|
IOException |  
        if an I/O error occurs when waiting for a connection. | 
SecurityException |  
        if a security manager exists and its checkAccept method doesn't allow the operation. |  
       
SocketTimeoutException |  
        if a timeout was previously set with setSoTimeout and the timeout has been reached. | 
IllegalBlockingModeException |  
        if this socket has an associated channel, the channel is in non-blocking mode, and there is no connection ready to be accepted | 
也可以看看:
void bind (SocketAddress endpoint, int backlog)
将 ServerSocket绑定到特定地址(IP地址和端口号)。
如果地址是 null ,那么系统将选择一个临时端口和一个有效的本地地址来绑定套接字。
backlog参数是套接字上请求的最大未决连接数。 其确切的语义是特定于实现的。 特别是,实现可能会施加最大长度,或者可能选择忽略参数altogther。 提供的值应该大于0 。 如果它小于或等于0 ,那么将使用特定于实现的默认值。
| 参数(Parameters) | |
|---|---|
endpoint |  
         SocketAddress: The IP address & port number to bind to. |  
       
backlog |  
         int: requested maximum length of the queue of incoming connections. |  
       
| 抛出异常(Throws) | |
|---|---|
IOException |  
        if the bind operation fails, or if the socket is already bound. | 
SecurityException |  
        if a SecurityManager is present and its checkListen method doesn't allow the operation. |  
       
IllegalArgumentException |  
        if endpoint is a SocketAddress subclass not supported by this socket | 
void bind (SocketAddress endpoint)
将 ServerSocket绑定到特定地址(IP地址和端口号)。
如果地址是 null ,那么系统将选择一个临时端口和一个有效的本地地址来绑定套接字。
| 参数(Parameters) | |
|---|---|
endpoint |  
         SocketAddress: The IP address & port number to bind to. |  
       
| 抛出异常(Throws) | |
|---|---|
IOException |  
        if the bind operation fails, or if the socket is already bound. | 
SecurityException |  
        if a SecurityManager is present and its checkListen method doesn't allow the operation. |  
       
IllegalArgumentException |  
        if endpoint is a SocketAddress subclass not supported by this socket | 
void close ()
关闭此插座。 目前被accept()封锁的任何线程都会抛出SocketException 。
如果此套接字具有关联的通道,那么该通道也会关闭。
| 抛出异常(Throws) | |
|---|---|
IOException |  
        if an I/O error occurs when closing the socket. | 
ServerSocketChannel getChannel ()
返回与此套接字关联的唯一 ServerSocketChannel对象(如果有)。
当且仅当通过 ServerSocketChannel.open方法创建通道本身时,服务器套接字才会有通道。
| 返回(Returns) | |
|---|---|
ServerSocketChannel |  
        the server-socket channel associated with this socket, or null if this socket was not created for a channel | 
InetAddress getInetAddress ()
返回此服务器套接字的本地地址。
如果套接字在 closed之前被 closed ,那么在套接字关闭后,此方法将继续返回本地地址。
| 返回(Returns) | |
|---|---|
InetAddress |  
        the address to which this socket is bound, or null if the socket is unbound.  |  
       
int getLocalPort ()
返回此套接字正在侦听的端口号。
如果套接字在 closed之前被 closed ,那么在套接字关闭后,此方法将继续返回端口号。
| 返回(Returns) | |
|---|---|
int |  
        the port number to which this socket is listening or -1 if the socket is not bound yet. | 
SocketAddress getLocalSocketAddress ()
返回此套接字绑定的端点的地址,如果尚未绑定,则 null 。
如果套接字在 closed之前被 closed ,那么在套接字关闭后,此方法将继续返回端点的地址。
| 返回(Returns) | |
|---|---|
SocketAddress |  
        a SocketAddress representing the local endpoint of this socket, or null if it is not bound yet. |  
       
int getReceiveBufferSize ()
获取此 ServerSocket的SO_RCVBUF选项的值,即将用于从此 ServerSocket接受的套接字的建议缓冲区大小。
请注意,接受的套接字中实际设置的值由调用 getReceiveBufferSize()决定。
| 返回(Returns) | |
|---|---|
int |  
        the value of the SO_RCVBUF option for this Socket. | 
| 抛出异常(Throws) | |
|---|---|
SocketException |  
        if there is an error in the underlying protocol, such as a TCP error. | 
也可以看看:
boolean getReuseAddress ()
测试SO_REUSEADDR是否启用。
| 返回(Returns) | |
|---|---|
boolean |  
        a boolean indicating whether or not SO_REUSEADDR is enabled. |  
       
| 抛出异常(Throws) | |
|---|---|
SocketException |  
        if there is an error in the underlying protocol, such as a TCP error. | 
也可以看看:
int getSoTimeout ()
检索SO_TIMEOUT的设置。 0返回意味着该选项被禁用(即,无限超时)。
| 返回(Returns) | |
|---|---|
int |  
        the SO_TIMEOUT value | 
| 抛出异常(Throws) | |
|---|---|
IOException |  
        if an I/O error occurs | 
也可以看看:
boolean isBound ()
返回ServerSocket的绑定状态。
| 返回(Returns) | |
|---|---|
boolean |  
        true if the ServerSocket succesfuly bound to an address | 
boolean isClosed ()
返回ServerSocket的关闭状态。
| 返回(Returns) | |
|---|---|
boolean |  
        true if the socket has been closed | 
void setPerformancePreferences (int connectionTime, 
                int latency, 
                int bandwidth) 
     为此ServerSocket设置性能首选项。
套接字默认使用TCP / IP协议。 某些实现可能提供具有与TCP / IP不同的性能特征的替代协议。 该方法允许应用程序表达自己的偏好,以便在实现从可用协议中选择时如何进行这些折衷。
性能首选项由三个整数描述,其值表示短连接时间,低延迟和高带宽的相对重要性。 整数的绝对值是无关紧要的; 为了选择一个协议,简单地比较这些值,较大的值表示较强的偏好。 例如,如果应用程序比低延迟和高带宽更喜欢短连接时间,则可以使用值(1, 0, 0)来调用此方法。 如果应用程序偏好低延迟以上的高带宽以及短连接时间以上的低延迟,则可以使用值(0, 1, 2)来调用此方法。
在此套接字绑定后调用此方法将不起作用。 这意味着要使用此功能,需要使用无参数构造函数创建套接字。
| 参数(Parameters) | |
|---|---|
connectionTime |  
         int: An int expressing the relative importance of a short connection time |  
       
latency |  
         int: An int expressing the relative importance of low latency |  
       
bandwidth |  
         int: An int expressing the relative importance of high bandwidth |  
       
void setReceiveBufferSize (int size)
为从此ServerSocket接受的套接字设置SO_RCVBUF选项的默认建议值。 实际在接受的套接字中设置的值必须在套接字由accept()返回后调用getReceiveBufferSize()来确定。
SO_RCVBUF的值既用于设置内部套接字接收缓冲区的大小,也用于设置通告给远程节点的TCP接收窗口的大小。
随后可以通过调用setReceiveBufferSize(int)来更改该值。 但是,如果应用程序希望允许一个大于64K字节的接收窗口(如RFC1323所定义的那样),那么建议的值必须在ServerSocket中绑定到本地地址之前设置。 这意味着,必须使用无参数构造函数创建ServerSocket,然后必须调用setReceiveBufferSize(),并最后通过调用bind()将ServerSocket绑定到地址。
如果不这样做不会导致错误,并且缓冲区大小可能会设置为请求的值,但从此ServerSocket接受的套接字中的TCP接收窗口不会超过64K字节。
| 参数(Parameters) | |
|---|---|
size |  
         int: the size to which to set the receive buffer size. This value must be greater than 0. |  
       
| 抛出异常(Throws) | |
|---|---|
SocketException |  
        if there is an error in the underlying protocol, such as a TCP error. | 
IllegalArgumentException |  
        if the value is 0 or is negative. | 
也可以看看:
void setReuseAddress (boolean on)
启用/禁用SO_REUSEADDR套接字选项。
当TCP连接关闭时,连接关闭后连接可能会保持超时状态一段时间(通常称为TIME_WAIT状态或2MSL等待状态)。 对于使用众所周知的套接字地址或端口的应用程序,如果在涉及套接字地址或端口的超时状态中存在连接,则可能无法将套接字绑定到所需的SocketAddress 。
在使用 bind(SocketAddress)绑定套接字之前启用 SO_REUSEADDR允许套接字绑定,即使先前的连接处于超时状态。
当创建ServerSocket时, 未定义初始设置SO_REUSEADDR 。 应用程序可以使用getReuseAddress()确定SO_REUSEADDR初始设置。
未定义绑定套接字后启用或禁用 SO_REUSEADDR时的行为(请参阅 isBound() )。
| 参数(Parameters) | |
|---|---|
on |  
         boolean: whether to enable or disable the socket option |  
       
| 抛出异常(Throws) | |
|---|---|
SocketException |  
        if an error occurs enabling or disabling the SO_RESUEADDR socket option, or the socket is closed. | 
void setSoTimeout (int timeout)
使用指定的超时启用/禁用SO_TIMEOUT,以毫秒为单位。 如果将此选项设置为非零超时,则对此ServerSocket的accept()调用将仅阻塞这段时间。 如果超时过期,则引发java.net.SocketTimeoutException ,尽管ServerSocket仍然有效。 该选项必须在进入阻止操作生效之前启用。 超时值必须大于0.超时值为零将被解释为无限超时。
| 参数(Parameters) | |
|---|---|
timeout |  
         int: the specified timeout, in milliseconds |  
       
| 抛出异常(Throws) | |
|---|---|
SocketException |  
        if there is an error in the underlying protocol, such as a TCP error. | 
也可以看看:
void setSocketFactory (SocketImplFactory fac)
为应用程序设置服务器套接字实现工厂。 工厂只能指定一次。
当应用程序创建一个新的服务器套接字时,会调用套接字实现工厂的 createSocketImpl方法来创建实际的套接字实现。
除非工厂已经设置,否则将 null传递给该方法是无操作的。
如果有安全管理器,则此方法首先调用安全管理器的方法checkSetFactory以确保允许操作。 这可能会导致SecurityException。
| 参数(Parameters) | |
|---|---|
fac |  
         SocketImplFactory: the desired factory. |  
       
| 抛出异常(Throws) | |
|---|---|
IOException |  
        if an I/O error occurs when setting the socket factory. | 
SocketException |  
        if the factory has already been defined. | 
SecurityException |  
        if a security manager exists and its checkSetFactory method doesn't allow the operation. |  
       
String toString ()
以 String返回此套接字的实现地址和实现端口。
| 返回(Returns) | |
|---|---|
String |  
        a string representation of this socket. | 
void implAccept (Socket s)
ServerSocket的子类使用此方法来重写accept()以返回它们自己的套接字的子类。 所以一个FooServerSocket通常会把这个方法一个空的 FooSocket。 从implAccept返回时,FooSocket将连接到客户端。
| 参数(Parameters) | |
|---|---|
s |  
         Socket: the Socket |  
       
| 抛出异常(Throws) | |
|---|---|
IllegalBlockingModeException |  
        if this socket has an associated channel, and the channel is in non-blocking mode | 
IOException |  
        if an I/O error occurs when waiting for a connection. |