1. 程式人生 > >[api翻譯]AsynchronousSocketChannel

[api翻譯]AsynchronousSocketChannel


Asynchronous socket channels are created in one of two ways. A newly-created AsynchronousSocketChannel is created by invoking one of the open methods defined by this class. A newly-created channel is open but not yet connected. A connected AsynchronousSocketChannel is created when a connection is made to the socket of an

AsynchronousServerSocketChannel. It is not possible to create an asynchronous socket channel for an arbitrary, pre-existing socket.

非同步套接字管道有兩種建立方式。

呼叫這個類中的open方法建立一個新的AsynchronousSocketChannel。

剛建立的管道是開啟這的,但是還沒有連線。

當有一個連線連線到AsynchronousServerSocketChannel套接字上時,會建立一個連線狀態的AsynchronousSocketChannel。

不能在已經存在的任意的socket上建立非同步套接管道。

A newly-created channel is connected by invoking its connect method; once connected, a channel remains connected until it is closed. Whether or not a socket channel is connected may be determined by invoking its getRemoteAddress method. An attempt to invoke an I/O operation upon an unconnected channel will cause a

NotYetConnectedException to be thrown.

新建立的管道在呼叫它的connect方法時建立連線;一旦建立了連線,管道會維持連線狀態直到關閉。

檢視一個套接字管道是否連線上了,可以呼叫他的getRemoteAddres 方法。

在未連線的管道上呼叫io操作會丟擲一個NotYetConnectedException

Channels of this type are safe for use by multiple concurrent threads. They support concurrent reading and writing, though at most one read operation and one write operation can be outstanding at any time. If a thread initiates a read operation before a previous read operation has completed then a ReadPendingException will be thrown. Similarly, an attempt to initiate a write operation before a previous write has completed will throw a WritePendingException.

Socket options are configured using the setOption method. Asynchronous socket channels support the following options:

這個型別的管道在多執行緒下是安全的。

他們支援併發讀寫,索然任何時候其實至多隻有一個讀操作盒一個寫操作在執行。

如果如果一個執行緒在前一個讀操作完成前,發起讀操作,就會丟擲一個ReadPendingException。

相似的,一個執行緒在前一個寫操作完成前,發起寫操作,會丟擲WritePendingException。

使用setOption方法配置套接字,非同步套接字管道支援如下選項:

Option Name Description
The size of the socket send buffer傳送緩衝區大小
The size of the socket receive buffer接受緩衝區大小
Keep connection alive保持連線
Disable the Nagle algorithm禁用nagle演算法
Additional (implementation specific) options may also be supported.