[UWP] DatagramSocket.MessageReceived不会在传入的广播消息上触发 [英] [UWP] DatagramSocket.MessageReceived does not trigger on incoming broadcast messages

查看:115
本文介绍了[UWP] DatagramSocket.MessageReceived不会在传入的广播消息上触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从我在过去6小时内搜索和尝试的内容来看,这一定是关于这个问题的第100个问题。 (有人甚至使用DatagramSocket吗?)



情况:


  1. 具有专用网络(C& S)功能的UWP应用(目标10586-15063)
  2. UDP广播发送至"255.255.255.255:7171"由网络中的Python服务(来自端口7070
  3. DatagramSocket 从不触发MessageReceived
  4. NuGet:System。 Net.Sockets.UdpClient工作得很漂亮但不能使用,因为代码必须使用Unity(HoloLens)

可以使用但无法使用的代码:

 var client = new UdpClient(); //来自System.Net.Sockets NuGet包
client.Client.SetSocketOption(SocketOptionLevel.Socket,SocketOptionName.ReuseAddress,true);
client.MulticastLoopback = false;
client.EnableBroadcast = true;
client.Client.Bind(new IPEndPoint(IPAddress.Any,7171));

while(true)
{
UdpReceiveResult incoming = await client.ReceiveAsync();
string message = Encoding.UTF8.GetString(incoming.Buffer);
}

现在是"现代"的DatagramSocket版本完全坏了。
坏了。

 async void Setup()
{
var ds = new DatagramSocket( );
ds.MessageReceived + = ds_MessageReceived;


await _Client.BindServiceNameAsync(" 7171"); //不起作用

//等待_Client.BindEndpointAsync(null," 7171"); //不起作用

//等待_Client.ConnectAsync(new HostName(" 255.255.255.255")," 7070"); //不起作用

// HostName myname = NetworkInformation.GetHostNames()。FirstOrDefault();
// var ep = new EndpointPair(myname," 7171",new HostName(" 255.255.255.255")," 7070");
//等待_Client.ConnectAsync(ep); //不起作用(机器名无效)

Debug.WriteLine(" Receiving。");
}

async void ds_MessageReceived(DatagramSocket sender,DatagramSocketMessageReceivedEventArgs args)
{
Debug.WriteLine(" Client_MessageReceived");
}

我在这里做错了吗?


这是一个知道没有修复的错误,因为没有人甚至使用DatagramSocket?


任何帮助都将不胜感激。


欢呼





$

解决方案

找到解决方案:DatagramSocket 仅在首次发送消息后才开始接收消息


对于Unity / UWP兼容的UDP广播接收器外观在这里:  https://gist.github.com/michaelosthege/857acac92b8ee689a6bb30d5bf23d9f6




From what I've searched and tried in the past 6 hours, this must be the hundredth question on this problem. (Is anybody even using the DatagramSocket?)

Situation:

  1. UWP app with Private Networks (C & S) capability (targeting 10586-15063)
  2. UDP broadcast sent to "255.255.255.255:7171" by a Python service in the network (from port 7070)
  3. DatagramSocket never triggers MessageReceived
  4. NuGet:System.Net.Sockets.UdpClient works beautifully but can't be used since the code has to play with Unity (HoloLens)

The code that works but can't be used:

var client = new UdpClient(); // from System.Net.Sockets NuGet package
client.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
client.MulticastLoopback = false;
client.EnableBroadcast = true;
client.Client.Bind(new IPEndPoint(IPAddress.Any, 7171));

while (true)
{
    UdpReceiveResult incoming = await client.ReceiveAsync();
    string message = Encoding.UTF8.GetString(incoming.Buffer);
}

And now the "modern" DatagramSocket version that is totally broken. So broken.

async void Setup()
{
    var ds = new DatagramSocket();
    ds.MessageReceived += ds_MessageReceived;

    
    await _Client.BindServiceNameAsync("7171");  // doesn't work

    //await _Client.BindEndpointAsync(null, "7171");  // doesn't work

    //await _Client.ConnectAsync(new HostName("255.255.255.255"), "7070");    // doesn't work
                
    //HostName myname = NetworkInformation.GetHostNames().FirstOrDefault();
    //var ep = new EndpointPair(myname, "7171", new HostName("255.255.255.255"), "7070");
    //await _Client.ConnectAsync(ep);  // doesn't work (machine name is not valid)

    Debug.WriteLine("Receiving.");
}

async void ds_MessageReceived(DatagramSocket sender, DatagramSocketMessageReceivedEventArgs args)
{
    Debug.WriteLine("Client_MessageReceived");
}

Is there something I am doing wrong here?

Is this a know bug that just doesn't get fixed because nobody even uses the DatagramSocket?

Any help would be appreciated.

cheers

解决方案

Found the solution: The DatagramSocket only starts to receive messages after it was first used to send!

For Unity/UWP compatible UDP broadcast receiver look here: https://gist.github.com/michaelosthege/857acac92b8ee689a6bb30d5bf23d9f6



这篇关于[UWP] DatagramSocket.MessageReceived不会在传入的广播消息上触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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