C#UDP sendto缓冲区

时间:2015-07-28 19:54:12

标签: c# sockets udp buffer

我正在制作一个会发送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     的HResult = -2147467259     消息=在数据报套接字上发送的消息大于内部消息缓冲区或某些其他网络限制,或者用于接收数据报的缓冲区小于数据报本身     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中的qnet.svchost.sendattack(String ip,Int32 port):第84行          在c:\ Users \ User \ OneDrive \ Documents \ Visual Studio 2013 \ Projects \ qnet \ qnet \ svchost.cs中的qnet.svchost.Form1_Load(Object sender,EventArgs e):第27行          在System.Windows.Forms.Form.OnLoad(EventArgs e)          在System.Windows.Forms.Form.OnCreateControl()          在System.Windows.Forms.Control.CreateControl(Boolean 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(Message& m)          在System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)          在System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd,Int32 msg,IntPtr wparam,IntPtr lparam)     InnerException:

1 个答案:

答案 0 :(得分:3)

错误不言自明:您无法发送大的数据包。 UDP数据包的理论最大大小约为64KB,通过互联网安全发送而没有碎片的大小小于1KB:What is the largest Safe UDP Packet Size on the Internet

你需要缩小规模。