ServerSocketChannel
Server Socket Channel role? What do you do full-time?
1. Monitor the new TCP link channel.
2. Create a new Socket Channel
Server Socket Channel does not have any capabilities
Server Socket Channel does not have the ability to transmit data
How to create ServerSocket Channel instance
ServerSocketChannel socketChannel =ServerSocketChannel.open();
This object associates a channel with an unbounded ServerSocket.
Binding listener port number for ServerSocket Channel
Before JDK 1.7, you need to call the socket method of ServerSocketChannel, and then call bind() for association.
After JDK 1.7, you can call the bind() of ServerSocketChannel directly for port binding.
How Server Socket Channel monitors incoming connections
Monitor incoming connections through the ServerSocketChannel.accept() method.
Server Socket Channel defaults to blocking mode, so you can view the JDK source code
As follows:
Setting blocking mode through ServerSocketChannel.configureBlocking(true)
1. In blocking mode,
If a new connection comes in, the accept() method returns a SocketChannel containing the new connection.
If there is no new connection, the accept() method will be blocked until some new connection comes in.
2. ServerSocketChannel. configureBlocking(false) in non-blocking mode
If a connection comes in, the accept() method returns a SocketChannel containing the new connection.
If there is no new connection, the accept() method immediately returns null
3. The fundamental difference between blocking mode and non-blocking mode?
In fact, in the absence of new connections, how to deal with it? If you don't return and wait all the time, you will be blocked.
If it returns immediately, it will not be blocked.
SocketChannel
How to create Socket Channel?
Way 1: Create on the client side
SocketChannel socketChannel = SocketChannel.open();
Mode 2: Create on the server side
Server Socket Channel receives a connection request, such as
SocketChannel socketChannel = serverSocketChannel.accept();
Read the data in the pipeline into the cache using socketChannel's read method
int read = socketChannel.read(sizeBuffer);
Write the cached data into the pipeline using the write method of socketChannel
socketChannel.write(sizeBuffer);
Examples are as follows:
Server:
package xingej.channel; import java.io.IOException; import java.net.InetSocketAddress; import java.nio.ByteBuffer; import java.nio.channels.ServerSocketChannel; import java.nio.channels.SocketChannel; public class ServerSocketChannelTest { public void initChannel() throws IOException { //Server side, create Server Socket Channel through open method //Note that at this point, the server side has not yet bound the port. ServerSocketChannel serverSocketChannel = ServerSocketChannel.open(); //Set to non-blocking mode // serverSocketChannel.configureBlocking(false); //Binding port number //Writing after JDK 1.7 serverSocketChannel.bind(new InetSocketAddress(8081)); //Writing before JDK 1.7 // serverSocketChannel.socket().bind(new InetSocketAddress(8081)); //Create a byte buffer //The size of the buffer is 1024 bytes, which can be debugged by itself, such as 64,128... ByteBuffer byteBuffer = ByteBuffer.allocate(1024); while (true) { System.out.println("-------Server side-----Start receiving-----Client Connection---------"); //On the server side, receive a link from the client and return a link if there is a client. //SocketChannel object //If it's blocking mode, if no new links come in, it's blocking here, otherwise, execute down. //If it is in non-blocking mode, without new links coming in, it will immediately return a null, where the program will not block. //It will go down immediately. SocketChannel socketChannel = serverSocketChannel.accept(); if (null != socketChannel) { while (true) { //Clear cache data, new data can be received byteBuffer.clear(); //Read the data from the pipe socketChannel into the cache byteBuffer //readSize denotes the number of bytes read int readSize = socketChannel.read(byteBuffer); if (readSize == -1) { break; } //Then other business logic operations are performed from the byte cache.\ // Note that the byte type used in the cache here // Therefore, if you need other types, you need to convert System.out.println(new String(byteBuffer.array())); } } try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } public static void main(String[] args) throws IOException { new ServerSocketChannelTest().initChannel(); } }
The client is as follows:
package xingej.channel; import java.io.IOException; import java.net.InetSocketAddress; import java.nio.ByteBuffer; import java.nio.channels.SocketChannel; public class SocketChannelTest { public void connectServer() throws IOException{ // Create a SocketChannel object, // Please note that there is no link to the server side. SocketChannel socketChannel = SocketChannel.open(); //Start linking the server side socketChannel.connect(new InetSocketAddress("localhost", 8081)); //Create a byte buffer on the client side ByteBuffer byteBuffer = ByteBuffer.allocate(1024); String msg = "\nhello, nio, hello ,spark, hello ,hadoop, flume, mesos, marathon, netty, mina, stream, inputstream, outputstream \n" + "hello, nio, hello ,spark, hello ,hadoop, flume, mesos, marathon, netty, mina, stream, inputstream, outputstream \n" + "hello, nio, hello ,spark, hello ,hadoop, flume, mesos, marathon, netty, mina, stream, inputstream, outputstream Beijing\n"; //To the byte buffer, add data byteBuffer.put(msg.getBytes()); // For updating the limit value, the value is updated to position for subsequent reads byteBuffer.flip(); while(byteBuffer.hasRemaining()) { //Write the data in the byte cache into the pipeline socketChannel.write(byteBuffer); } socketChannel.close(); } public static void main(String[] args) throws IOException{ new SocketChannelTest().connectServer(); } }
Start-up mode:
Start the server first, then the client.