如何在 C# 中确定 UDP 套接字缓冲区大小?

发布于 2024-10-17 21:41:40 字数 125 浏览 1 评论 0原文

我将连续发送 ECG 数据的 UDP 数据包(1500 字节)长达 5 分钟。我可以使用什么安全的 socket.ReceiveBufferSize ?我将过滤这些数据并绘制传入的数据。我想防止任何缓冲区问题。

I'll be sending UDP packets (1500 bytes) of ECG data continuously for up to 5 minutes. What is a safe socket.ReceiveBufferSize I could use? I'll be filtering this data and plotting the data as it comes in. I want to prevent any buffer issues.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

橘虞初梦 2024-10-24 21:41:40
int packetSize = 1500; // as stated in the question
int safeBufferSize = maxPacketsPerSec * packetSize * howManySecondsToBuffer;

计算或估计 maxPacketsPerSec 的值,为 howManySecondsToBuffer 选择一个安全/慷慨的值,例如 5 范围内的值(至少可以承受最严重的垃圾)收集暂停)到 60(如果你完全偏执的话,甚至是完整的 300 秒),然后进行数学计算。

更重要的是:尽快将字节从套接字接收缓冲区中取出,将它们交给某个队列,该队列由其他线程或多个线程(如果需要)处理。这样,您的应用内队列将缓冲与 RAM 一样多的数据,并且您的 Socket.ReceiveBufferSize 值将变得不那么重要。

int packetSize = 1500; // as stated in the question
int safeBufferSize = maxPacketsPerSec * packetSize * howManySecondsToBuffer;

Figure out or estimate your value for maxPacketsPerSec, choose a safe/generous value for howManySecondsToBuffer, something in the range of, say, 5 (to at least weather even the worst garbage collection pauses) to 60 (or even the full 300 seconds if you are completely paranoid), and then do the math.

More important though: Heave the bytes out of the socket receive buffer as fast as you can, hand them over to some queue which is processed by some other thread, or multiple threads, if need be. That way your in-app queue will buffer as much as you have RAM, and your Socket.ReceiveBufferSize value will become less important.

樱娆 2024-10-24 21:41:40

如果你想防止任何问题,你需要使用 TCP。
如果您使用 udp,您可能会丢失数据或得到错误的数据包序列。

If you want to prevent any issues you need to use TCP.
In case you are using udp you are posibly loss data or get wrong sequence of packets.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文