当前位置:网站首页>[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
边栏推荐
- Google Earth engine (GEE) -- 1975 dataset of Landsat global land survey
- How to write test cases for test coupons?
- widerperson数据集转化为YOLO格式
- PSINS中19维组合导航模块sinsgps详解(滤波部分)
- QT common Concepts-1
- MySQL提升大量数据查询效率的优化神器
- MATLB|具有储能的经济调度及机会约束和鲁棒优化
- [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入门完整教程:客户端案例分析
- Cloud Mail .NET Edition
猜你喜欢
Cloud Mail .NET Edition
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?
Redis入门完整教程:客户端案例分析
What are the characteristics of the operation and maintenance management system
KYSL 海康摄像头 8247 h9 isapi测试
Five reasons for clothing enterprises to deploy MES management system
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?
Django数据库(SQlite)基本入门使用教程
Wireshark installation
The 8 element positioning methods of selenium that you have to know are simple and practical
随机推荐
Django database (SQLite) basic introductory tutorial
【Socket】①Socket技术概述
How to write test cases for test coupons?
What are the applications and benefits of MES management system
导数、偏导数、方向导数
左程云 递归+动态规划
PCL 常用拟合模型及使用方法
Detailed explanation of 19 dimensional integrated navigation module sinsgps in psins (time synchronization part)
The 8 element positioning methods of selenium that you have to know are simple and practical
MySQL - common functions - string functions
Cloud Mail .NET Edition
用全连接+softmax对图片的feature进行分类
Redis getting started complete tutorial: common exceptions on the client
PSINS中19维组合导航模块sinsgps详解(初始赋值部分)
Redis入门完整教程:复制拓扑
代码调试core-踩内存
Unity使用MaskableGraphic画一条带箭头的线
MES管理系统的应用和好处有哪些
MySQL
Common fitting models and application methods of PCL