套接字:有时(很少)数据包在接收过程中丢失 [英] Sockets: sometimes (rarely) packets are lost during receiving

查看:25
本文介绍了套接字:有时(很少)数据包在接收过程中丢失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Socket 从 udp 多播接收数据.代码很简单:

I'm using Socket to receive data from udp multicast. The code is trivial:

s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
....
while (true)
{
    int count = 0;
    try
    {
        count = socket.Receive(byteArray);
    }
    catch (Exception e)
    {
        Console.WriteLine(e.Message);
        return;
    }
    if (count > 0)
    {
        OnNewMessage(new NewMessageEventArgs(byteArray, count));
    }
}

问题是有时我会丢失数据包.不要太频繁,~每 2 分钟一次.

The problem is that sometimes I lose packets. Not too often, ~ once per 2 minutes.

我确定数据包已经到达,因为我可以在同一台计算机上启动并配置为接收相同数据包的另一个 C++ 程序中看到它.

I'm sure that packet is arrived because I can see it in another C++ program launched on the same computer and configured to receive same packets.

为什么我的程序不能捕获其他程序可以捕获的数据包?为什么我丢包?是不是计算机太慢(或太忙)而无法接收数据包?

Why my program can not catch packets that others can? Why I lose packets? Is it possible that computer is just too slow (or too busy) to receive packets?

我每秒接收大约 2000 个数据包并使用至强 E3 处理器,我认为这应该足够了...

I receive about 2 000 packets per second and using Xeon E3 processor, that should be more that enough I think...

推荐答案

如果您绝对确定数据包已到达(并且:我必须强调,使用 UDP 时不能保证这一点,并且 1每两分钟丢弃 2000 个数据包的数据包接收率比您希望的要好,即使对于两台相邻的机器也是如此),那么这可能意味着接收缓冲区在短时间内已满.尝试增加 ReceiveBufferSize.

If you are absolutely sure the packet is arriving (and: I must emphasise that this is not guaranteed when using UDP, and 1 packet dropped every two mintes at 2000 packets a second is a better receive rate than you should probably hope for, even for two adjacent machines), then this possibly means that the receive buffer is full at brief moments. Try increasing the ReceiveBufferSize.

这篇关于套接字:有时(很少)数据包在接收过程中丢失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆