UDP 套接字 C#.NET [英] UDP socket C#.NET

查看:39
本文介绍了UDP 套接字 C#.NET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 .NET 中使用 udp 套接字;特别是我制作了一个程序,通过 udp 套接字将图像从图片框发送到其他程序.

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.

这是代码:

这是套接字从端口 8000 接收图像的侦听器部分

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;

         }

这是socket发送图像的发送部分[代码]

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"

感谢您的时间

推荐答案

我建议您不要使用当前的方法.您试图实现的目标并不是以稳健的方式完成的那么简单.我建议您查看出色的 zeromq 库,该库很好地封装了原始套接字 API,并提供了许多发送和接收数据的特定模式.

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.

对于您所描述的问题,zeromq 支持的发布/订阅"模式可能非常适合.

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

Zeromq 是一个跨平台的 c 库,但是可以使用良好的 CLR/.NET 包装器.例如,我建议您阅读这篇文章作为介绍:http:///www.codeproject.com/Articles/488207/ZeroMQ-via-Csharp-Introduction

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

这篇关于UDP 套接字 C#.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆