当前位置:网站首页>[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
边栏推荐
猜你喜欢

Matlb| economic scheduling with energy storage, opportunity constraints and robust optimization

普通测试年薪15w,测试开发年薪30w+,二者差距在哪?

Classify the features of pictures with full connection +softmax

Remember the problem analysis of oom caused by a Jap query

【Socket】①Socket技术概述

wireshark安装

Unity使用MaskableGraphic画一条带箭头的线

运维管理系统有哪些特色

Hash table and full comments

Redis入门完整教程:RDB持久化
随机推荐
Fundamentals of process management
普通测试年薪15w,测试开发年薪30w+,二者差距在哪?
【2022国赛模拟】多边形——计算几何、二分答案、倍增
记一次JAP查询导致OOM的问题分析
【软件测试】最全面试问题和回答,全文背熟不拿下offer算我输
INS/GPS组合导航类型简介
Software testing -- common assertions of JMeter interface testing
Number theory --- fast power, fast power inverse element
wzoi 1~200
Digital scrolling increases effect
[software test] the most complete interview questions and answers. I'm familiar with the full text. If I don't win the offer, I'll lose
Redis入门完整教程:客户端常见异常
服装企业部署MES管理系统的五个原因
基于ensp防火墙双击热备二层网络规划与设计
Kubernetes源码分析(二)----资源Resource
Statistics of radar data in nuscenes data set
Redis入门完整教程:RDB持久化
Linear list --- circular linked list
PSINS中19维组合导航模块sinsgps详解(初始赋值部分)
widerperson数据集转化为YOLO格式