我怎么可以设置下的Socket UDP的缓冲区大小? C# [英] How can I set the buffer size for the underneath Socket UDP? C#

查看:6171
本文介绍了我怎么可以设置下的Socket UDP的缓冲区大小? C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如我们所知道的UDP接收,​​我们使用Socket.ReceiveFrom或UdpClient.receive

As we know for UDP receive, we use Socket.ReceiveFrom or UdpClient.receive

Socket.ReceiveFrom接受一个字节数组从你把UDP数据的采集。

Socket.ReceiveFrom accept a byte array from you to put the udp data in.

UdpClient.receive直接返回一个字节数组

UdpClient.receive returns directly a byte array where the data is

我的问题是,如何设置内部套接字缓冲区大小。我认为操作系统维护其自己的缓冲区接收UDP数据,对不对?对于例如,如果一个UDP数据包被发送到我的机器,操作系统就会把它的缓冲区,等待我们去Socket.ReceiveFrom或UdpClient.receive,对吧?

My question is that How to set the buffer size inside Socket. I think the OS maintains its own buffer for receive UDP data, right? for e.g., if a udp packet is sent to my machine, the OS will put it to a buffer and wait us to Socket.ReceiveFrom or UdpClient.receive, right?

如何更改内部缓冲区的大小?

How can I change the size of that internal buffer?

我曾尝试Socket.ReceiveBuffSize,它没有任何效果在所有的UDP,并明确表示这是TCP窗口。此外,我已经做了很多实验证明这是Socket.ReceiveBufferSize不是UDP。

I have tried Socket.ReceiveBuffSize, it has no effect at all for UDP, and it clearly said that it is for TCP window. Also I have done a lot of experiments which proves Socket.ReceiveBufferSize is NOT for UDP.

任何人都可以分享给UDP内部缓冲区的一些见解???

Can anyone share some insights for UDP internal buffer???

感谢

我看到一些帖子在这里,如

I have seen some posts here, for e.g.,

http://social.msdn.microsoft.com /论坛/ EN-US / NCL /线程/ c80ad765-b10f-4bca-917e-2959c9eb102a

戴夫说,Socket.ReceiveBufferSize可以设置内部缓冲器对于UDP。 。我不同意

Dave said that Socket.ReceiveBufferSize can set the internal buffer for UDP. I disagree.

我做的实验是这样的:

27主机发送一个10KB的UDP数据包发送到我一个局域网同时(至少几乎)内。我有一个while循环来处理每个数据包的。对于每个数据包,我创建了一个线程处理。我用UdpClient或插口接收的报文。

27 hosts send a 10KB udp packet to me within a LAN at the same time (at least almost). I have a while-loop to handle each of the packet. For each packet, I create a thread a handle it. I used UdpClient or Socket to receive the packets.

我失去了分组的50%左右。我认为这是UDP一阵发送,我不能处理所有的时间。

I lost about 50% of the packets. I think it is a burst of the UDP sending and I can't handle all of them in time.

这就是为什么我要增加对UDP缓冲区的大小。比方说,如果我改变缓冲区大小为1MB,那么27 * 10KB = 270KB的数据可以在缓冲区被接受,对吧?

This is why I want to increase the buffer size for UDP. say, if I change the buffer size to 1MB, then 27 * 10KB = 270KB data can be accepted in the buffer, right?

我试图改变Socket.ReceiveBufferSize很多许多价值,它只是没有在所有的效果。

I tried changing Socket.ReceiveBufferSize to many many values, and it just does not have effects at all.

任何一个可以帮助?

推荐答案

我使用.NET UDPClient经常和我一直用的 Socket.ReceiveBufferSize ,并有良好的效果。在内部调用 Socket.SetSocketOption 用的ReceiveBuffer 参数。这里是一些快速,简单的代码,你可以测试:

I use the .NET UDPClient often and I have always used the Socket.ReceiveBufferSize and have good results. Internally it calls Socket.SetSocketOption with the ReceiveBuffer parameter. Here is a some quick, simple, code you can test with:

public static void Main(string[] args)
{
  IPEndPoint remoteEp = null;
  UdpClient client = new UdpClient(4242);
  client.Client.ReceiveBufferSize = 4096;

  Console.Write("Start sending data...");
  client.Receive(ref remoteEp);
  Console.WriteLine("Good");

  Thread.Sleep(5000);
  Console.WriteLine("Stop sending data!");
  Thread.Sleep(1500);

  int count = 0;
  while (true)
  {
    client.Receive(ref remoteEp);
    Console.WriteLine(string.Format("Count: {0}", ++count));
  }
}



尝试调整传递到ReceiveBufferSize价值。我测试了送5秒内的数据络绎不绝,并得到了10个数据包。然后,我增加了4倍,下一次得到了38包。

Try adjusting the value passed into the ReceiveBufferSize. I tested sending a constant stream of data for the 5 seconds, and got 10 packets. I then increased x4 and the next time got 38 packets.

我想看看你的网络中的其他地方,你可能会丢弃数据包。特别是因为你提到你的其他帖子您要发送的数据包10KB。当它被发送到分组的MTU的大小10KB会分散。如果系列中的任何1包被丢弃整个包将被丢弃。

I would look to other places in your network where you may be dropping packets. Especially since you mention on your other post that you are sending 10KB packets. The 10KB will be fragmented when it is sent to packets the size of MTU. If any 1 packet in the series is dropped the entire packet will be dropped.

这篇关于我怎么可以设置下的Socket UDP的缓冲区大小? C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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