当前位置:网站首页>[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
边栏推荐
- 1 -- Xintang nuc980 nuc980 porting uboot, starting from external mx25l
- Electrical engineering and automation
- Digital scrolling increases effect
- Static proxy of proxy mode
- Remember the problem analysis of oom caused by a Jap query
- AWS learning notes (I)
- Redis getting started complete tutorial: common exceptions on the client
- AWS学习笔记(一)
- 测试优惠券要怎么写测试用例?
- Increase 900w+ playback in 1 month! Summarize 2 new trends of top flow qiafan in station B
猜你喜欢
Classify the features of pictures with full connection +softmax
Statistics of radar data in nuscenes data set
Station B's June ranking list - feigua data up main growth ranking list (BiliBili platform) is released!
Web3's need for law
Dotconnect for DB2 Data Provider
Pioneer of Web3: virtual human
Wireshark installation
Apifox, is your API interface document rolled up like this?
【2022国赛模拟】多边形——计算几何、二分答案、倍增
Have fun | latest progress of "spacecraft program" activities
随机推荐
Rethinking of investment
Unity custom webgl packaging template
1 -- Xintang nuc980 nuc980 porting uboot, starting from external mx25l
Niuke programming problem -- double pointer of 101 must be brushed
C语言练习题_1
6-6漏洞利用-SSH安全防御
Work of safety inspection
What management points should be paid attention to when implementing MES management system
[Mori city] random talk on GIS data (II)
Kysl Haikang camera 8247 H9 ISAPI test
[leetcode]Search for a Range
左程云 递归+动态规划
Google Earth Engine(GEE)——Landsat 全球土地调查 1975年数据集
The 8 element positioning methods of selenium that you have to know are simple and practical
运维管理系统有哪些特色
How to write test cases for test coupons?
Redis入门完整教程:客户端常见异常
Detailed explanation of 19 dimensional integrated navigation module sinsgps in psins (time synchronization part)
The third season of ape table school is about to launch, opening a new vision for developers under the wave of going to sea
PCL 常用拟合模型及使用方法