Summary of network communication (TCP communication, UDP communication, multicast and NiO) of java basic knowledge

Summary of network communication based on Java

In this article, we mainly discuss how to use java to realize network communication, including TCP communication, UDP communication, multicast and NiO.

TCP connection

Socket is the basis of TCP. In TCP connection, we will use ServerSocket and socket. After the client and server establish a connection, the rest is basically I / O control.

Let's first look at a simple TCP communication, which is divided into client and server.

The client code is as follows:

The server code is as follows:

The function of the server here is very simple. It receives the message sent by the client, and then returns the message "intact" to the client. When the client sends "end", the communication ends.

The above code basically outlines the main framework of the client and server in the TCP communication process. We can find that in the above code, the server can only process one request from the client at any time. It is processed serially and cannot be parallel, which is different from the server processing method in our impression, We can add multiple threads to the server. When a client's request enters, we create a thread to process the corresponding request.

The improved server code is as follows:

The modified server can process multiple requests from the client at the same time.

In the process of programming, we will have the concept of "resource". For example, database connection is a typical resource. In order to improve performance, we usually do not directly destroy database connections, but use database connection pool to manage multiple database connections, which has achieved the purpose of reuse. For socket connections, it is also a resource. When our program needs a large number of socket connections, if each connection needs to be re established, it will be a very inefficient practice.

Similar to the database connection pool, we can also design a TCP connection pool. The idea here is that we use an array to maintain multiple socket connections, and another status array to describe whether each socket connection is in use. When the program needs a socket connection, we traverse the status array and take out the first unused socket connection. If all connections are in use, Throw an exception. This is a very intuitive and simple "scheduling strategy". In many open source or commercial frameworks (APACHE / Tomcat), there will be similar "resource pools".

The code of TCP connection pool is as follows:

UDP connection

UDP is a connection mode different from TCP. It is usually used in occasions with high real-time requirements and low requirements for alignment determination, such as online video. UDP will have "packet loss". In TCP, if the server is not started, the client will report an exception when sending a message, but for UDP, no exception will be generated.

The two classes used for UDP communication are datagram socket and datagram packet, which store the communication content.

The following is a simple UDP communication example. Like TCP, it is also divided into client and server. The client code is as follows:

The server side code is as follows:

Here, we also assume that, like TCP, when the client sends an "end" message, the communication is considered to be over, but in fact, this design is not necessary. The client can be disconnected at any time and does not need to care about the server state.

Multicast

Multicast adopts a method similar to UDP. It uses class D IP address and standard UDP port number. Class D IP address refers to 224.0 0.0 to 239.255 Addresses between 255.255, excluding 224.0 0.0。

The class used by multicast is multicastsocket, which has two methods to pay attention to: joingroup and leavegroup.

The following is an example of multicast. The client code is as follows:

The server code is as follows:

  NIO(New IO)

NiO is jdk1 4, which has a new design in buffer management, network communication, file access and character set operation. NiO uses the concepts of buffer and channel for network communication.

The following is an example of NiO, which is very different from the code style mentioned above.

The above code will try to access a local web address and print its contents.

Thank you for reading, hope to help you, thank you for your support to this site!

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
分享
二维码
< <上一篇
下一篇>>