Home > Common Problem > body text

What is the difference between socket communication and tcp communication?

藏色散人
Release: 2022-01-24 14:51:11
Original
38568 people have browsed it

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.

What is the difference between socket communication and tcp communication?

#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 What is the difference between socket communication and tcp communication?

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

The client receives the service After the client's response signal ACK M 1, the client knows: The server knows that my client will no longer send data packets to your server, and you can stop monitoring me.

But at this time, only the one-way flow from the client to the server has stopped, and the flow in the other direction:

The data transmission from the server to the client can still proceed normally until The server sends a FIN N wave signal to the client, and the client responds with an ACK N 1 signal to the server. Only then does the server know: The client knows that my server will no longer send data packets to your client. You can stop treating me. monitored.

At this point, the one-way flow from the client to the server and the one-way flow from the server to the client have stopped, and the connection can be closed.

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.


After being able to uniquely identify processes on the network, they can communicate using sockets. What is a socket?

We often translate socket as socket. Socket is an abstraction layer between the application layer and the transport layer. It abstracts the complex operations of the TCP/IP layer into several simple interfaces for the application layer. Call the implemented process to communicate in the network.

What is the difference between socket communication and tcp communication? Figure 5: Schematic diagram of the socket communication system module

The process of using socket communication between the tcp server and the tcp client is as follows.

As you can see from the picture, the socket connection can maintain a long connection.
What is the difference between socket communication and tcp communication?
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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template