1

Im making an app that will be sending data of 1MB length. Bellow is my testing code which is just sending a simple byte array of 1MB, However it keeps throwing the bellow exception even when I try increasing the send buffer to 1MB or above.

Code

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

Error

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 Packet size is 65507 bytes, for pure windows networking and much smaller otherwise. Then for 1Mb packets, switch to TCP. – Graffito Jul 28 '15 at 20:08

1 Answers1

3

The error is self-explanatory: You cannot send a packet that large. The theoretical maximum size of a UDP packet is about 64KB, and the size that will be safely sent over the internet without fragmentation is less than 1KB: What is the largest Safe UDP Packet Size on the Internet

You need to make things smaller.

Community
  • 1
  • 1
user5090812
  • 888
  • 1
  • 7
  • 7
  • So would it be best to split into smaller packets of 10KB and send them separately? –  Jul 28 '15 at 20:06
  • Yes, or something like that; you'll have to handle breaking down your data into smaller pieces at a higher level. Remember that on reassembly, there's no guarantee that the packets will come out in the same order that they were sent; so that's another complication to deal with. – user5090812 Jul 28 '15 at 20:08
  • The problem with splitting UDP packets is that UDP doesn't grant reception of all packets, what may lead to implement a recovery mechanism. – Graffito Jul 28 '15 at 20:11