I'm making a Hololens app with unity which starts a udp server. This one waits for a message from an external udp client. Here is the server side :
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
}
And the nodejs client side :
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();
});
The server runs without issue, it waits for message from the server. When I start the client, I receive the message which confirms that the message was sent successfully. However the server side never receives the client's message, no errors in the console, like if was not sent. I don't really know where to find a solution... Thanks a lot for your help.