【问题标题】:C# UDP sendto bufferC# UDP 发送到缓冲区
【发布时间】:2015-07-28 19:54:12
【问题描述】:

我正在制作一个将发送 1MB 长度数据的应用。 Bellow 是我的测试代码,它只是发送一个 1MB 的简单字节数组,但是即使我尝试将发送缓冲区增加到 1MB 或更高,它也会不断抛出波纹管异常。

代码

private void sendattack(string ip, int port)
    {
        IPEndPoint RemoteEndPoint = new IPEndPoint(IPAddress.Parse(ip), port);
        Socket serversoc = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
        char[] data = new char[100000];
        var send = Encoding.ASCII.GetBytes(data);
        serversoc.SendTo(send, send.Length, SocketFlags.None, RemoteEndPoint);
    }

错误

在数据报套接字上发送的消息大于内部消息缓冲区或其他网络限制,或者用于接收数据报的缓冲区小于数据报本身

System.Net.Sockets.SocketException 未处理 错误代码=10040 H结果=-2147467259 Message=在数据报套接字上发送的消息大于内部消息缓冲区或其他网络限制,或者用于接收数据报的缓冲区小于数据报本身 NativeErrorCode=10040 源=系统 堆栈跟踪: 在 System.Net.Sockets.Socket.SendTo(Byte[] 缓冲区,Int32 偏移量,Int32 大小,SocketFlags socketFlags,EndPoint remoteEP) 在 System.Net.Sockets.Socket.SendTo(Byte[] 缓冲区,Int32 大小,SocketFlags socketFlags,EndPoint remoteEP) 在 C:\Users\User\OneDrive\Documents\Visual Studio 2013\Projects\qnet\qnet\svchost.cs:line 84 中的 qnet.svchost.sendattack(String ip, Int32 端口) 在 C:\Users\User\OneDrive\Documents\Visual Studio 2013\Projects\qnet\qnet\svchost.cs:line 27 中的 qnet.svchost.Form1_Load(Object sender, EventArgs e) 在 System.Windows.Forms.Form.OnLoad(EventArgs e) 在 System.Windows.Forms.Form.OnCreateControl() 在 System.Windows.Forms.Control.CreateControl(布尔 fIgnoreVisible) 在 System.Windows.Forms.Control.CreateControl() 在 System.Windows.Forms.Control.WmShowWindow(消息和 m) 在 System.Windows.Forms.Control.WndProc(消息和 m) 在 System.Windows.Forms.ScrollableControl.WndProc(消息和 m) 在 System.Windows.Forms.Form.WmShowWindow(消息和 m) 在 System.Windows.Forms.Form.WndProc(消息和 m) 在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(消息& m) 在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(消息和 m) 在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd,Int32 msg,IntPtr wparam,IntPtr lparam) 内部异常:

【问题讨论】:

  • UDP 数据包大小为 65507 字节,用于纯 Windows 网络,否则更小。然后对于 1Mb 的数据包,切换到 TCP。

标签: c# sockets udp buffer


【解决方案1】:

错误是不言自明的:您不能发送那么大的数据包。一个UDP数据包的理论最大大小约为64KB,在不分片的情况下通过互联网安全发送的大小小于1KB:What is the largest Safe UDP Packet Size on the Internet

你需要把事情做得更小。

【讨论】:

  • 那么最好还是拆分成10KB的小包分别发送?
  • 是的,或者类似的;您必须在更高级别处理将数据分解为更小的部分。请记住,在重新组装时,无法保证数据包会以与发送时相同的顺序出现;所以这是另一个需要处理的复杂问题。
  • 拆分UDP数据包的问题是UDP不能接收所有数据包,这可能导致实现恢复机制。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-11
  • 2010-11-09
  • 2021-12-14
  • 2014-05-10
  • 2011-08-02
相关资源
最近更新 更多