当前位置:网站首页>Socket problem record
Socket problem record
2022-06-25 08:05:00 【qq_ forty-two million eight hundred and sixty-three thousand ni】
This article mainly records socket Code problems .
List of articles
recv(size) Larger than buffer size What's going to happen
The problem is that the author wants to send a large file , But the pass was slow , So increase the buffer size , Although the donkey is not right , But I thought about it as he thought , That should be no different from normal , This problem is similar to that the data in the buffer is 10, In your app size by 100, What you receive is 10 Well .
The following code
char msgbuffer[100];
int size;
if((size = recv(sock, msgbuffer, sizeof(msgbuffer), 0)) < 0){
// Read buffer data to msgbuffer in
cout << " Read error ! The reason for the error errno:" << errno << endl;
close(sock);
return 0;
}
else if(size == 0){
cout << " The reader is turned off " << endl;
close(sock);
return 0;
}
else
cout << " Read successful :" << msgbuffer << endl << " Read byte :" << size << endl;
Set the threshold
Read threshold can be set , You can't write , I don't know why
This is because the online article writing, reading and writing thresholds are 1 and 2048, But I checked , All are 1, And the read threshold can be modified , It is not allowed to write the threshold .
int val;
socklen_t valLen = sizeof(int);
cout << " Socket read buffer " << endl;
getsockopt(sock, SOL_SOCKET, SO_RCVBUF, &val, &valLen);
cout << " Default size :" << val << endl;
getsockopt(sock, SOL_SOCKET, SO_RCVLOWAT, &val, &valLen);
cout << " Default water level :" << val << endl;
cout << " Socket write buffer " << endl;
getsockopt(sock, SOL_SOCKET, SO_SNDBUF, &val, &valLen);
cout << " Default size :" << val << endl;
getsockopt(sock, SOL_SOCKET, SO_SNDLOWAT, &val, &valLen);
cout << " Default water level :" << val << endl;
val = 1000;
setsockopt(sock, SOL_SOCKET, SO_RCVLOWAT, &val, sizeof(val));
getsockopt(sock, SOL_SOCKET, SO_RCVLOWAT, &val, &valLen);
cout << " The reading water level is modified to :" << val << endl;
val = 1000;
setsockopt(sock, SOL_SOCKET, SO_SNDLOWAT, &val, sizeof(val));
getsockopt(sock, SOL_SOCKET, SO_SNDLOWAT, &val, &valLen);
cout << " Write the water level as :" << val << endl;
You will find that the writing water level is 1 And can't modify .
Read / write and modify buffer size
When reading data > Read side buffer
When writing data > Write side buffer , What's going to happen .
When reading data > Read side buffer
Nothing will happen . Reading end-to-end data is actually the sending data of the opposite end . When connecting, the sending end will set the sending window according to the buffer size of the reading end , Therefore, it will not overflow . The following code
// Modify the read end size to the minimum : The minimum size of the read end is 2304
val = 1;
setsockopt(sock, SOL_SOCKET, SO_RCVBUF, &val, sizeof(int));
getsockopt(sock, SOL_SOCKET, SO_RCVBUF, &val, &valLen);
cout << " Read end size :" << val << endl;
// Assume that the peer transmission is greater than 2304 data . Set to 20000
//recv Read from buffer
char msgbuffer[1000000];
int size;
int sum = 0;
while(true){
size = recv(sock, msgbuffer, sizeof(msgbuffer), 0);
cout << " Read bytes :" << size << endl;
sum += size;
if(sum == 20000){
cout << " Read sum :" << sum << endl;
return 0;
}
}
We will find that the data in the buffer does not exceed the buffer size .
// The code above msgbuffer It is amended as follows 100
char msgbuffer[100];
You can see read-only 100. This is because msgbuffer by 100, But that doesn't mean the buffer is just 100 The data of .
When writing data > Write side buffer
rwqrq
rqwrqw
// Modify the write end size to the minimum : The minimum write end size is 4608
val = 1;
setsockopt(sock, SOL_SOCKET, SO_SNDBUF, &val, sizeof(int));
getsockopt(sock, SOL_SOCKET, SO_SNDBUF, &val, &valLen);
cout << " Write end size :" << val << endl;
// Assume that the peer transmission is greater than 2304 data . Set to 20000
// write in
char msgbuffer[20000];
int size;
size = send(sock, msgbuffer, sizeof(msgbuffer), 0);
cout << " Write Bytes :" << size << endl;
send(sock, msgbuffer, 1000000, 0); To write 100w
recv(sock, msgbuffer, 1000000, 0); Read 100w
Consider two things separately
1.100w And buffer space
2.100w And the number of buffers
100w < Space
Write a successful
100w = Space
Write a successful
100w > Space
Writing failure , Returns the number of writes .
100w < Number
Read successfully
100w = Number
Read successfully
100w > Number
Read failed , Returns the number of reads .
let me put it another way
When it comes to writing > Written in , Describe buffer space < There is only so much to write
When it comes to reading > Read the , Specify the number of buffers < There is only so much to read
recv(sock, msgbuffer, 1000000, 0); Read 100w
return < 100w : The read buffer has < 100w The data of
return = 100w : The read buffer has >=100w The data of
return > 100w : It's impossible to return > 100w
Sending buffer size modification is useless , Read buffer can . I don't understand why .
边栏推荐
- 电子学:第014课——实验 15:防入侵报警器(第一部分)
- Force deduction 76 questions, minimum covering string
- 使用报文和波形记录分析仪RoyalScope的帧统计功能排查CAN总线偶发性故障
- CAN透传云网关CANIOT,CANDTU记录CAN报文远程收发CAN数据
- Dietary intervention reduces cancer treatment-related symptoms and toxicity
- How to create a new branch with SVN
- 洛谷P2839 [国家集训队]middle(二分 + 主席树 + 区间合并)
- Vscode is good, but I won't use it again
- 初体验完全托管型图数据库 Amazon Neptune
- 基于Anaconda的模块安装与注意事项
猜你喜欢
Import data into Matlab
Electronics: Lesson 010 - Experiment 9: time and capacitors
50 pieces of professional knowledge of Product Manager (IV) - from problem to ability improvement: amdgf model tool
Debugging mipi-dsi screen based on stm32mp157
CVPR 2022 Oral 2D图像秒变逼真3D物体
力扣 272. 最接近的二叉搜索树值 II 递归
[deep learning lightweight backbone] 2022 edgevits CVPR
产品经理专业知识50篇(四)-从问题到能力提升:AMDGF模型工具
Opencv minimum filtering (not limited to images)
挖掘微生物暗物质——新思路
随机推荐
函数尽量不要通过变量指定操作类型
Machine learning notes linear regression of time series
1464. maximum product of two elements in an array
挖掘微生物暗物质——新思路
使用报文和波形记录分析仪RoyalScope的帧统计功能排查CAN总线偶发性故障
WebSocket的理解以及应用场景
2160. minimum sum of the last four digits after splitting
How to create a new branch with SVN
socket问题记录
线程+线程问题记录
Import data into Matlab
时钟刻度盘的绘制
[Mobius inversion]
417-二叉树的层序遍历1(102. 二叉树的层序遍历、107.二叉树的层次遍历 II、199.二叉树的右视图、637.二叉树的层平均值)
Opencv minimum filtering (not limited to images)
Basic use of ActiveMQ in Message Oriented Middleware
Authority design of SaaS system based on RBAC
CAN透传云网关CANIOT,CANDTU记录CAN报文远程收发CAN数据
共话云原生数据库的未来
C # set up FTP server and realize file uploading and downloading