当前位置:网站首页>Socket programming (medium)
Socket programming (medium)
2022-06-13 11:10:00 【Think and act 66】
socket Programming ( in )
List of articles

1. The network accesses the public network ip Or private network ip
- When the client and server are on the same machine , Visit the public network ip Or private network ip Fine
- Clients and servers need to cross the network ( When not on the same machine ), Access the server public network ip
2.tcp The programming process
- Server side : Create socket , Binding address information , monitor , Get a new connection , Sending and receiving data , Close the connection
- client : Create socket , Do not bind address information ( Can be bound ), A connection , Sending and receiving data , Close the connection
- Listening meaning : monitor tcp Client new link , Establish a new connection with the client ( Be careful : here tcp The connection is established in the kernel )
- Get the meaning of the new connection : Get the socket descriptor of the new connection , every last tcp The connection produces a socket descriptor
- The meaning of initiating a connection : Send... To the server txp Connect
3. Correlation function
monitor :
int listen(int sockfd,int backlog);
- sockfd: socket descriptor
- backlog:tcp Number of concurrent connections ( Size of completed connection )
- Connection queue not completed : Connections that are still in the process of connection establishment are placed in this queue ( It can be understood that the connection with three handshakes is in the queue )
- Size of completed connection queue : Connection established , Connections that can communicate normally are placed in this queue ( It can be understood that the connection after three handshakes is in the queue )
- It can be modified by :/proc/sys/net/ipv4/tcp_max_syn_backlog The value of it , Modify the size of the incomplete connection queue
- Return value : Success for 0, Failure is -1
Blocking call :
int accept(int sockfd,struct sockaddr *addr,socklen_t *addrlen);
- sockfd: socket descriptor
- addr: Address information structure , A structure that describes client address information (ip、 port )
- addrlen: Length of address information
- Return value : Successfully returned the newly connected socket , Failure to return -1
- Be careful : If there is no established connection in the completed queue , The block ; If you get a new connection , Just go back to
- Socket returned : The socket of the new connection returned is for communication with the client , No monitoring function , It also contains the address information of the client
- summary : The service side with socket Created socket descriptor , Is used to listen on sockets , Listen for new connections
The service side with accept Create a new connection socket , Is to communicate with the client
Connect :
int connect(int sockfd,const struct sockaddr *addr,socklen_t addrlen);
- sockfd: socket descriptor
- addr: Address information structure , Describe the server address information (ip、 port )
- addrlen: Length of address information
- Return value :0 For success ,-1 The failure
- Be careful : This function can not only complete the connection function , If the client is not bound , At the same time, you can bind the address information of the client
send data :
ssize_t send(int sockfd,const void *buf,size_t len,int flags);
- sockfd: socket descriptor (accept Received , Not listening on sockets )
- buf: send out buf Description pointing to space
- len: Data length
- flags:0( Blocking transmission )
- Return value : Successfully return the number of bytes sent , Failure to return -1
receive data :
ssize_t resv(int sockfd,void *buf,size_t len,int flags);
- sockfd: socket descriptor
- buf: Store the received data in buf Designated space , Space needs to be developed by programmers in advance
- len: Number of bytes expected to be accepted
- flags:0( Blocking reception )
- Return value : success , Number of bytes received ,0, Close the connection end to end ,-1, Receive error
边栏推荐
- Prim求最小生成树(朴素版稠密图)
- 宝塔添加一个网站:PHP项目
- 很妙的贪心(F2. Nearest Beautiful Number (hard version))
- The road of ospo construction of Weibo: how to promote enterprise open source through ospo construction?
- 判定二分图和二分图最大匹配
- What is 400g Ethernet?
- DNS protocol analysis
- MFC custom button to realize color control
- Go needs to add an arrow syntax, which is more like PHP!
- 2020 ICPC Asia Taiwan Online Programming Contest C Circles
猜你喜欢

塔米狗知识|全面剖析国有企业并购含义及其作用

F2. nearest beautiful number (hard version)

Go needs to add an arrow syntax, which is more like PHP!

Go zero microservice Practice Series (III. API definition and table structure design)

【TcaplusDB知识库】TcaplusDB单据受理-创建游戏区介绍

【TcaplusDB知识库】Tmonitor后台一键安装介绍(一)

Use of servers

St table learning

Finally, the monthly income is 20000!!

Digital DP example
随机推荐
It's the first time that the programmer interview pays so much attention to the concept of investigation
Model building process 1==miidock
ST表学习
Web3和NFT中的匿名性问题
ACP | 东北地理所在气象-空气质量双向耦合模式研究中取得进展
WinForm resolves frequent refresh of black screen
22. Class E power amplifier design of ads usage record (Part 2)
恶意代码实战分析Lab05-01
Meta universe land: what makes digital real estate valuable
Acwing game 55
Prim求最小生成树(朴素版稠密图)
Nature communications - modeling armed conflict risk under climate change using machine learning and time series data
Easyclick run code snippet out null
Vivo large scale kubernetes cluster automation operation and maintenance practice
MySQL transaction isolation level and mvcc
【TcaplusDB知识库】TcaplusDB新增机型介绍
21世纪以来的历次“粮食危机”,发生了什么?
高斯消元求n元方程组
Questions and answers of the labor worker general basic (labor worker) work license in 2022
硬件工程师薪资虚高,你认可吗?