gpt4 book ai didi

.Net UDP 数据包丢失,尽管所有数据包都到达(Wireshark) - ReceiveBuffer 不是问题

转载 作者:行者123 更新时间:2023-12-03 12:00:26 25 4
gpt4 key购买 nike

这与 C# UDP packetloss though all packets arrive (WireShark) 相同的问题,但我确实设置了 ReceiverBuffer到 10MB,我还监控 Socket.Available这表明缓冲区甚至没有达到 200k 的利用率。

在一个简单的测试案例中,使用 tcpreplay 以 40Mbps 的速率发送相同的小(多播)UDP 数据包(净数据大小:137)25,000 次,通常不是所有数据包都到达我的程序,尽管所有数据包都到达了 Wireshark,我在那个盒子上同时运行。有时只丢失一个数据包,有时大约十几个,有时更多。

代码

static void Main()
{
int udpPacketsReceived = 0;
var bindAddress = IPAddress.Parse("192.168.20.54");
var socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
socket.ReceiveBufferSize = 10485760; // 10M

socket.Bind(new IPEndPoint(bindAddress, 51001));
socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(IPAddress.Parse("239.193.1.1")));
Console.WriteLine("Effective ReceiveBuffer size: " + socket.GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveBuffer));

var t = new Thread(ipAndClient =>
{
var buffer = new byte[2000];
int maxAvail = 0;
while (true)
{
socket.Receive(buffer);
Interlocked.Increment(ref udpPacketsReceived);
var available = socket.Available;
if (available > maxAvail)
{
Console.WriteLine("available new max: " + available);
maxAvail = available;
}
}
});
t.Start();
int lastValue = -1;
while (true)
{
var newValue = Thread.VolatileRead(ref udpPacketsReceived);
if (newValue != lastValue)
{
Console.WriteLine("Total UDP packets received: {0}", newValue);
lastValue = newValue;
}
Thread.Sleep(1000);
}
}

示例输出
Effective ReceiveBuffer size: 10485760
Total UDP packets received: 0
available new max: 685
available new max: 3288
available new max: 3836
available new max: 6165
available new max: 8220
available new max: 10960
Total UDP packets received: 14307
available new max: 18632
available new max: 140425
available new max: 141521
available new max: 143439
Total UDP packets received: 24996

怎么了?

重要补充 (事后证明这一点至关重要,请参阅 my own answer ):我正在 VirtualBox 主机中的 Windows 客户机上运行,​​使用 “桥接”虚拟网卡 .

最佳答案

Wireshark 在数据包被传递到协议(protocol)栈之前获取数据包,而接收缓冲区位于内核协议(protocol)栈的末尾。有很多内存分配和其他处理正在进行,在高负载时,它可能会在一些数据包到达接收缓冲区之前简单地丢弃它们。

这是MS support had to say :

Winsock layer sits on top of the transport layer. Datagrams may be dropped at various layers within the networking stack.

关于.Net UDP 数据包丢失,尽管所有数据包都到达(Wireshark) - ReceiveBuffer 不是问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26540513/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com