public abstract class
ServerSocketChannel
extends AbstractSelectableChannel
java.lang.Object
? java.nio.channels.spi.AbstractInterruptibleChannel
? java.nio.channels.SelectableChannel
? java.nio.channels.spi.AbstractSelectableChannel
? java.nio.channels.ServerSocketChannel
Class Overview
A ServerSocketChannel is a partial abstraction of a selectable, stream-oriented listening socket. Binding and manipulation of socket options can only be done through the associated ServerSocket object, returned by calling socket(). ServerSocketChannels can not be constructed for an already existing server-socket, nor can a SocketImpl be assigned.
一个ServerSocketChannel是一个partial abstraction of a selectable,面向流的监听socket。绑定和操纵socket只能通过ServerSocket对象,由调用socket()方法。
ServerSocketChannels不能由已经创建的server-socket构造,也不能由SocketImpl分配。
A server-socket channel is open but not bound when created by the open() method. Calling accept before bound will cause a NotYetBoundException. It can be bound by calling the bind method of a related ServerSocket instance.
调用open()创建server-socket通道,此时server-socket通道处于打开但是未绑定状态。此时调用accept方法会导致NotYetBoundException异常。
(备注:试图在尚未绑定的服务器套接字通道上调用 I/O 操作时,抛出此未经检查的异常。)使用bind方法可以将server-socket channel绑定到对应的ServerSocket实例。
Summary
Protected Constructors
ServerSocketChannel(SelectorProvider selectorProvider)
Constructs a new ServerSocketChannel.
构造一个新的ServerSocketChannel.
Public Methods
abstract SocketChannel accept()
Accepts a connection to this server-socket channel.
接收一个到该服务端channel的连接。
static ServerSocketChannel open()
Creates an open and unbound server-socket channel.
创建一个打开但是未绑定的服务端channel。
abstract ServerSocket socket()
Return the server-socket assigned this channel, which does not declare any public methods that are not declared in ServerSocket.
返回一个分配给该channel的服务端socket。...
final int validOps()
Gets the valid operations of this channel.
获取该channel的可执行操作。
[Expand]
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.io.Closeable
From interface java.nio.channels.Channel
From interface java.nio.channels.InterruptibleChannel
Protected Constructors
protected ServerSocketChannel (SelectorProvider selectorProvider)
Since: API Level 1
Constructs a new ServerSocketChannel.
Parameters
selectorProvider an instance of SelectorProvider.
一个SelectorProvider实例。该实例由SelectorProvider的一个静态方法提供。
Public Methods
public abstract SocketChannel accept ()
Since: API Level 1
Accepts a connection to this server-socket channel.
This method returns null when this channel is non-blocking and no connection is available, otherwise it blocks until a new connection is available or an I/O error occurs. The socket channel returned by this method will always be in blocking mode.
This method just executes the same security checks as the accept() method of the ServerSocket class.
接收到该channel的连接。当无连接可用且该channel处于非阻塞状态时,该方法返回null;否则channel将一直阻塞直到一个新的可用连接出现或者发生I/O错误。
由该方法返回的socket channel将一直处于阻塞模式。
该方法进行的安全检查和ServerSocket的accept()方法相同。
Returns
the accepted SocketChannel instance, or null if the channel is non-blocking and no connection is available.
接收到的SocketChannel实例,否则:
null channel处于非阻塞模式且无连接可用。
Throws
AsynchronousCloseException if this channel is closed by another thread while this method is in operation.
ClosedByInterruptException if another thread interrupts the calling thread while this operation is in progress. The interrupt state of the calling thread is set and the channel is closed.
ClosedChannelException if this channel is closed.
IOException if another I/O error occurs.
NotYetBoundException if the socket has not yet been bound.
socket尚未绑定。
SecurityException if there is a security manager and it does not permit to access the new connection.
安全管理员不允许接收新连接。
public static ServerSocketChannel open ()
Since: API Level 1
Creates an open and unbound server-socket channel.
This channel is created by calling openServerSocketChannel method of the default SelectorProvider instance.
Returns
创建未绑定服务端channel。
channel通过调用默认的SelectorProvider实例的openServerSocketChannel方法创建。
the new channel which is open but unbound.
Throws
IOException if an I/O error occurs.
public abstract ServerSocket socket ()
Since: API Level 1
Return the server-socket assigned this channel, which does not declare any public methods that are not declared in ServerSocket.
返回分配给该channel的server-socket。...
Returns
the server-socket assigned to this channel.
public final int validOps ()
Since: API Level 1
Gets the valid operations of this channel. Server-socket channels support accepting operation, so this method returns SelectionKey.OP_ACCEPT.
获取可以操作,包括SelectionKey.OP_ACCEPT.
Returns
the operations supported by this channel.
See Also
validOps()
分享到:
相关推荐
Java NIO支持多种类型的通道,包括文件通道(FileChannel)、套接字通道(SocketChannel)和服务器套接字通道(ServerSocketChannel)等。通道可以同时进行读写操作,并且可以实现异步读写。 2. **缓冲区(Buffers...
在Java NIO中,数据是以通道(Channels)和缓冲区(Buffers)的形式进行传输,而不是直接通过流。这种设计使得NIO能够同时处理多个输入/输出操作,从而实现多路复用。 标题“nio.rar_NIO_NIO-socket_java nio_java ...
import java.nio.channels.ServerSocketChannel; import java.nio.channels.SocketChannel; import java.nio.charset.Charset; import java.nio.charset.CharsetDecoder; import java.util.Iterator; /** 测试文件...
关键类包括:`java.nio.channels.Selector`、`java.nio.channels.ServerSocketChannel`、`java.nio.channels.SocketChannel`,以及缓冲区类如`ByteBuffer`。 **3. 埼于Java的AIO(Asynchronous I/O)** AIO,也...
ServerSocketChannel serverSocketChannel = ServerSocketChannel.open(); serverSocketChannel.socket().bind(new InetSocketAddress(8080)); // 设置非阻塞模式 serverSocketChannel.configureBlocking(false); ...
import java.nio.channels.SelectionKey; import java.nio.channels.Selector; import java.nio.channels.ServerSocketChannel; import java.nio.channels.SocketChannel; import java.util.Iterator; public class...
- `java.nio.channels.ServerSocketChannel`:用于服务器端,可以监听新的连接请求。 - `java.nio.channels.Selector`:通过`open()`方法创建,用于注册感兴趣的通道,并监听这些通道上的事件。 - `java.nio....
1. **通道(Channels)**:Java NIO中的通道类似于流,但它们是双向的,可以读写数据。常见的通道类有FileChannel、SocketChannel、ServerSocketChannel和DatagramChannel。 2. **缓冲区(Buffers)**:缓冲区是...
ServerSocketChannel serverSocket = ServerSocketChannel.open(); serverSocket.bind(new InetSocketAddress(8080)); Selector selector = Selector.open(); serverSocket.configureBlocking(false); ...
Java NIO提供了多种类型的通道,如FileChannel、SocketChannel、ServerSocketChannel等,它们分别对应于文件、套接字和服务器套接字操作。 3. **缓冲区(Buffers)**:缓冲区是数据存储的容器,它在读写操作中起到...
ServerSocketChannel server = ServerSocketChannel.open(); server.bind(new InetSocketAddress("localhost", 8080)); Selector selector = Selector.open(); server.configureBlocking(false); server....
ServerSocketChannel serverSocket = ServerSocketChannel.open(); serverSocket.bind(new InetSocketAddress(8000)); Selector selector = Selector.open(); serverSocket.configureBlocking(false); ...
import java.nio.channels.ServerSocketChannel; import java.nio.channels.SocketChannel; import java.util.Iterator; /** * NIO服务端 * @author 小路 */ public class NIOServer { // 通道管理器 private ...
传统的Java I/O基于字节流和字符流,而NIO则提供了通道(Channels)和缓冲区(Buffers)的概念,以及非阻塞I/O操作的能力。本资料"java-nio.rar"主要探讨的是如何使用Java NIO实现异步连接池,这在高并发场景下尤其...
1. **打开通道**:使用`java.nio.channels.FileChannel`、`java.net.SocketChannel`和`java.nio.channels.ServerSocketChannel`等类创建通道。 2. **创建缓冲区**:`java.nio.ByteBuffer`是最常用的缓冲区类型,...
1. 打开`ServerSocketChannel`:通过`java.nio.channels.ServerSocketChannel.open()`静态方法获取一个未绑定的`ServerSocketChannel`实例。 2. 绑定到指定端口:调用`bind(SocketAddress address)`方法将`...
import java.nio.channels.SocketChannel; import java.util.Iterator; import com.nio.user.ClientUser; import com.nio.user.ClientUserManager; import com.nio.user.UserData; public class NIOClient { ...
它们都是`java.nio.channels.Channel`接口的实现类。 2. **缓冲区(Buffer)**:NIO的核心是缓冲区,它提供了一种存储和操作数据的高效方式。缓冲区类型包括ByteBuffer、CharBuffer、IntBuffer、DoubleBuffer等,...
在实际开发中,通常只与 `java.nio`、`java.nio.channels` 和 `java.nio.charset` 三个包打交道,因为它们提供了最常用的功能。 #### 示例:文件复制 以下是一个简单的文件复制示例,使用 NIO 的 `FileChannel`: ...