您的位置:首页 > 其它

SocketChannel 文档中文翻译

2013-04-23 11:16 411 查看
A
SocketChannel
is a selectable channel that provides a partial abstraction of stream connecting socket.
socket()
returns
the related
Socket
instance which can handle the socket.

SocketChannel 是一个可选择的通道,提供部分抽象流连接的套接字。socket() 返回的相关的套接字实例,可处理套接字。

A socket channel is open but not connected when created by open(). After connecting it by calling connect(SocketAddress), it will remain connected until it gets closed. If the connection is non-blocking then connect(SocketAddress)
is used to initiate the connection, followed by a call of finishConnect() to perform the final steps of connecting. isConnectionPending() indicates if the connection is blocked or not; isConnected() indicates if the socket is finally connected or not.

一个套接字通道通过open()方法打开通道,但是此时没有连接。通过调用 connect(SocketAddress)来打开连接,它将保持连接,直到它获取关闭。如果连接处于非阻塞 connect(SocketAddress) 用于启动该连接,然后通过 finishConnect() 的调用来执行连接的最后步骤。isConnectionPending() 指示是否连接被阻止
;isConnected() 指示是否套接字最后连接。

The input and output sides of a channel can be shut down independently and asynchronously without closing the channel. The
shutdownInput
method is used for the input side
of a channel and subsequent read operations return -1, which means end of stream. If another thread is blocked in a read operation when the shutdown occurs, the read will end without effect and return end of stream. The
shutdownOutput
method
is used for the output side of the channel; subsequent write operations throw a
ClosedChannelException
.
If the output is shut down and another thread is blocked in a write operation, an
AsynchronousCloseException
will
be thrown to the pending thread.

通道(channel)的输入和输出双方都可以独立并且异步的关闭,而不用关闭通道(channel).
shutdownInput 方法用于从通道里读取数据,当流到达末尾的时候返回-1.如果另一个线程在读取时被关闭阻塞,那么这个读取操作将在不被影响的情况下结束这个流,
shutdownOutput 方法用于向通道中写入数据,随后的写入操会抛出一个异常:ClosedChannelException,如果这个输出被关闭并且另一个线程在写入操作的时候被阻塞,将会抛出ClosedChannelException.

Socket channels are thread-safe, no more than one thread can read or write at any given time. The
connect(SocketAddress)
and
finishConnect()
methods
are synchronized against each other; when they are processing, calls to
read
and
write
will block.

Socket channels
是线程安全的,在给定的时间里,不会有超过1个线程进行读取或者写入操作,(SocketAddress) and
finishConnect()
是互相同步的,当它们正在运行时,读取和写入操作会被阻止.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: