NIO: stream (TCP) channel details

Source: Internet
Author: User
Tags set socket
The stream channel has two variants: SocketChannel and ServerSocketChannel. Like its corresponding Socket, SocketChannel is a channel for communication between interconnected terminals. SocketChannel: Create, connect, and close staticSocketChannelopen (SocketAdd... the stream channel has two variants: SocketChannel and ServerSocketChannel. Like its corresponding Socket, SocketChannel is a channel for communication between interconnected terminals. SocketChannel: Create, connect, and close static SocketChannel open (SocketAddress remote) static SocketChannel open () boolean connect (SocketAddress remote) boolean isConnected () void close () boolean isOpen () socket socket () can call the static factory method open () of SocketChannel to create an instance. The first form of the open () method uses SocketAddress (See Chapter 2nd) as the parameter to return a etchannel instance connected to the specified server. Note that this method may be blocked indefinitely. The parameter-free form of open () is used to create an unconnected SocketChannel instance. The instance can be connected to the specified terminal by calling the connect () method. After using the SocketChannel, you need to call the close () method to close it. It is very important that each SocketChannel instance "wraps" A Basic Java Socket and can be accessed through the socket () method. This allows you to bind and set Socket options through the Basic Socket method. For an example of creating, connecting, and disabling a SocketChannel, see TCPEchoClientNonblocking. java (pp. 113rd-114 ). After creating and connecting SocketChannel, you can call the read/write method of this channel for I/O operations. SocketChannel: read and write int read (ByteBuffer dst) long read (ByteBuffer [] dsts) long read (ByteBuffer [] dsts, int offset, int length) int write (ByteBuffer src) long write (ByteBuffer [] srcs) long write (ByteBuffer [] srcs, int offset, int length) the most basic form of a read operation is a ByteBuffer parameter, and fill in the read data in all the remaining bytes of the buffer. In another form, multiple ByteBuffer parameters (ByteBuffer array) are used, and the read data is filled in the remaining byte space of each buffer according to the order in the array. This method is called scattering read because it disperses the read bytes into multiple buffers. Note that scattering read does not necessarily fill all the buffers. The total size of these buffers is only an upper limit. The most basic form of write operations is a ByteBuffer parameter, and attempts to write the remaining bytes in the buffer to the channel. Another form uses a ByteBuffer array as a parameter and tries to write the remaining bytes in all the buffers to the channel. This method is called aggregate writing because it aggregates the bytes in multiple buffers and sends them together. For examples of read/write operations, see TCPEchoClientNonblocking. java and TCPServerSelector. java. Like the corresponding ServerSocket, The ServerSocketChannel is used to listen on the channel connected to the client. ServerSocketChannel: Create, accept, and close the static ServerSocketChannel open () ServerSocket socket () SocketChannel accept () void close () boolean isOpen () call the static factory method open () you can create a ServerSocketChannel instance. Each instance contains a ServerSocket instance and can be accessed through the socket () method. As shown in the preceding example, you must access the underlying ServerSocket instance to bind a specified port and set socket options. After creating a channel instance and binding a port, you can call the accept () method to prepare to receive client connection requests. If the connection is successful, a new connected SocketChannel is returned. After using the ServerSocketChannel, you need to call the close () method to close it. For the example of using ServerSocket, see TCPServerSelector. java (page 116th-117 ). As mentioned above, blocking channels have almost no advantages in addition to being able (required) to be used together with the Buffer. Therefore, you may always need to set the channel to a non-blocking type. SocketChannel and Server SocketChannel: sets the blocking behavior SelectableChannel configureBlocking (boolean block) boolean isBlocking (). You can set SocketChannel or ServerSocketChannel to non-blocking mode by calling configureBlocking (false. The configureBlocking () method returns a SelectableChannel, which is the parent class of SocketChannel and ServerSocketChannel. Consider setting connections for SocketChannel. If the factory method open () passed to SocketChannel is a remote address, the call to this method will block the wait until the connection is established successfully. To avoid this problem, you can use the non-blocking mode of the open () method, configure the channel, and then call the connect () method to specify the remote terminal address. If the connection has been established without blocking, the connect () method returns true; otherwise, you need to check whether the socket is successfully connected. SocketChannel: Test connectivity boolean finishConnect () boolean isConnected () boolean isConnectionPending () for non-blocking SocketChannel, once a connection has been initiated, the underlying socket may be not connected or not connected, the connection is "in progress ". Due to the working mechanism of the underlying protocol (see Chapter 6th), the socket may remain in this state. The finishConnect () method can be used to check the status of the connection attempted on the non-blocking Socket. It can also be used to block the wait during the process of blocking the socket connection until the connection is established successfully. For example, you may need to configure the channel to non-blocking mode and initiate a connection through the connect () method. After some other work is done, configure the channel to blocking mode and then call finishConnect () method. Alternatively, you can keep the channel in non-blocking mode and call the finishConnect () method repeatedly, as shown in TCPEchoClientNonblocking. java. IsConnected () is used to check whether the socket has established a connection. This prevents NotYetConnectedException from being thrown during other operations (for example, when read () or write () is called ). You can also use the isConnectionPending () method to check whether a connection is initiated on the channel. It is necessary to know whether a connection is initiated, because if not, the finishConnect () method will throw a NoConnectionPendingException.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.