Most visited

Recently visited

Added in API level 1

ServerSocketChannel

public abstract class ServerSocketChannel
extends AbstractSelectableChannel implements NetworkChannel

java.lang.Object
   ↳ java.nio.channels.spi.AbstractInterruptibleChannel
     ↳ java.nio.channels.SelectableChannel
       ↳ java.nio.channels.spi.AbstractSelectableChannel
         ↳ java.nio.channels.ServerSocketChannel


面向流式侦听套接字的可选频道。

通过调用open方法创建服务器套接字通道。 不可能为任意的,预先存在的ServerSocket创建一个频道。 新创建的服务器套接字通道已打开,但尚未绑定。 尝试调用未绑定的服务器套接字通道的accept方法将导致引发NotYetBoundException 可以通过调用bind定义的bind方法之一来绑定服务器套接字通道。

套接字选项使用setOption方法进行配置。 服务器插槽通道支持以下选项:

Option Name 描述
SO_RCVBUF The size of the socket receive buffer
SO_REUSEADDR Re-use address
Additional (implementation specific) options may also be supported.

服务器套接字通道可安全地用于多个并发线程。

Summary

Protected constructors

ServerSocketChannel(SelectorProvider provider)

初始化此类的新实例。

Public methods

abstract SocketChannel accept()

接受对此通道套接字的连接。

abstract ServerSocketChannel bind(SocketAddress local, int backlog)

将通道的套接字绑定到本地地址并配置套接字以侦听连接。

final ServerSocketChannel bind(SocketAddress local)

将通道的套接字绑定到本地地址并配置套接字以侦听连接。

static ServerSocketChannel open()

打开服务器套接字通道。

abstract <T> ServerSocketChannel setOption(SocketOption<T> name, T value)

设置套接字选项的值。

abstract ServerSocket socket()

检索与此通道关联的服务器套接字。

final int validOps()

返回标识此频道支持的操作的操作集。

Inherited methods

From class java.nio.channels.spi.AbstractSelectableChannel
From class java.nio.channels.SelectableChannel
From class java.nio.channels.spi.AbstractInterruptibleChannel
From class java.lang.Object
From interface java.nio.channels.Channel
From interface java.nio.channels.InterruptibleChannel
From interface java.nio.channels.NetworkChannel
From interface java.io.Closeable
From interface java.lang.AutoCloseable

Protected constructors

ServerSocketChannel

Added in API level 1
ServerSocketChannel (SelectorProvider provider)

初始化此类的新实例。

Parameters
provider SelectorProvider

Public methods

accept

Added in API level 1
SocketChannel accept ()

接受对此通道套接字的连接。

如果此通道处于非阻塞模式,那么如果没有未决连接,此方法将立即返回null 否则,它将无限期地阻塞,直到有新的连接可用或发生I / O错误。

无论此通道的阻止模式如何,此方法返回的套接字通道(如果有)都将处于阻止模式。

此方法执行与ServerSocket类的accept方法完全相同的安全检查。 也就是说,如果安全管理器已经安装,那么对于每个新连接,此方法将验证安全管理器的checkAccept方法允许连接的远程端点的地址和端口号。

Returns
SocketChannel The socket channel for the new connection, or null if this channel is in non-blocking mode and no connection is available to be accepted
Throws
ClosedChannelException If this channel is closed
AsynchronousCloseException If another thread closes this channel while the accept operation is in progress
ClosedByInterruptException If another thread interrupts the current thread while the accept operation is in progress, thereby closing the channel and setting the current thread's interrupt status
NotYetBoundException If this channel's socket has not yet been bound
SecurityException If a security manager has been installed and it does not permit access to the remote endpoint of the new connection
IOException If some other I/O error occurs

bind

Added in API level 24
ServerSocketChannel bind (SocketAddress local, 
                int backlog)

将通道的套接字绑定到本地地址并配置套接字以侦听连接。

此方法用于在套接字和本地地址之间建立关联。 一旦建立了关联,套接字就会保持绑定状态,直到通道关闭。

backlog参数是套接字上挂起连接的最大数量。 其确切的语义是特定于实现的。 特别是,实现可能会施加最大长度,或者可能选择忽略参数altogther。 如果backlog参数的值为0或负值,则使用实现特定的默认值。

Parameters
local SocketAddress: The address to bind the socket, or null to bind to an automatically assigned socket address
backlog int: The maximum number of pending connections
Returns
ServerSocketChannel This channel
Throws
AlreadyBoundException If the socket is already bound
UnsupportedAddressTypeException If the type of the given address is not supported
ClosedChannelException If this channel is closed
IOException If some other I/O error occurs
SecurityException If a security manager has been installed and its checkListen method denies the operation

bind

Added in API level 24
ServerSocketChannel bind (SocketAddress local)

将通道的套接字绑定到本地地址并配置套接字以侦听连接。

此方法的调用等效于以下内容:

 bind(local, 0);
 

Parameters
local SocketAddress: The local address to bind the socket, or null to bind to an automatically assigned socket address
Returns
ServerSocketChannel This channel
Throws
AlreadyBoundException
UnresolvedAddressException
ClosedChannelException
IOException
SecurityException If a security manager has been installed and its checkListen method denies the operation

open

Added in API level 1
ServerSocketChannel open ()

打开服务器套接字通道。

新通道是通过调用系统范围的默认 SelectorProvider对象的 openServerSocketChannel方法创建的。

新频道的套接字最初是未绑定的; 在连接可以被接受之前,它必须通过其一个套接字的bind方法绑定到特定地址。

Returns
ServerSocketChannel A new socket channel
Throws
IOException If an I/O error occurs

setOption

Added in API level 24
ServerSocketChannel setOption (SocketOption<T> name, 
                T value)

设置套接字选项的值。

Parameters
name SocketOption: The socket option
value T: The value of the socket option. A value of null may be a valid value for some socket options.
Returns
ServerSocketChannel This channel
Throws
UnsupportedOperationException
IllegalArgumentException
ClosedChannelException
IOException

socket

Added in API level 1
ServerSocket socket ()

检索与此通道关联的服务器套接字。

返回的对象不会声明任何未在 ServerSocket类中声明的公共方法。

Returns
ServerSocket A server socket associated with this channel

validOps

Added in API level 1
int validOps ()

返回标识此频道支持的操作的操作集。

服务器套接字通道仅支持接受新连接,因此此方法返回 OP_ACCEPT

Returns
int The valid-operation set

Hooray!