当前位置:网站首页>TCP server communication flow
TCP server communication flow
2022-07-01 04:21:00 【Soy sauce;】
1. Server communication model

tcp The server
Answer the phone model : Create socket ( Server phone ), binding ip And port on socket ( Bind the fixed telephone number ), Listen for new connections ( monitor ), Accept the connection ( Answer the phone ), Sending and receiving data ( Reading and writing is right socket File operations )( Talking and communicating ), close ( Hang up )
explain : There may be more than one host ip So we have to bind
explain : The client does not need to bind , The system randomly assigns a port to him
Detailed process


explain :
listen monitor Create a completed queue and an incomplete queue , The unfinished queue reaches the completed queue after three handshakes , Turn the server into passive receiving mode
accept Extract the connection from the completed queue , Get a new connected socket , Use this connected socket to communicate with the client , The original socket is only used to process new requests and receive new connections , Don't use the original communication .
Socket port and... Are connected ip And the original listening socket ip It is the same as the port
To serve many clients , More than one 
effect : Specialization , Improve work efficiency , Avoid confusion in a socket
Function description :
bind function
function : binding ip And port on the server socket
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen);
sockfd:
socket File descriptor , Corresponding to the target socket
addr:
Construct out IP Address plus slogan , Get server ipv4 Socket structure address
addrlen:
sizeof(addr) length ,ipv4 Size of structure
Return value :
Successfully returns 0, Failure to return -1, Set up errno
listen function
function : Listen for connections
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
int listen(int sockfd, int backlog);
sockfd:
socket File descriptor ,socket Socket
backlog:
Queue up 3 Second handshake queue and just established 3 The number of links in the secondary handshake queue and
The maximum value of the sum of completed connections and incomplete connections ( General filling 128 Will be enough )
explain :
Check the system default backlog
cat /proc/sys/net/ipv4/tcp_max_syn_backlog
Return value
listen() Successfully returns 0, Failure to return -1.
accept function
function : Extract a new connection from the completed connection queue
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen);
sockdf:
socket File descriptor , Socket
addr:
Out parameter , Return link client address information , contain IP Address and port number
Get client ipv4 Socket structure address
addrlen:
Address of socket structure size
Incoming and outgoing parameters ( value - result ), Pass in sizeof(addr) size , Function returns the size of the received address structure
Return value :
Successfully returned a new socket File descriptor , Create a new socket , For communicating with clients Don't use that old socket
Failure to return -1, Set up errno
explain : If there is no new connection ,accept It will block
边栏推荐
- HoloLens2开发环境搭建及部署app
- [ta - Frost Wolf May - 100 people plan] 1.2.1 base vectorielle
- [send email with error] 535 error:authentication failed
- TCP/IP 详解(第 2 版) 笔记 / 3 链路层 / 3.4 桥接器与交换机 / 3.4.2 多属性注册协议(Multiple Registration Protocol (MRP))
- Go learning --- unit test subtest
- 25.k sets of flipped linked lists
- Common thread methods and daemon threads
- Possible problems and solutions of using scroll view to implement slider view
- Use winmtr software to simply analyze, track and detect network routing
- Hololens2 development environment building and deploying apps
猜你喜欢

It's settled! 2022 JD cloud summit of JD global technology Explorer conference see you in Beijing on July 13

Use winmtr software to simply analyze, track and detect network routing

Jenkins automatically cleans up construction history
![[leetcode skimming] February summary (updating)](/img/62/0d0d9f11434e49d33754a2e4f2ea65.jpg)
[leetcode skimming] February summary (updating)

基于Unet的环路滤波

Knowledge supplement: basic usage of redis based on docker

283.移动零

After many job hopping, the monthly salary is equal to the annual salary of old colleagues

NFT:使用 EIP-2981 开启 NFT 版税之旅

Chen Yu (Aqua) - Safety - & gt; Cloud Security - & gt; Multicloud security
随机推荐
Hololens2 development environment building and deploying apps
离线安装wireshark2.6.10
创新界,聚势行 | 2022人大金仓“百城巡展”火热开启
MallBook:后疫情时代下,酒店企业如何破局?
Huawei simulator ENSP - hcip - Hybrid Experiment 2
Coinbase in a bear market: losses, layoffs, stock price plunges
Procurement intelligence is about to break out, and Alipay'3+2'system helps enterprises build core competitive advantages
什么是uid?什么是Auth?什么是验证器?
Haskell lightweight threads overhead and use on multicores
How to choose the right server for website data collection?
[learn C and fly] S1E20: two dimensional array
嵌入式系統開發筆記80:應用Qt Designer進行主界面設計
类和对象收尾
Internet winter, how to spend three months to make a comeback
Jenkins自动清理构建历史
Mallbook: how can hotel enterprises break the situation in the post epidemic era?
Threejs opening
嵌入式系统开发笔记81:使用Dialog组件设计提示对话框
JS image path conversion Base64 format
【TA-霜狼_may-《百人計劃》】1.2.1 向量基礎