当前位置:网站首页>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 .
边栏推荐
- 挖掘微生物暗物质——新思路
- socket问题记录
- DNS协议及其DNS完整的查询过程
- Neural network and deep learning-3-simple example of machine learning pytorch
- 【补题】2021牛客暑期多校训练营6-n
- 图像超分综述:超长文一网打尽图像超分的前世今生 (附核心代码)
- 【莫比乌斯反演】
- 取消word文档中某些页面的页眉
- 50. pow (x, n) - fast power
- Three Siemens fire-fighting hosts fc18 are equipped with can optical transceiver for optical fiber redundant ring network networking test
猜你喜欢

时钟刻度盘的绘制

Electronics: Lesson 014 - Experiment 15: intrusion alarm (Part I)
![洛谷P1073 [NOIP2009 提高组] 最优贸易(分层图+最短路)](/img/cb/046fe4b47898fd6db86edc8a267c34.png)
洛谷P1073 [NOIP2009 提高组] 最优贸易(分层图+最短路)

Allgero reports an error: program has encoded a problem and must exit The design will be saved as a . SAV file

Introduction to the main functions of the can & canfd comprehensive test and analysis software lkmaster of the new usbcan card can analyzer

Basic use of ActiveMQ in Message Oriented Middleware

FM signal, modulated signal and carrier

深度学习系列45:图像恢复综述

Importer des données dans MATLAB

一文了解 | 革兰氏阳性和阴性菌区别,致病差异,针对用药
随机推荐
云计算考试版本1.0
MySQL simple permission management
allgero报错:Program has encountered a problem and must exit. The design will be saved as a .SAV file
Importer des données dans MATLAB
C # set up FTP server and realize file uploading and downloading
C examples of using colordialog to change text color and fontdialog to change text font
洛谷P2486 [SDOI2011]染色(树链+线段树 + 树上区间合并 )
Atlas conference vulnerability analysis collection
Electronics: Lesson 010 - Experiment 9: time and capacitors
不怕百战失利,就怕灰心丧气
What are the problems with traditional IO? Why is zero copy introduced?
c#磁盘驱动器及文件夹还有文件类的操作
TCP 加速小记
Sword finger offer II 027 Palindrome linked list
自制坡道,可是真的很香
CAN总线工作状况和信号质量“体检”
Analysis of kinsing dual platform mining family virus
牛客:飞行路线(分层图+最短路)
Debugging mipi-dsi screen based on stm32mp157
Niuke: flight route (layered map + shortest path)