24. Nio (non-blocking mode)

Nio (non-blocking mode)

The first step: Set the ssc of ServerSocketChannel (server) to non-blocking mode (ssc.configurBlocking(false)) (affecting the following ssc.accept method (establishing a connection) (non-blocking, (no data received) thread continues The operation will not change from the running state to the blocking state. (still running state)), no data is received here, and a null value is returned)

 

Comment out the following first, and then run the server directly (that is, there is no client to establish a connection)

 

The second step: (set the sc of socketChannel (the channel used to establish a connection with the client) to non-blocking) the impact is the following received data (here we run the server, and then run the client (only the client has a connection) Then you can enter the following method of reading data (we added an if judgment)))

 

Guess you like

Origin blog.csdn.net/logtcm4/article/details/127804026