当前位置:网站首页>[socket] ① overview of socket technology
[socket] ① overview of socket technology
2022-07-07 02:50:00 【Caixukun, Houchang Village Road】
List of articles

One 、 summary
When we design software systems , There is often a need for network communication with other software . Now? , Most network communications are based on TCP/IP Agreement to proceed , And at the bottom of the TCP/IP agreement 、 The process is cumbersome ,socket The programming interface provides a simple TCP/IP Mode of operation .
- demand : Network communication is needed for information exchange between software processes .
- TCP/IP agreement : It is a data transmission scheme .
- Socket Interface : It is the abstraction of network communication endpoint , One can shield TCP/IP Cumbersome low-level operation tools .
Information communication flow of application , As shown in the figure :
Two 、TCP And UDP Workflow 、 difference
Reference resources :https://blog.csdn.net/weixin_47021806/article/details/124883559
( One )TCP/UDP Workflow
1、UDP Communication workflow
| Server side | establish socket | Binding address / port | send out / receive data | close |
|---|---|---|---|---|
| client | establish socket | ---- | send out / receive data | close |

2、TCP Communication workflow
| Server side | establish socket | Binding address / port | Convert to passive connection ( monitor ) | Receive client connections | receive / send data | close |
|---|---|---|---|---|---|---|
| client | establish socket | – | – | Connect to server | send out / receive data | close |

- socket() Method : Is used to create a socket , With sockets, you can send and receive data through the network . This is why a program that communicates over a network must first create a socket . Specify the type of service to use when creating a socket , Use TCP Protocol selection streaming service (SOCK_STREAM).
- bind() Method : It is used to specify socket usage IP Address and port .IP The address is the address of your host , If the host is not connected to the network , The loopback address can be used when testing the program “127.0.0.1”. The port is a 16 Bit shaping value , commonly 0-1024 Is a well-known port , Such as HTTP The use of 80 Port no. . This kind of port is generally not used by users . secondly ,1024-4096 Reserved port for , Users generally do not use .4096 These are temporary ports , Users can use . stay Linux On ,1024 Port number within , Only root Users can use .
- listen() Method : It is used to create a listening queue . There are two types of listening queues , One is to store connections that have not completed three handshakes , One is to store the connection that has completed three handshakes .listen() The second parameter is to specify the length of the queue that has completed three handshakes .
- accept(): Method processing is stored in listen Create a connection in the queue that has completed three handshakes . Each connection processed , be accept(): Returns the socket descriptor corresponding to the connection . If the queue is empty , be accept Blocking .
- connect(): Methods are generally executed by client programs , You need to specify the server side of the connection IP Address and port . After the method is executed , There will be three handshakes , Establishing a connection .
- send() Method : Used to direct to TCP The opposite end of the connection sends data .send() Successful implementation , It can only indicate that the data is successfully written to the sending buffer of the sending end , It does not mean that the data has been sent to the opposite end .send() The return value of is the length of data actually written into the transmit buffer .
- recv() Method : For reception TCP Data sent by the opposite end of the connection .recv() Read data from the local receive buffer , If there is no data in the receive buffer , be recv() Method will block . The return value is the number of bytes actually read , If
- recv() The return value is 0, It means that the other party has closed TCP Connect .
- close(): Method to close TCP Connect . here , Will wave four times .
3、TCP And UDP Communication differences
(1) Difficult and easy to operate
UDP It's a connectionless protocol , So there's no need to look like TCP equally listen and accept, It's easy to operate
UDP The server : establish socket、 Binding port 、 Transceiver operation 、 close socket
TCP The server : establish socket、 Binding port 、 Listening port 、 Receiving connection 、 Transceiver operation 、 close socket
(2) Retransmission mechanism and delay
UDP If the transmitted message is lost on the network , It's lost .UDP The protocol itself has no retransmission mechanism .
and TCP There is a message at the bottom of the protocol to verify whether it arrives , If you lose , The mechanism that the sender will retransmit .
UDP: It can ensure real-time
TCP: Can guarantee integrity
(3) Message order problem
TCP The information data of both sides of the protocol communication is like flowing in the pipeline , There is a clear order .
Sender Application The message sent first is Ken First received by the receiving application . and UDP The protocol sends independent messages one by one , The receiving order of the receiving application is not necessarily the same as the sending order .
(4)TCP Advantages and disadvantages :
advantage :
1.TCP Provides for explicitly creating and terminating connections in an approved way .
2.TCP Be sure to be reliable 、 Sequential ( Packets are received in the order they are sent ) And data transmission that won't repeat .
3.TCP Process flow control .
4. Allow data first
5. If the data is not transmitted to , be TCP Socket interface returns an error status condition .
6.TCP Processing large data blocks by keeping them continuous and dividing them into smaller pieces .— No need for programmers to know
shortcoming : TCP You must create... When transferring data ( And keep ) A connection . This connection adds overhead to the communication process , Let it compare UDP Slow down .
(5)UDP Advantages and disadvantages :
1.UDP There is no requirement to keep a connection
2.UDP No packet received due to receiver's acceptance ( Or automatic retransmission when the packet does not arrive correctly ) And the cost .
3. Design UDP The purpose of the is for short applications and control messages
4. On the basis of a packet connecting a packet ,UDP Required network bandwidth ratio TDP smaller .
3、 ... and 、C/S The choice of server and client solutions in the architecture
1、 The server needs to be bound IP And port
2、Tcp You need to start the server first
边栏推荐
猜你喜欢

Classify the features of pictures with full connection +softmax

Number theory --- fast power, fast power inverse element

Redis入门完整教程:客户端管理

Electrical engineering and automation

慧通编程入门课程 - 2A闯关

Digital scrolling increases effect

Redis getting started complete tutorial: client management
Django数据库(SQlite)基本入门使用教程

MySQL - common functions - string functions

Have fun | latest progress of "spacecraft program" activities
随机推荐
3 -- Xintang nuc980 kernel supports JFFS2, JFFS2 file system production, kernel mount JFFS2, uboot network port settings, and uboot supports TFTP
Detailed explanation of 19 dimensional integrated navigation module sinsgps in psins (filtering part)
Mmdetection3d loads millimeter wave radar data
Difference and the difference between array and array structure and linked list
unity中跟随鼠标浮动的面板,并可以自适应文字内容的大小
PSINS中19维组合导航模块sinsgps详解(初始赋值部分)
软件测试——Jmeter接口测试之常用断言
Compress JS code with terser
记一次JAP查询导致OOM的问题分析
Unity custom webgl packaging template
Matlb| economic scheduling with energy storage, opportunity constraints and robust optimization
哈希表及完整注释
Wireshark installation
[Mori city] random talk on GIS data (II)
6-6漏洞利用-SSH安全防御
NuScenes数据集关于Radar数据的统计
AWS learning notes (I)
一本揭秘字节万台节点ClickHouse背后技术实现的白皮书来了!
Planning and design of double click hot standby layer 2 network based on ENSP firewall
Kubernetes源码分析(二)----资源Resource