当前位置:网站首页>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
边栏推荐
- [learn C and fly] S1E20: two dimensional array
- 不同性能测试工具的并发模式
- 【LeetCode】100. Same tree
- C language games (I) -- guessing games
- HoloLens2开发环境搭建及部署app
- NFT:使用 EIP-2981 開啟 NFT 版稅之旅
- Obtain detailed ideas for ABCDEF questions of 2022 American Games
- Daily question - line 10
- Jenkins automatically cleans up construction history
- NFT: start NFT royalty journey with eip-2981
猜你喜欢
![[learn C and fly] S1E20: two dimensional array](/img/68/34fad73ff23d3e0719ef364fc60cb5.jpg)
[learn C and fly] S1E20: two dimensional array

Introduction of Spock unit test framework and its practice in meituan optimization___ Chapter I

Volley parsing data shows networking failure

JMeter learning notes 2 - brief introduction to graphical interface

Recommend the best product development process in the Internet industry!

Annual inventory review of Alibaba cloud's observable practices in 2021

定了!2022京东全球科技探索者大会之京东云峰会7月13日北京见

MySQL advanced -- you will have a new understanding of MySQL

Use of JMeter counters
![[leetcode skimming] February summary (updating)](/img/62/0d0d9f11434e49d33754a2e4f2ea65.jpg)
[leetcode skimming] February summary (updating)
随机推荐
Account sharing technology enables the farmers' market and reshapes the efficiency of transaction management services
Network metering - application layer
嵌入式系统开发笔记81:使用Dialog组件设计提示对话框
HoloLens2开发环境搭建及部署app
Libevent Library Learning
Usage of AfxMessageBox and MessageBox
mysql 函数 变量 存储过程
TS type gymnastics: illustrating a complex advanced type
Use of JMeter counters
Unity's 3D multi-point arrow navigation
MFC window scroll bar usage
Mallbook: how can hotel enterprises break the situation in the post epidemic era?
CUDA development and debugging tool
Threejs opening
离线安装wireshark2.6.10
NFT: start NFT royalty journey with eip-2981
分账技术赋能农贸市场,重塑交易管理服务效能
431. encode n-ary tree as binary tree DFS
ThreeJS开篇
采购数智化爆发在即,支出宝'3+2'体系助力企业打造核心竞争优势