Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the differences between socket communication and tcp communication

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

This article introduces the relevant knowledge of "what is the difference between socket communication and tcp communication". In the operation of practical cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

The difference between socket communication and tcp communication: 1. Tcp protocol belongs to transport layer protocol, while socket is an abstract layer between application layer and transport layer; 2. Establishing a TCP connection requires a three-way handshake; 3. Socket connection can maintain a long connection; 4. Tcp server and tcp client use socket communication.

This article operating environment: Windows10 system, Dell G3 computer.

What is the difference between socket communication and tcp communication?

The difference between tcp and socket

One: tcp protocol

The tcp protocol is a transport layer protocol (UDP is also a transport layer protocol, but the UDP protocol is stateless). It takes three handshakes to establish a TCP connection and four waves to disconnect an TCP connection. The mobile phone can use the networking function because the bottom layer of the mobile phone implements the TCP/IP protocol, and the mobile terminal can establish a tcp connection with the server through the wireless network. TCP protocol can provide an interface to the upper layer network, so that the transmission of the upper layer network data is based on the "undifferentiated" network.

Tcp requires a three-way handshake to establish a connection:

Figure 3-way handshake diagram of 1:tcp connection

As shown in the figure:

If the client wants to establish a tcp connection with the server, it first sends a syn J signal to the server. After receiving this signal, the server answers an ACK Junction 1 signal to the client, and adds an additional syn K signal. After receiving the ACK Junction 1 signal, the client knows that the server can receive my signal, and then my client can rest assured to send data to you without worrying that the server will not receive the data I sent.

After receiving the SYN K signal sent by the server, the client also needs to return an ACK K signal to the server, so that the server receives this signal and knows that the signal sent by my server to your client can be received by your client, so that my server can rest assured to send data to your client without worrying that your client will not receive the data it sends.

In fact, you can see from the above description:

Establishing a connection can be divided into four steps like disconnecting a TCP connection, but the response signal ACK of the server and the authentication request signal SYNC of the client are made into one step.

You need to wave four times when the tcp connection is closed:

Figure 4 waves when the 2:tcp connection is disconnected

After the client receives the ACK response signal from the server, the client knows: the server knows that my client will no longer send packets to your server, so you can stop listening to me.

But at this point, only the one-way flow on the client-> server stops, and the flow in the other direction:

The data transmission of the server-> client can still proceed normally, until the server sends the FIN N wave signal to the client, and the client responds to the ACK Number1 signal to the server, the server does not know: the client knows that the server will no longer send packets to your client, so you can stop listening to me.

At this point, both the one-way flow of the client-> server and the one-way flow of the server-> client have stopped, and the connection can be closed.

Two: socket

We know that if two processes need to communicate, the most basic premise is to be able to uniquely mark a process. In local process communication, we can use PID to uniquely identify a process, but PID is only unique locally, and the probability of PID conflict between the two processes in the network is very high. At this time, we need to find another path. We know that the ip address of the IP layer can uniquely identify the host. The TCP layer protocol and port number can uniquely identify a process of the host, so we can use ip address + protocol + port number to uniquely identify a process in the network.

Once the processes in the network can be uniquely identified, they can communicate using socket. What is socket?

We often translate socket into a socket. Socket is an abstract layer between the application layer and the transport layer. It abstracts the complex operations of the TCP/IP layer into several simple interface provisioning layers that call realized processes to communicate in the network.

Figure 5:socket communication system module schematic diagram

The process for tcp server and tcp client to communicate using socket is as follows.

As you can see from the figure, the socket connection can maintain a long connection.

Figure 6: basic socket client / server communication flow diagram

Note that the socket connection can be actively closed by the client or by the server.

This is the end of the content of "what is the difference between socket communication and tcp communication". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report