Principle of realizing native socket communication mechanism in Java

This paper introduces the principle of realizing the native socket communication mechanism in Java and shares it with you as follows:

Current environment

jdk == 1.8

Knowledge points

scene

Today, let's talk about socket communication in Java. Here we take the simplest request response model as an example, assuming that we now need to communicate with Baidu site. How can we implement it with Java Native socket.

Establish socket connection

First, we need to establish a socket connection (core code)

Handle socket I / O stream

After the socket connection is successfully established, we can obtain its input and output stream. The essence of communication is the processing of input and output stream. Through the input stream, read the data uploaded from the network connection, and through the output stream, transfer the local data to the remote end.

Socket connection is actually a little similar to processing file stream. Both are IO operations.

Get the input and output stream codes as follows:

For the processing of IO streams, we usually use the corresponding wrapper classes to process IO streams. If we directly process IO streams, we need to operate on byte [], which is relatively cumbersome. If the wrapper class is used, we can directly process with string, int and other types, simplifying IO byte operation.

The following uses BufferedReader and printwriter as input and output wrapper classes for processing.

Data request and response

With socket connection and IO input / output stream, it is time to send request data to and obtain the response result of the request.

With the support of IO wrapper class, we can directly transmit in string format, and the wrapper class helps us load the data into the corresponding byte stream.

Because we have HTTP access with Baidu site, we don't need to define the output format. Using the standard HTTP transmission format, you can respond to requests (some specific RPC frameworks may have custom communication formats).

The requested data content is processed as follows:

The sending request data code is as follows:

So far, we have finished all the core codes of creating connection, sending request and receiving response under the native socket.

The complete code is as follows:

Next, we instantiate a client to show the results of socket communication.

Result output:

Request model optimization

In this way, although there is no problem in realizing the function. However, when we look closely, we find that IO blocking occurs during IO write and read. Namely:

So if you want to request 10 different sites at the same time, as follows:

It must be the end of the first request response before initiating the next site processing.

This is more obvious on the server side. Although the code here is a client connection, the specific operation is similar to that on the server side. Requests can only be processed serially one by one, which is certainly not up to standard in response time.

Some people think this is not a problem at all. Java is a multithreaded programming language. In this case, a multi-threaded model is most appropriate.

This method seems very useful at first, but with a large amount of concurrency, the application will play a lot of threads. As we all know, on the server, each thread will actually occupy a file handle. The number of handles on the server is limited, and a large number of threads will cause considerable consumption of inter thread switching. Therefore, this method cannot be carried in scenarios with a large amount of concurrency.

Since there are too many threads, let's control the number of threads created. Only a fixed number of threads are started for socket processing, which not only makes use of multi-threaded processing, but also controls the resource consumption of the system.

For the number of threads started, generally, the CPU intensive type is set to N + 1 (n is the number of CPU cores), and the IO intensive type is set to 2n + 1.

This way, it seems to be the best. Is it better if a thread can handle multiple socket connections at the same time and does not block when the input and output data of each socket is not ready. This technology is called "IO multiplexing". In the NiO package of Java, the corresponding implementation is provided.

follow-up

GitHub address: https://github.com/jasonGeng88/blog/tree/master/201708

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>