Socket/ServerSocket Channel of JAVA-7NIO, niosocketchannel
I. ServerSocketChannel
ServerSocketChannel in Java NIO is a channel that can listen to new TCP connections, just like ServerSocket in standard IO. The ServerSocketChannel class is in the java. nio. channels package.
Open ServerSocketChannel
Open the ServerSocketChannel by calling the ServerSocketChannel. open () method.
Disable ServerSocketChannel
Disable ServerSocketChannel by calling the ServerSocketChannel. close () method.
Listen for new connections
Use the ServerSocketChannel. accept () method to listen for new connections. When the accept () method returns, it returns a SocketChannel containing the new connection. Therefore, the accept () method is blocked until a new connection arrives.
Generally, the accept () method is called in the while LOOP instead of listening to only one connection.
Of course, you can also use exit criteria other than true in the while loop.
Non-Blocking Mode
ServerSocketChannel can be set to non-blocking mode. In non-blocking mode, the accept () method returns immediately. If no new connection is available, null is returned. Therefore, check whether the returned SocketChannel is null. For example:
/*** Socket server channel */@ Test public void text2 () throws IOException {ServerSocketChannel channel = ServerSocketChannel. open (); // create a channel. socket (). bind (new InetSocketAddress (9999); // listener port channel. configureBlocking (true); // set blocking while (true) {SocketChannel accept = channel. accept (); // if it is set to blocking, this method is blocked until there is a connection. // if it is set to non-blocking, You need to judge here accept = null? ByteBuffer byteBuffer = ByteBuffer. allocate (1, 1024); accept. read (byteBuffer); byteBuffer. flip (); // reverse while (byteBuffer. hasRemaining () {// judge System. err. println (char) byteBuffer. get (); // output }}}
Ii. SocketChannel
SocketChannel in Java NIO is a channel connected to a TCP network socket. You can create a SocketChannel in either of the following ways:
Open SocketChannel
The following describes how to open the SocketChannel:
Disable SocketChannel
After using SocketChannel, call SocketChannel. close () to disable SocketChannel:
Read data from SocketChannel
To read data from SocketChannel, call one of the read () methods.
First, allocate a Buffer. The data read from SocketChannel will be placed in this Buffer.
Then, call SocketChannel. read (). This method reads data from SocketChannel to Buffer. The int value returned by the read () method indicates the number of bytes read into the Buffer. If-1 is returned, it indicates that the stream has been read to the end (the connection is closed ).
Write SocketChannel
The SocketChannel. write () method is used to write data to SocketChannel. This method uses a Buffer as a parameter.
Note that the call of the SocketChannel. write () method is in a while loop. The Write () method cannot guarantee how many bytes can be written to the SocketChannel. Therefore, we call write () repeatedly until the Buffer has no bytes to write.
Non-Blocking Mode
You can set SocketChannel to non-blocking mode. After setting it, you can call connect (), read () and write () in asynchronous mode.
Connect ()
If SocketChannel is in non-blocking mode and connect () is called, this method may be returned before the connection is established. To determine whether a connection is established, you can call the finishConnect () method.
Write ()
In non-blocking mode, the write () method may return if no content has been written. Therefore, you need to call write () in the loop (). The previous example already exists. I will not go into details here.
Read ()
In non-blocking mode, the read () method may return if no data has been read. So you need to pay attention to its int return value, which will tell you how many bytes are read.
Non-blocking mode and Selector
The non-blocking mode works better with the Selector. By registering one or more socketchannels with the Selector, you can ask which channel of the Selector is ready for reading and writing. The use of Selector and SocketChannel will be detailed later.
/*** Socket channel */@ Test public void test3 () throws IOException {SocketChannel channel = SocketChannel. open (); // create a server channel. connect (new InetSocketAddress ("127.0.0.1", 9999); // connect to the server address ByteBuffer byteBuffer = ByteBuffer. allocate (1024); // buffer byteBuffer. put ("123 ". getBytes (); byteBuffer. flip (); // reverse while (byteBuffer. hasRemaining () {// judge the channel. write (byteBuffer );}}