当前位置:网站首页>[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
边栏推荐
- 安全巡检的工作
- [Mori city] random talk on GIS data (II)
- The 8 element positioning methods of selenium that you have to know are simple and practical
- The annual salary of general test is 15W, and the annual salary of test and development is 30w+. What is the difference between the two?
- Derivative, partial derivative, directional derivative
- Digital scrolling increases effect
- Qt蓝牙:QBluetoothDeviceInfo
- Statistics of radar data in nuscenes data set
- Static proxy of proxy mode
- 普通测试年薪15w,测试开发年薪30w+,二者差距在哪?
猜你喜欢
进程管理基础
Redis入门完整教程:复制配置
Google Earth engine (GEE) -- 1975 dataset of Landsat global land survey
MATLB|具有储能的经济调度及机会约束和鲁棒优化
Es6中Promise的使用
HAVE FUN | “飞船计划”活动最新进展
软件测试——Jmeter接口测试之常用断言
Electrical engineering and automation
Statistics of radar data in nuscenes data set
Remember the problem analysis of oom caused by a Jap query
随机推荐
6-6漏洞利用-SSH安全防御
wzoi 1~200
Oracle中日期的使用方法实例
记一次JAP查询导致OOM的问题分析
如何设计好接口测试用例?教你几个小技巧,轻松稿定
wireshark安装
MySQL
Cloud Mail . NET Edition
Lombok makes the pit of ⽤ @data and @builder at the same time
Mmdetection3d loads millimeter wave radar data
unity 自定义webgl打包模板
What management points should be paid attention to when implementing MES management system
QT common Concepts-1
NuScenes数据集关于Radar数据的统计
Use of fiddler
Contribution of Writing Series
The annual salary of general test is 15W, and the annual salary of test and development is 30w+. What is the difference between the two?
1 -- Xintang nuc980 nuc980 porting uboot, starting from external mx25l
哈希表及完整注释
Pioneer of Web3: virtual human