我正在创建一个带有统一的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();
});
服务器运行没有问题,它等待来自服务器的消息。当我启动客户端时,我收到消息,确认消息已成功发送。但是,服务器端从不接收客户端的消息,控制台中没有错误,就像没有发送一样。我真的不知道在哪里找到解决方案...... 非常感谢你的帮助。
答案 0 :(得分:0)
至少你可能需要第二个。 在WSA平台的Player Settings中,向下滚动到Capabilities面板,检查所需的面板,Unity将自动生成相应的清单文件。在Capabilities面板中向下滚动以及更多内容。
答案 1 :(得分:0)
在Hololens上运行时,UDP堆栈中的某个错误要求您先发送所有数据,然后才能接收消息。我以为我可以与UdpClient
一起使用,但是只读出了几句话而停止了–没有进一步探索。
以上要点上的逐字代码:
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();