The difference between socket communication and tcp communication: 1. The tcp protocol belongs to the transport layer protocol, while socket is an abstraction layer between the application layer and the transport layer; 2. A three-way handshake is required to establish a TCP connection; 3. Socket connections can maintain long connections; 4. TCP server and TCP client use socket communication, etc.
#The operating environment of this article: Windows 10 system, Dell G3 computer.
What is the difference between socket communication and tcp communication?
The difference between tcp and socket
## 1: tcp protocol
The tcp protocol belongs to the transport layer protocol (UDP also belongs to the transport layer protocol, but the UDP protocol is stateless). Establishing a TCP connection requires three handshakes, and disconnecting a TCP connection requires four waves. The mobile phone can use the networking function because the bottom layer of the mobile phone implements the TCP/IP protocol. You can use the mobile phone terminal to establish a TCP connection with the server through the wireless network. The TCP protocol can provide an interface to the upper-layer network, so that the transmission of upper-layer network data is established on an "undifferentiated" network. tcp requires three handshakes to establish a connection:Figure 1: Tcp connection three-way handshake diagram
As shown in the figure: The client needs To establish a tcp connection with the server, first send a syn J signal to the server. After receiving this signal, the server responds to the client with an ACK J 1 signal and an additional syn K signal. The client receives ACK J 1 After receiving the signal, I will know that the server can receive my signal, and then my client can safely send data to your server without worrying that your server cannot receive the data I sent. After the client receives the SYN K signal sent by the server, it also needs to return an ACK K 1 signal to the server. In this way, the server will know when it receives this signal that the server sent it to your client. signal, your client can receive it, so that my server can safely send data to your client without worrying that your client cannot receive the data it sent. In fact, we can see from the above description: Establishing a connection can be done in four steps just like disconnecting the TCP connection. It just combines the server's response signal ACK and the client's authentication request. The two steps of signal SYNC are completed in one step. Four waves are required when the tcp connection is closed: Figure 2: Four wave waves when the tcp connection is disconnected
2: socket
We know that if two processes need to communicate, the most basic premise is to be able to uniquely identify a process. In local process communication, we can PID is used to uniquely identify a process, but PID is only unique locally. There is a high probability of PID conflict between two processes in the network. At this time, we need to find another way. We know that the IP address of the IP layer can uniquely identify the host, and The TCP layer protocol and port number can uniquely identify a process on the host, so we can use the IP address + protocol + port number to uniquely identify a process on the network. Figure 5: Schematic diagram of the socket communication system module
As you can see from the picture, the socket connection can maintain a long connection.
Figure 6: Basic socket client/server communication process diagram
Note that the socket connection can be actively closed by the client or the server.
For more related knowledge, please visit the FAQ column!
The above is the detailed content of What is the difference between socket communication and tcp communication?. For more information, please follow other related articles on the PHP Chinese website!