2

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.

Silvering
  • 756
  • 1
  • 6
  • 33
  • What's the capability configuration in your Hololens UWP app? We should enable the Internet (Client) capability. Check out the Package.appxmanifest file – Franklin Chen - MSFT May 20 '17 at 06:26
  • In unity I checked "Private Networks (Client & Server)" but maybe I need to edit manually the manifest file? Thanks! – Silvering May 21 '17 at 09:48
  • Did you solve this? I have a UDP server (UWP) which works perfectly and does send messages on the network. However the Hololens does not pick anything up. It is as if it is being blocked by some firewall. I have even confirmed with Wireshark, everything works. Except the stupid Hololens. – sebrock Jul 02 '17 at 18:30

2 Answers2

0
  • Internet Client/Server capability is for Internet but not local network.
  • Private Client/Server capability is for local network but not Internet.

You need probably the second one at least. In Player Settings for WSA platform, scroll down to the Capabilities panel, check required ones and Unity will automatically generate the manifest file accordingly. Scroll down within the Capabilities panel itself as well for more.

RCYR
  • 1,452
  • 13
  • 28
  • Hi RCYR. Thanks for your reply. I set exactly the manifest file like this. However no message received.... I tried with different ports without success... – Silvering May 29 '17 at 06:47
0

When running on the Hololens, there is a bug somewhere in the UDP stack that requires you to send any data out before being able to receive messages. I thought I was able to get this finding working with UdpClient, but only got a few reads out and it stopped - didn't explore further.

As discovered here & gist.

Code verbatim from above 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();
fionbio
  • 3,368
  • 2
  • 23
  • 38