如何动态分配缓冲区用于接收UDP套接字(VB.Net)
我和一个朋友正在开发一个项目,我们需要使用 VB.Net 构建一个可靠的 UDP 客户端/服务器。我们的工作进展顺利,但仍然困扰我们的一件事是如何为传入数据动态分配(字节)缓冲区。现在我们必须硬编码最大值/MTU(或者使用非常大的缓冲区大小并在完成接收后调整它的大小)。有谁知道无需指定接收缓冲区大小即可完成此操作的方法?
基本上,在使用大小为 x 的缓冲区调用套接字上的接收函数之前,我们想知道 x,以便我们可以分配适当大小的缓冲区。也许这是您必须处理的所有套接字编程中的一个问题?
A friend and I are working on a project where we're required to build a reliable UDPclient/server using VB.Net. We have things working well, but one thing that still eludes us is how to dynamically allocate a (byte) buffer for the incoming data. Right now we have to hard code a maximum value/MTU (or use a really large buffer size and resize it once we've finished receiving). Does anyone know of a way that this can be done without needing to specify the receive buffer size?
Basically, before calling the receive function on the socket with a buffer of size x, we want to know x so we can allocate an appropriately sized buffer. Perhaps this is a problem in all socket programing that you just have to deal with??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是使用 UDP 时必须承担的负担之一。您必须考虑路径 MTU 发现。话又说回来,由于您正在创建可靠的 UDP,因此您应该能够自动检测到这一点并动态切换到较小的数据包大小。这也将解决 PMTUD 问题。
希望这听起来不太像:“那些不使用 TCP 的人注定要重新发明它。”查看该文章中链接的 RFC 以获取想法。
This is one of the burden's you'll have to take on when you use UDP. You'll have to consider Path MTU discovery. Then again, since you are making reliable UDP, you should be able to auto-detect this and dynamically switch to a smaller packet size. That will solve PMTUD problems as well.
Hopefully, this doesn't sound too much like: "those whose don't use TCP are doomed to reinvent it." Check out the RFCs that are linked in that article for ideas.