先介紹java NIO 網絡編程比較重要的四個類
ServerSocketChannel
The ServerSocketChannel class has one purpose: to accept incoming connections. You cannot read from, write to, or connect a ServerSocketChannel. The only operation it supports is accepting a new incoming connection. The class itself only declares four methods, of which accept() is the most important. ServerSocketChannel also inherits several methods from its superclasses, mostly related to registering with a Selector for notification of incoming connections. And finally, like all channels, it has a close() method that shuts down the server socket.
SocketChannel
The SocketChannel class reads from and writes to TCP sockets. The data must be encoded in ByteBuffer objects for reading and writing. Each SocketChannel is associated with a peer Socket object that can be used for advanced configuration, but this requirement can be ignored for applications where the default options are fine.
Selector
@return The number of keys, possibly zero, whose ready-operation sets were updated
The other two select methods are blocking:
public abstract int select() throws IOException
public abstract int select(long timeout) throws IOException
The first method waits until at least one registered channel is ready to be processed before returning. The second waits no longer than timeout milliseconds for a channel to be ready before returning 0. These methods are useful if your program doesn’t have anything to do when no channels are ready to be processed.
SelectionKey
SelectionKey objects serve as pointers to channels. They can also hold an object attachment, which is how you normally store the state for the connection on that channel.
SelectionKey objects are returned by the register() method when registering a channel with a selector. However, you don’t usually need to retain this reference. The
selectedKeys() method returns the same objects again inside a Set. A single channel can be registered with multiple selectors.
總體而言java網絡編程 NIO 封裝了好幾個類,相比於LInux 多路復用編程難度下降了很多。
這里的 Selector 和 Linux select 系統調用功能不一樣,底層實現也不一樣,跟 Linux epoll 有點類似,屬於事件驅動。
ServerSocketChannel 對應於 Linux 的監聽描述符 加 socket
SocketChannel 對應於Linux的連接描述符,可讀可寫
SelectionKey 對應 就緒事件及描述符
先介紹java NIO 網絡編程比較重要的四個類
ServerSocketChannel
The ServerSocketChannel class has one purpose: to accept incoming connections. You cannot read from, write to, or connect a ServerSocketChannel. The only operation it supports is accepting a new incoming connection. The class itself only declares four methods, of which accept() is the most important. ServerSocketChannel also inherits several methods from its superclasses, mostly related to registering with a Selector for notification of incoming connections. And finally, like all channels, it has a close() method that shuts down the server socket.
SocketChannel
The SocketChannel class reads from and writes to TCP sockets. The data must be encoded in ByteBuffer objects for reading and writing. Each SocketChannel is associated with a peer Socket object that can be used for advanced configuration, but this requirement can be ignored for applications where the default options are fine.
Selector
@return The number of keys, possibly zero, whose ready-operation sets were updated
The other two select methods are blocking:
public abstract int select() throws IOException
public abstract int select(long timeout) throws IOException
The first method waits until at least one registered channel is ready to be processed before returning. The second waits no longer than timeout milliseconds for a channel to be ready before returning 0. These methods are useful if your program doesn’t have anything to do when no channels are ready to be processed.
SelectionKey
SelectionKey objects serve as pointers to channels. They can also hold an object attachment, which is how you normally store the state for the connection on that channel.
SelectionKey objects are returned by the register() method when registering a channel with a selector. However, you don’t usually need to retain this reference. The
selectedKeys() method returns the same objects again inside a Set. A single channel can be registered with multiple selectors.
總體而言java網絡編程 NIO 封裝了好幾個類,相比於LInux 多路復用編程難度下降了很多。
這里的 Selector 和 Linux select 系統調用功能不一樣,底層實現也不一樣,跟 Linux epoll 有點類似,屬於事件驅動。
ServerSocketChannel 對應於 Linux 的監聽描述符 加 socket
SocketChannel 對應於Linux的連接描述符,可讀可寫
SelectionKey 對應 就緒事件及描述符