Click to enter _ more _java thousand ask
1. What is the difference between Socket and Socketchannel?
Find out what the socket looks for here: what the socket is
Socket, socketchannel both of the essence are the same, are to achieve client-server connection and exist, but in use, but there is a great difference. Specific as follows:
The owning package is different
The socket is in the java.net package, and the Socketchannel is in the Java.nio package.
asynchronous in different ways
From the different packages, we can roughly infer their main difference: the socket is blocking the connection (of course we can implement non-blocking ourselves), Socketchannel can set the non-blocking connection.
When using the ServerSocket socket class, the server socket usually allocates a thread for each client socket, and each thread is likely to be in a long blocking state. Too many threads can also affect server performance (using thread pool optimization, see here: How to write a multithreaded socket program). The use of the Socketchannel, Serversocketchannel class can be non-blocking communication, so that the server side only needs a single thread to handle all client socket requests.
Understanding blocking, non-blocking look here: [What is the difference between blocking and non-blocking][3]
different Performance
Generally speaking, using Socketchannel will have better performance. In fact, the socket should actually be more efficient than socketchannel, but because of the user design and other reasons, efficiency is lower than the direct use of Socketchannel.
different ways to use
Sockets, ServerSocket classes can pass in different parameters to instantiate objects directly and bind IP and ports, such as:
new Socket("127.0.0.1""8000"new ServerSocket("8000");
The Socketchannel and Serversocketchannel classes need to be controlled by selector class, such as:
SelectorselectorSelector.open();ServerSocketChannel serverChannel = ServerSocketChannel.open();serverChannel.configureBlocking(false// 设置为非阻塞方式,如果为true 那么就为传统的阻塞方式serverChannel.socket().bind(new// 绑定IP 及 端口serverChannel.register(selector// 注册 OP_ACCEPT事件new// 开启一个线程 处理所有请求
2. What is the core class of Socketchannel mode
Here are a few of the core classes required by the Socketchannel approach:
Serversocketchannel
An alternative class of serversocket that supports blocking and non-blocking communication.
Socketchannel
An alternative class of sockets that supports blocking and non-blocking communication.
Selector
Receive Client Connection readiness events for Serversocketchannel monitoring and connect server read-ready and write-ready events for socketchannel monitoring.
Selectionkey
Represents the handle to the Selector register event for Serversocketchannel and Socketchannel. When a
When the Selectionkey object is in the Selected-keys collection of the Selector object, it indicates that the event associated with the Selectionkey object has occurred.
There are several static constants in the Selectionkey class:
Selectionkey.op_accept, the Client Connection readiness event, equals the Listener serversocket.accept (), which returns a socket.
Selectionkey.op_connect, ready to connect the server ready, similar to the above, but only for the equivalent of the socket Listener socket.connect ().
Selectionkey.op_read, read Ready event, indicates that the input stream already has readable data and can perform read operations.
A selectionkey.op_write, write-ready event that indicates that a write operation can be performed.
Learn how to develop serversocketchannel see here: [How to write a non-blocking Socketchannel program][4]
Java thousand ask _01 basic concept (013) _socket, Socketchannel what is the difference