【发布时间】: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。