Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#UDP发送到缓冲区_C#_Sockets_Udp_Buffer - Fatal编程技术网

C#UDP发送到缓冲区

C#UDP发送到缓冲区,c#,sockets,udp,buffer,C#,Sockets,Udp,Buffer,我正在制作一个将发送1MB长度数据的应用程序。Bellow是我的测试代码,它只是发送一个1MB的简单字节数组,但是即使我尝试将发送缓冲区增加到1MB或更高,它也会不断抛出Bellow异常 代码 错误 在数据报套接字上发送的消息大于内部消息缓冲区或某些其他网络限制,或者用于接收数据报的缓冲区小于数据报本身 System.Net.Sockets.SocketException未处理 错误代码=10040 HResult=-2147467259 Message=在数据报套接字上发送的消息大于内部消息缓

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

代码

错误

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

System.Net.Sockets.SocketException未处理 错误代码=10040 HResult=-2147467259 Message=在数据报套接字上发送的消息大于内部消息缓冲区或其他网络限制,或者用于接收数据报的缓冲区小于数据报本身 NativeErrorCode=10040 来源=系统 堆栈跟踪: 位于System.Net.Sockets.Socket.SendTo(字节[]缓冲区、Int32偏移量、Int32大小、SocketFlags、SocketFlags、端点remoteEP) 位于System.Net.Sockets.Socket.SendTo(字节[]缓冲区,Int32大小,SocketFlags,SocketFlags,端点remoteEP) 在C:\Users\User\OneDrive\Documents\visualstudio 2013\Projects\qnet\qnet\svchost.cs中的qnet.svchost.sendattack(字符串ip,Int32端口)处:第84行 在C:\Users\User\OneDrive\Documents\visualstudio 2013\Projects\qnet\qnet\svchost.cs中的qnet.svchost.Form1_加载(对象发送方,事件参数e):第27行 在System.Windows.Forms.Form.OnLoad(EventArgs e)中 在System.Windows.Forms.Form.OnCreateControl()中 位于System.Windows.Forms.Control.CreateControl(布尔值不可修改) 在System.Windows.Forms.Control.CreateControl()中 在System.Windows.Forms.Control.WmShowWindow(Message&m)中 位于System.Windows.Forms.Control.WndProc(Message&m) 在System.Windows.Forms.ScrollableControl.WndProc(Message&m)中 在System.Windows.Forms.Form.WmShowWindow(消息和消息) 在System.Windows.Forms.Form.WndProc(Message&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) 内部异常:


错误是不言自明的:您不能发送这么大的数据包。UDP数据包的理论最大大小约为64KB,通过internet安全发送而不会出现碎片的大小小于1KB:


你需要把东西缩小一些

那么,最好将10KB的数据包分成更小的数据包,分别发送吗?是的,或者类似的;您必须在更高的级别处理将数据分解为更小的部分的问题。请记住,在重新组装时,无法保证数据包将以发送的相同顺序出现;因此,这是另一个需要处理的复杂问题。拆分UDP数据包的问题是,UDP不允许接收所有数据包,这可能会导致实施恢复机制。对于纯windows网络,UDP数据包大小为65507字节,其他情况下则小得多。然后,对于1Mb数据包,切换到TCP。
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);
    }