Socket principle explanation

Quoting the principle
of socket You are familiar with the words TCP/IP, UDP, and Socket programming, right? With the development of network technology, these words flood our ears. Then I want to ask:

1. What is TCP/IP and UDP?
2. Where is Socket?
3. What is Socket?
4. Will you use them?

What are TCP/IP and UDP?
TCP/IP (Transmission Control Protocol/Internet Protocol) is the transmission control protocol/internet protocol. It is an industry standard protocol set, which is designed for wide area networks (WANs).
UDP (User Data Protocol, user datagram protocol) is a protocol corresponding to TCP. It is a kind of TCP/IP protocol family.
Here is a diagram showing the relationship between these agreements.
Insert picture description here
Figure 1 The
TCP/IP protocol suite includes the transport layer, network layer, and link layer. Now you know the relationship between TCP/IP and UDP.
Where is Socket?
In Figure 1, we don't see the shadow of Socket, so where is it? Still use the picture to speak, it is clear at a glance.
Insert picture description here

Figure 2 The
original Socket is here.
What is Socket?
Socket is the middleware abstraction layer for communication between the application layer and the TCP/IP protocol suite. It is a set of interfaces. In the design mode, Socket is actually a facade mode, which hides the complex TCP/IP protocol family behind the Socket interface. For users, a set of simple interfaces is all, allowing Socket to organize data to conform to the specified protocol.
Will you use them?
The predecessors have done a lot for us, and the communication between networks is much simpler, but after all, there is still a lot of work to be done. I heard about Socket programming before and thought it was a relatively advanced programming knowledge, but as long as we understand the working principle of Socket programming, the mystery will be lifted.
A scene in life. If you want to call a friend, dial the number first, and the friend will pick up the phone after hearing the ringing. At this time, you and your friend will establish a connection and you can talk. When the communication is over, hang up the phone to end the conversation. The scenes in life explain the working principle. Perhaps the TCP/IP protocol family was born in life, and this is not necessarily true.
Insert picture description here

Figure 3
starts with the server side. The server first initializes the Socket, then binds to the port, listens to the port, calls accept to block, and waits for the client to connect. At this time, if a client initializes a Socket, and then connects to the server (connect), if the connection is successful, then the connection between the client and the server is established. The client sends a data request, the server receives the request and processes the request, then sends the response data to the client, the client reads the data, and finally closes the connection, and the interaction ends.

We are well versed in the value of information exchange, how do processes in the network communicate, such as when we open the browser to browse the web every day, how does the browser process communicate with the web server? When you chat with QQ, how does the QQ process communicate with the server or the QQ process of your friend? All these have to rely on sockets? What is a socket? What are the types of sockets? There are also the basic functions of sockets, which are all that this article wants to introduce. The main content of this article is as follows:

1. How to communicate between processes in the network?

2. What is Socket?

3. The basic operation of socket

3.1, socket() function

3.2, bind() function

3.3, listen(), connect() functions

3.4, accept() function

3.5, read(), write() functions, etc.

3.6, close() function

4. Detailed explanation of TCP three-way handshake connection establishment in socket

5. Detailed explanation of TCP's four-way handshake release connection in socket

6. An example bold style

Guess you like

Origin blog.csdn.net/yinlingjishu/article/details/108598270