当前位置:网站首页>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 .
边栏推荐
- [daily training] 207 Class Schedule Card
- 洛谷P2048 [NOI2010] 超级钢琴(RMQ+优先队列)
- 产品经理专业知识50篇(四)-从问题到能力提升:AMDGF模型工具
- C control refresh
- c#搭建ftp服务器并实现文件上传和下载
- C examples of using colordialog to change text color and fontdialog to change text font
- TCP MIN_RTO 辩证考
- 【补题】2021牛客暑期多校训练营6-n
- 1464. maximum product of two elements in an array
- Force deduction 76 questions, minimum covering string
猜你喜欢

c#磁盘驱动器及文件夹还有文件类的操作

自制坡道,可是真的很香

Application of can optical transceiver of ring network redundant can/ optical fiber converter in fire alarm system

Three Siemens fire-fighting hosts fc18 are equipped with can optical transceiver for optical fiber redundant ring network networking test

将数据导入到MATLAB

Electronics: Lesson 011 - experiment 10: transistor switches

Vscode is good, but I won't use it again

飞机引气系统的建模与故障仿真

CVPR 2022 Oral 2D图像秒变逼真3D物体

网络模型——OSI模型与TCP/IP模型
随机推荐
Debugging mipi-dsi screen based on stm32mp157
洛谷P2048 [NOI2010] 超级钢琴(RMQ+优先队列)
Functions should not specify operation types through variables
What are the problems with traditional IO? Why is zero copy introduced?
C # set up FTP server and realize file uploading and downloading
TCP与UDP
Import data into Matlab
电子学:第012课——实验 13:烧烤 LED
洛谷P2486 [SDOI2011]染色(树链+线段树 + 树上区间合并 )
2160. minimum sum of the last four digits after splitting
剑指offer刷题(中等等级)
基于Anaconda的模块安装与注意事项
Solving some interesting problems with recurrence of function
Invalid Navicat scheduled task
This article uses pytorch to build Gan model!
【补题】2021牛客暑期多校训练营9-n
[Mobius inversion]
Set the textalign property of the label control in C to control the method of text centering
c#搭建ftp服务器并实现文件上传和下载
C control refresh