UDP服务器套接字缓冲区溢出 [英] UDP Server Socket Buffer Overflow
问题描述
我正在Linux上编写C ++应用程序.我的应用程序有一个UDP服务器,该服务器在某些事件上将数据发送到客户端. UDP服务器还从客户端收到一些反馈/确认.
I am writing a C++ application on Linux. My application has a UDP server which sends data to clients on some events. The UDP server also receives some feedback/acknowledgement back from the clients.
为实现此应用程序,我使用了一个UDP套接字(例如int fdSocket
)从所有客户端发送和接收数据.我将其绑定到端口8080,并将套接字设置为NON_BLOCKING模式.
To implement this application I used a single UDP Socket(e.g. int fdSocket
) to send and receive data from all the clients. I bound this socked to port 8080 and have set the socket into NON_BLOCKING mode.
我创建了两个线程.在一个线程中,我等待某个事件发生,如果发生事件,则使用fdsocket将数据发送到所有客户端(在for循环中).
I created two threads. In one thread I wait for some event to happen, if an event occurs then I use the fdsocket to send data to all the clients(in a for loop).
在另一个线程中,我使用fdSocket
从客户端(recvfrom()
)接收数据.该线程计划每4秒运行一次(即,每4秒钟它将调用recvfrom()
从套接字缓冲区中检索数据.由于它处于NON-BLOCKING模式,因此如果没有UDP数据,则recvfrom()
函数将立即返回.可以,那么我将入睡4秒钟.)
In the another thread I use the fdSocket
to receive data from clients (recvfrom()
). This thread is scheduled to run every 4 seconds (i.e. every 4 seconds it will call recvfrom()
to retrieve the data from the socket buffer. Since it is in NON-BLOCKING mode the recvfrom()
function will return immediately if no UDP data is available, then I will go to sleep for 4 secs).
来自所有客户端的UDP反馈/确认具有固定的有效负载,其大小为20字节.
The UDP Feedback/Acknowledge from all the clients has a fixed payload whose size is 20bytes.
现在我有两个与此实现有关的问题:
Now I have two questions related to this implementation:
- 使用相同的套接字发送/接收UDP数据是否正确 与众多客户?
- 如何找到我的应用程序可以处理的最大UDP反馈/确认包数,而不会导致UDP套接字缓冲区溢出(因为如果我每4秒读取一次,则我 在这4秒钟内收到很多数据包,我可能会丢失一些数据包,即,我需要找到可以安全处理的数据包/秒的速率)?
- Is it correct to use the same socket for sending/receiving UDP data with Mulitiple clients ?
- How to find the maximum number of UDP Feedback/Acknowledge Packets my application can handling without UDP Socket Buffer Overflow (since I am reading at every 4secs, if I receive lot of packets within this 4 seconds I might loose some packet ie., I need to find the rate in packets/sec I can handle safely)?
我尝试使用功能调用getsockopt(fdsocket,SOL_SOCKET,SO_RCVBUF,(void *)&n, &m);
来获取我的套接字(fdsocket
)的Linux套接字缓冲区大小.通过此函数,我发现我的套接字缓冲区大小为110592.但是我不清楚该套接字缓冲区中将存储哪些数据:它将仅存储UDP有效载荷或整个UDP数据包还是整个以太网数据包?我引用了这个链接来获取一些内容想法,但感到困惑.
I tried to get the Linux Socket Buffer size for my socket (fdsocket
) using the function call getsockopt(fdsocket,SOL_SOCKET,SO_RCVBUF,(void *)&n, &m);
. From this function I discover that my Socket Buffer size is 110592. But I am not clear as what data will be stored in this socket buffer: will it store only the UDP Payload or Entire UDP Packet or event the Entire Ethernet Packet? I referred this link to get some idea but got confused.
当前我的代码有点脏,我将对其进行清理并将其不久后发布在这里.
Currently my code it little bit dirty , I will clean and post it soon here.
以下是我在发布此问题之前引用的链接.
The following are the links I have referred before posting this question.
- Linux网络
- UDP SentTo和Recvfrom最大缓冲区大小
- UDP套接字缓冲区溢出检测
- UDP广播和通过同一套接字的单播吗?
- 从同一UDP套接字中发送多线程
- 如何刷新C中UDP套接字的输入缓冲区?
- 如何查找套接字缓冲区大小linux
- 我如何获取UDP套接字的排队数据量?
- Linux Networking
- UDP SentTo and Recvfrom Max Buffer Size
- UDP Socket Buffer Overflow Detection
- UDP broadcast and unicast through the same socket?
- Sending from the same UDP socket in multiple threads
- How to flush Input Buffer of an UDP Socket in C?
- How to find the socket buffer size of linux
- How do I get amount of queued data for UDP socket?
推荐答案
以4秒钟的固定间隔读取套接字肯定会导致您丢失数据包.非阻塞I/O的常规尝试方法是解复用器系统调用 poll(2)
/ epoll(7)
.看看是否可以使用它们来捕获/响应其他事件.
Having socket reading at fixed interval of four seconds definitely sets you up for losing packets. The conventional tried-and-true approach to non-blocking I/O is the de-multiplexer system calls select(2)
/poll(2)
/epoll(7)
. See if you can use these to capture/react to your other events.
On the other hand, since you are already using threads, you can just do blocking recv(2) without that four second sleep.
阅读史蒂文斯来解释SO_RCVBUF
.
这篇关于UDP服务器套接字缓冲区溢出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!