当前位置:网站首页>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
边栏推荐
- Network metering - application layer
- Offline installation of Wireshark 2.6.10
- js 图片路径转换base64格式
- 网站服务器:好用的网站服务器怎么选这五方面要关注
- ThreeJS开篇
- Common thread methods and daemon threads
- Embedded System Development Notes 79: why should I get the IP address of the local network card
- Use selenium automated test tool to climb the enrollment score line and ranking of colleges and universities related to the college entrance examination
- Ospfb notes - five messages [ultra detailed] [Hello message, DD message, LSR message, LSU message, lsack message]
- Coinbase in a bear market: losses, layoffs, stock price plunges
猜你喜欢
跳槽一次涨8k,5年跳了3次...
Task04 | statistiques mathématiques
js 图片路径转换base64格式
Edge浏览器的小技巧:Enter+Ctrl可以自动将地址栏转换为网址
In the innovation community, the "100 cities Tour" of the gold warehouse of the National People's Congress of 2022 was launched
定了!2022京东全球科技探索者大会之京东云峰会7月13日北京见
Knowledge supplement: basic usage of redis based on docker
NFT: utilisez EIP - 2981 pour commencer un voyage de redevances NFT
[human version] Web3 privacy game in the dark forest
基于Unet的环路滤波
随机推荐
Unity之三维空间多点箭头导航
DO280管理应用部署--RC
Use winmtr software to simply analyze, track and detect network routing
js 图片路径转换base64格式
JMeter学习笔记2-图形界面简单介绍
使用WinMTR软件简单分析跟踪检测网络路由情况
TCP/IP 详解(第 2 版) 笔记 / 3 链路层 / 3.4 桥接器与交换机 / 3.4.2 多属性注册协议(Multiple Registration Protocol (MRP))
Why is Hong Kong server most suitable for overseas website construction
网站服务器:好用的网站服务器怎么选这五方面要关注
[TA frost wolf \u may- hundred talents plan] 1.2.2 matrix calculation
Knowledge supplement: basic usage of redis based on docker
(12) Somersault cloud case (navigation bar highlights follow)
[Master / slave] router election in DD message
MallBook:后疫情时代下,酒店企业如何破局?
LeetCode 1828. Count the number of points in a circle
做网站数据采集,怎么选择合适的服务器呢?
Go learning --- unit test subtest
CUDA development and debugging tool
All in one 1086: Jiaogu conjecture
In the innovation community, the "100 cities Tour" of the gold warehouse of the National People's Congress of 2022 was launched