【问题标题】:Hololens UDP Server never receives messageHololens UDP Server 从不接收消息
【发布时间】:2017-10-17 11:06:50
【问题描述】:

我正在制作一个具有统一性的 Hololens 应用程序,它会启动一个 udp 服务器。这个等待来自外部 udp 客户端的消息。这是服务器端:

using UnityEngine;
using System;
using System.IO;


#if !UNITY_EDITOR
using Windows.Networking.Sockets;
#endif

public class server : MonoBehaviour
{
#if !UNITY_EDITOR
    DatagramSocket socket;
#endif
#if UNITY_EDITOR
    void Start()
    {
    }
#endif
#if !UNITY_EDITOR
    // use this for initialization
    async void Start()
    {
      socket = new DatagramSocket();
      socket.MessageReceived += Socket_MessageReceived;

      try
      {
      await socket.BindEndpointAsync(null, "24017");
      }
      catch (Exception e)
      {
      Debug.Log(e.ToString());
      Debug.Log(SocketError.GetStatus(e.HResult).ToString());
      return;
      }

    }
#endif

    // Update is called once per frame
    void Update()
    {

    }

#if !UNITY_EDITOR
    private async void Socket_MessageReceived(Windows.Networking.Sockets.DatagramSocket sender,
    Windows.Networking.Sockets.DatagramSocketMessageReceivedEventArgs args)
    {
    //Read the message that was received from the UDP echo client.
    Stream streamIn = args.GetDataStream().AsStreamForRead();
    StreamReader reader = new StreamReader(streamIn);
    string message = await reader.ReadLineAsync();

    Debug.Log("MESSAGE: " + message);
    }
#endif
}

还有nodejs客户端:

var PORT = 24017;
var HOST = '192.168.1.111';

var dgram = require('dgram');
var message = new Buffer('My KungFu is Good!\r\n');

var client = dgram.createSocket('udp4');
client.send(message, 0, message.length, PORT, HOST, function(err, bytes) {
    if (err) throw err;
    console.log('UDP message sent to ' + HOST +':'+ PORT);
    client.close();
});

服务器运行没有问题,它等待来自服务器的消息。当我启动客户端时,我收到确认消息已成功发送的消息。但是服务器端永远不会收到客户端的消息,控制台中没有错误,就像没有发送一样。我真的不知道在哪里可以找到解决方案... 非常感谢您的帮助。

【问题讨论】:

  • Hololens UWP 应用中的功能配置是什么?我们应该启用 Internet(客户端)功能。查看 Package.appxmanifest 文件
  • 统一我检查了“私有网络(客户端和服务器)”,但也许我需要手动编辑清单文件?谢谢!
  • 你解决了吗?我有一个 UDP 服务器 (UWP),它运行良好并且确实在网络上发送消息。然而,Hololens 并没有捡起任何东西。就好像它被某些防火墙阻止了一样。我什至已经向 Wireshark 确认,一切正常。除了愚蠢的 Hololens。

标签: sockets unity3d server udpclient hololens


【解决方案1】:
  • Internet 客户端/服务器功能适用于 Internet,但不适用于本地网络。
  • 专用客户端/服务器功能适用于本地网络,但不适用于 Internet。

您可能至少需要第二个。 在 WSA 平台的 Player Settings 中,向下滚动到 Capabilities 面板,选中所需的,Unity 将自动生成相应的清单文件。在 Capabilities 面板中向下滚动以了解更多信息。

【讨论】:

  • 嗨 RCYR。感谢您的回复。我完全像这样设置清单文件。但是没有收到任何消息......我尝试使用不同的端口但没有成功......
【解决方案2】:

在 Hololens 上运行时,UDP 堆栈中的某处存在一个错误,要求您在接收消息之前发送任何数据。我以为我可以通过 UdpClient 得到这个发现,但只读取了一些内容就停止了 - 没有进一步探索。

发现here & gist

以上要点的逐字代码:

var portStr = "3109";
socket = new DatagramSocket();
socket.MessageReceived += _Socket_MessageReceived;
await _Socket.BindServiceNameAsync(portStr);

// Unclear the need for this            
await Task.Delay(3000);

// send out a message, otherwise receiving does not work ?!
var outputStream = await _Socket.GetOutputStreamAsync(new HostName("255.255.255.255"), portStr);
DataWriter writer = new DataWriter(outputStream);
writer.WriteString("Hello World!");
await writer.StoreAsync();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-10
    • 2021-01-09
    • 2020-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-12
    相关资源
    最近更新 更多