0
votes

I'm using udp socket in .NET;in particular I made a program that,by udp socket,sends image,from picture box,to other program.

This is the code:

this is the listener part where the socket receives image from port 8000

private void Listening()
        {
            IPEndPoint ipep = new IPEndPoint(IPAddress.Loopback, 8000);

            Socket newsock = new Socket(AddressFamily.InterNetwork,
                            SocketType.Dgram, ProtocolType.Udp);

            newsock.Bind(ipep);


            int recv;
            IPEndPoint sender1 = new IPEndPoint(IPAddress.Parse("192.168.1.1"), 6000);

            EndPoint Remote = (EndPoint)sender1;
            byte[] data = new byte[11230];


            recv = newsock.ReceiveFrom(data,ref Remote);

            MemoryStream x = new MemoryStream(data);

            Image y = Image.FromStream(x);

            pictureBox2.Image = y;

         }

this is the sending part where socket sends image [code]

private void Sending()
        {
            IPAddress host = IPAddress.Parse("127.0.0.1");
            IPEndPoint hostep = new IPEndPoint(host, 8000);
            Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram,    ProtocolType.Udp);

            sock.Connect(hostep);


            MemoryStream ms = new MemoryStream();

            pictureBox1.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);

            sock.Send(ms.ToArray());

          }

Both parts are in the same PC.

My problem is when it's called "Send" function in the "Sending" part it generates exception whit this message:

"The size of the message sent on a datagram socket is larger than the internal message buffer or some other network limit, or the size of the receive buffer of the datagram are lower than those of the datagram itself"

Thanks for your time

2
Don't use UDP. Seriously. Unless you really know you need UDP, do not use it. - Jonathon Reinhart
The error message is quite clear: the amount of data you're trying to send is too large for UDP. Besides that, there are tons of problems in your code, so +1 to what @JonathonReinhart said. - dtb
@dtb except for the choice of UDP instead of TCP,what are the errors? - Alessio Vinaccia

2 Answers

1
votes

I would advise you against your current approach. What you are attempting to achieve is not so simple to accomplish in a robust manner. I would advise you to check out the excellent zeromq library that nicely encapsulates raw socket api's and that provides a number of specific patterns to send and receive data.

For the problem that you are describing, the "publish / subscribe" pattern supported by zeromq might be a very good fit.

Zeromq is a crossplatform c library, but good CLR/.NET wrappers are available for it. I would urge you to read this article for example as an introduction: http://www.codeproject.com/Articles/488207/ZeroMQ-via-Csharp-Introduction

0
votes

The error message is pretty straitforward: you are exceeding the limit of message size. In UDP, you send datagrams, not a stream of bytes. In IPv4, you cannot send a UDP datagram larger than 65507 bytes (i.e., 64KiB including IPv4 and UDP headers).

UDP suits well for passing short messages, but for transferring large amounts of data TCP is more preferable.