使用C#进行UDP广播 [英] UDP Broadcasting using c#

查看:340
本文介绍了使用C#进行UDP广播的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我正在使用c#udp套接字开发一个应用程序,我在局域网中有40到50台计算机,我的应用程序广播其IP和名称,这ip + name消息字符串到达​​20 25台计算机,但不是全部,尽管所有计算机都在运行相同的接收器模块.我检查了每台计算机上的网络设置是否相同.
我正在使用以下发送和接收方法:

发件人:
      ;        套接字senderSocket =新的Socket(AddressFamily.InterNetwork,SocketType.Dgram,ProtocolType.Udp);
;  senderSocket.EnableBroadcast = true;
               senderSocket.SetSocketOption(SocketOptionLevel.Socket,SocketOptionName.Broadcast,1);
IPEndPoint iep1 =新IPEndPoint(IPAddress.Broadcast,1000);//9050);
 字符串消息=连接";
               byte []数据= Encoding.ASCII.GetBytes(message);
senderSocket.SendTo(data,iep1);
              senderSocket.Close();

收件人:

           套接字袜子=新套接字(AddressFamily.InterNetwork,
           &nsp    SocketType.Dgram,ProtocolType.Udp);
           IPEndPoint iep =新IPEndPoint(IPAddress.Any,1000);//9050);
sock.Bind(iep);
            EndPoint ep =(EndPoint)iep;
          
           byte [] data = new byte [1024];
           int recv = sock.ReceiveFrom(data,ref ep);
string str = Encoding.ASCII.GetString(data,0,recv);

亲爱的专家们如何使此广播过程可靠,请帮助我!

问候

阿卜杜拉·拜格


阿卜杜拉

Hi all,


I'm developing an application using c# udp sockets, I've 40 to 50 machines in local area network, my application broadcasts its IP and name, this ip+name message string reaches to 20 25 machines but not to all, despite that all machines have same receiver module is running. I've checked the network setting on each machine is same.
I'm using following sending and receiving methods:

Sender:
                Socket senderSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                senderSocket.EnableBroadcast = true;
                senderSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1);
                IPEndPoint iep1 = new IPEndPoint(IPAddress.Broadcast, 1000);//9050);
                string message = "connect";
                byte[] data = Encoding.ASCII.GetBytes(message);
                senderSocket.SendTo(data, iep1);
                senderSocket.Close();

Receiver:

            Socket sock = new Socket(AddressFamily.InterNetwork,
                     SocketType.Dgram, ProtocolType.Udp);
            IPEndPoint iep = new IPEndPoint(IPAddress.Any, 1000);//9050);
            sock.Bind(iep);
            EndPoint ep = (EndPoint)iep;
           
            byte[] data = new byte[1024];
            int recv = sock.ReceiveFrom(data, ref ep);
            string str = Encoding.ASCII.GetString(data, 0, recv);

Dear experts how to make this broadcasting process reliable, Help me!!!

thanks in advance

regards

Abdullah Baig


Abdullah

推荐答案

不,您不能使UDP可靠,尤其是在广播中,但是50%的故障率是不寻常的.

不能发送给相同机器的机器总是一样吗?如果是这样,我建议您检查防火墙,并且它们都位于同一子网中(广播到255.255.255.255的数据包无法路由,只能由您的本地子网接收).

HTH
--mc

No, you cannot make UDP reliable, especially with broadcasting, but a 50% fault rate is unusual.

Are the machines you cannot send to always the same? If so I would recommend that you check the firewalls and that they all live in the same subnet (packets broadcasted to 255.255.255.255 cannot be routed and will only be received by your local subnet).

HTH
--mc


这篇关于使用C#进行UDP广播的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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