Detailed summary of TCP / UDP in Java

TCP / UDP: TCP is mainly a connection oriented protocol. It includes the functions of establishing and dismantling connections and ensuring the sequence and correctness of data flow.

Each data operation in the middle of TCP is equivalent to accessing a data stream. Its most typical feature is the connection establishment process of the three handshakes. What the server needs to do is to establish a communication endpoint and wait for the request sent by the client. Typical processing steps are as follows:

1. Build a ServerSocket instance and specify the local port. This socket is used to listen for connection requests on the specified port.

2. Repeat the following steps:

a. Call the accept () method of socket to obtain the connection request of the following client. Through the socket instance returned by the accept () method, a new connection with the client is established.

b. Get the InputStream and OutputStream through the returned socket instance. You can read and write data through these two streams respectively.

c. At the end, call the close () method of the socket instance to close the socket connection.

TCP server side:

TCP client:

1. Build a socket instance and establish a connection through the specified remote server address and port.

2. Read and write data through the InputStream and OutputStream contained in the socket instance.

3. after the operation, call the close method of the socket instance and close it.

UDP (User Datagram Protocol)

There are two typical differences between UDP and TCP. One is that it does not need to establish a connection. The other is that it retains the message boundary every time it sends and receives messages.

Because UDP protocol does not need to establish a connection, its process is as follows:

1. Construct datagramsocket instance and specify local port.

2. Receive datagram packet through the receive method of datagram socket instance The datagram packet contains the content of communication.

3. Receive and send datagram packets through the send and receive methods of datagram socket

The steps of UDP client are also relatively simple, mainly including the following three steps:

1. Construct datagramsocket instance.

2. Send datagram packet message through send and receive methods of datagram socket instance.

After 3., the close method called DatagramSocket is closed.

Because different from TCP, UDP can send messages to different servers at will on the same local port. Generally, it is not necessary to specify the address of the destination server in the constructor of UDP datagram socket.

In addition, another important difference between UDP clients is that after sending echo connection messages, TCP clients will enter the blocking state when calling the read method, while UDP does not. Because UDP can allow message loss. If the message is lost and the process has been blocked or suspended, the process will never be able to go down.

Therefore, a setsotimeout method is generally set to specify how long to give up without receiving a message. You can also specify a number and cycle the specified number of times to read the message. If you read it, you will return, otherwise you will give up.

The above content, friends in need can refer to

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