gpt4 book ai didi

C# UDP 发送缓冲区

转载 作者:太空宇宙 更新时间:2023-11-03 23:32:26 25 4
gpt4 key购买 nike

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

代码

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);
}

错误

A message sent on a datagram socket was larger than the internal message buffer or some other network limit, or the buffer used to receive a datagram into was smaller than the datagram itself

System.Net.Sockets.SocketException was unhandled ErrorCode=10040 HResult=-2147467259 Message=A message sent on a datagram socket was larger than the internal message buffer or some other network limit, or the buffer used to receive a datagram into was smaller than the datagram itself NativeErrorCode=10040 Source=System StackTrace: at System.Net.Sockets.Socket.SendTo(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags, EndPoint remoteEP) at System.Net.Sockets.Socket.SendTo(Byte[] buffer, Int32 size, SocketFlags socketFlags, EndPoint remoteEP) at qnet.svchost.sendattack(String ip, Int32 port) in C:\Users\User\OneDrive\Documents\Visual Studio 2013\Projects\qnet\qnet\svchost.cs:line 84 at qnet.svchost.Form1_Load(Object sender, EventArgs e) in C:\Users\User\OneDrive\Documents\Visual Studio 2013\Projects\qnet\qnet\svchost.cs:line 27 at System.Windows.Forms.Form.OnLoad(EventArgs e) at System.Windows.Forms.Form.OnCreateControl() at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible) at System.Windows.Forms.Control.CreateControl() at System.Windows.Forms.Control.WmShowWindow(Message& m) at System.Windows.Forms.Control.WndProc(Message& m) at System.Windows.Forms.ScrollableControl.WndProc(Message& m) at System.Windows.Forms.Form.WmShowWindow(Message& m) at System.Windows.Forms.Form.WndProc(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) InnerException:

最佳答案

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

你需要让事情变得更小。

关于C# UDP 发送缓冲区,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31685865/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com