使用udp将数据发送到多个客户端C# [英] Send data using udp to multiple clients C#

查看:225
本文介绍了使用udp将数据发送到多个客户端C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望每分钟都能通过udp进行通信的设备进行状态更新。我应该用什么方法。任何建议将不胜感激。谢谢



我尝试了什么:



我试过这个



 foreach(iPAddresses中的IPAddress ips)
{
Byte [] receiveBytes = {};
Byte [] dataToSend = new Byte [] {0x8B,0xB9,0x00,0x03,0x05,0x01,0x09};
IPEndPoint endPoint =新IPEndPoint(ips,1024);
EndPoint ep =(EndPoint)endPoint;
Socket udpClient = new Socket(AddressFamily.InterNetwork,SocketType.Dgram,ProtocolType.Udp);

尝试
{
udpClient.Connect(endPoint);
udpClient.SendTo(dataToSend,ep);
// Thread.Sleep(50);
IPEndPoint localip = new IPEndPoint(IPAddress.Any,1200);

UdpClient receivingUdpClient = new UdpClient(localip);


receiveBytes = receivingUdpClient.Receive(ref endPoint);
// Thread.Sleep(50);

udpClient.Close();


receivingUdpClient.Close();
receivingUdpClient.Dispose();
updateData(receiveBytes,ips);
}
最后
{

udpClient.Dispose();
}

}







我能找到的问题是通过这一行 -



 receiveBytes = receivingUdpClient.Receive(ref endPoint); 







receive()中的ref端点只有第一个ip地址。它没有变化。发送方法可以更改IP地址。但是没有接收方法。



在此之前我也尝试过其他一些方法。但这就是我现在正在做的事情

解决方案

你没有检查连接(返回)状态。



您可能无法连接;或者不等待足够长的时间。



但是既然你实际上忽略了任何错误(空的前处理程序),你就会想知道发生了什么

I want status update from the devices which communicate on udp, every minute. what method i should use. Any kind of suggestions will be appreciated. Thanks

What I have tried:

I have tried this

foreach (IPAddress ips in iPAddresses)
        {
            Byte[] receiveBytes = { };
            Byte[] dataToSend = new Byte[] { 0x8B, 0xB9, 0x00, 0x03, 0x05, 0x01, 0x09 };
            IPEndPoint endPoint = new IPEndPoint(ips, 1024);
            EndPoint ep = (EndPoint)endPoint;
            Socket udpClient = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

            try
            {
                udpClient.Connect(endPoint);
                udpClient.SendTo(dataToSend, ep);
               // Thread.Sleep(50);
                IPEndPoint localip = new IPEndPoint(IPAddress.Any, 1200);

                UdpClient receivingUdpClient = new UdpClient(localip);


                receiveBytes = receivingUdpClient.Receive(ref endPoint);
               // Thread.Sleep(50);

                udpClient.Close();


                receivingUdpClient.Close();
                receivingUdpClient.Dispose();
                updateData(receiveBytes, ips);
            }
            finally
            {

                udpClient.Dispose();
            }

        }




The problem I can find is through this line-

receiveBytes = receivingUdpClient.Receive(ref endPoint);




ref endpoint in receive() is having 1st ip address only. its not changing. Send method is able to change ip address. but not receive method.

I have tried some other methods too before this. but this is what I am doing now

解决方案

You're not checking the "connect" (return) status.

You're probably failing to connect; or not "waiting" long enough.

But since you're effectively ignoring any errors ("empty" ex handler), you're left to wonder what's going on.


这篇关于使用udp将数据发送到多个客户端C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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