This is the 21st day of my participation in the Novembermore Challenge.The final text challenge in 2021

directory

TCP and UDP protocols

Ports and sockets

Hello! Hello, everyone! I’m Grey Ape!

In the Java programming development, the development of network communication is a very important part of, it is the basis of two or more computer networks, network programming is in keeping with the purpose of network communication between other computers, so the Wolf came to Java and share with you today to know some knowledge of network communication.

In fact, in the development process of Java, in order to facilitate network communication, developers have gradually encapsulated some content needed in network programming into different classes. Users only need to create objects of corresponding classes and call corresponding methods when network programming.

TCP and UDP protocols

In the TCP/UDP protocol stack, there are two advanced protocols that we should know when we write network applications, namely transmission control protocol TCP and user datagram protocol UDP.

TCP is a fixed-line based protocol that provides reliable data transmission between two computers. TCP ensures that data is delivered accurately from one end to the other end of the connection and is arranged in the same order as when it is sent out.

Therefore, TCP is suitable for situations with high reliability requirements. For example, when two parties are making a phone call, I have to dial the other party first. After the two parties confirm the connection, they can hear each other and know what the other party is using.

** UDP protocol is connectionless communication protocol, does not guarantee the reliable transmission of data, but can send data to several targets, or receive data from several parks, UDP to send independent packets of data, ** this way is like a Courier to send express to customers, can send many packages to the same person, Each package is independent of each other, and the order of delivery is not important, nor can the Courier receive the package in the same order as the worry package.

Therefore, UDP protocol is suitable for some networks that have low requirements for data accuracy but high requirements for data transmission speed and timeliness, such as common network chat rooms and online movies.

The reason for this is that TCP has extra cost in authentication, which may slow down the transmission speed. UDP does not seriously harm the communication even if a small part of packets are lost or the transmission sequence is different.

This is why TCP is often said to be reliable but not secure, and UDP is often said to be secure but not reliable.

It is important to note that some firewalls and routers are configured to not allow packets to be transmitted. Therefore, if you encounter UDP connection problems, you should first determine whether your network allows UDP

Ports and sockets

When you first hear these two terms, you may not know them very well. Here, for example, a socket is like a power socket, which connects the complex client and server side together.

Generally speaking, for a computer, there is only a single physical connection to the network through which all data is sent to a specific computer both internally and externally. This is the definition of Port, the network programming Port (Port) is not real, he is only an imaginary connection device, the Port is specified as an integer between 0 and 65535. For example, HTTP services generally use port 80 and FTP services use port 21. Therefore, when a computer provides multiple services, clients will use different ports to determine which service on the server to connect to.

For example, ports between 0 and 1023 are generally used for some well-known network services and applications. Common network applications should use ports higher than 1024 to avoid port conflicts with ports used by other applications or system services.

The following figure shows the port usage

The Socket in the network program is used to connect the application program with the port, and the Socket is also a hypothetical connection device. The Socket can be abstracted as a class in JAVA programming. When we develop the program, we only need to create the Socket class object, and we can use the Socket.

Sockets can be represented simply as follows:

Well, that’s all about the basics of network communication, and then the big Bad Wolf will share with you how to write network programs using TCP and UDP and MQTT protocols.

Remember to like your attention if you think it’s useful (^ ~ ^)

The Wolf is looking forward to making progress with you