Hololens UDP Receiver: Actually working code does not work in Unity

Hi there,

I got some code for receiving local UDP messages for HoloLens. I tested it with a Visual Studio UWP application and it worked on HoloLens - I am receiving my messages from PC. But the same code actually does not work in Unity although i made sure to check the capability of private networking. I already HAD a working version of the receiver on my HoloLens but I had to recreate my project due to other reasons and like to know what the problem might be.
I am using the SDK 10.0.16299.0 for both applications.

Here is the important code:

using UnityEngine;
using System.Net;
#if !UNITY_EDITOR
using System;
using System.Threading.Tasks;
using System.IO;
using Windows.Networking;
using Windows.Networking.Sockets;
using Windows.Storage.Streams;
#endif

public static class Receiver
{
    static int recvPort = 1337;
    static int sendPort = 1338;
    public static string log = "";
    static DatagramSocket socket;
    static DataWriter writer;  
#if !UNITY_EDITOR
    static DatagramSocket socket;
    static DataWriter writer;      

    public static async void Initialize() # gets invoked from outside
    {
        HostName recvHostname = new HostName("192.168.0.148");
        HostName sendHostname = new HostName("192.168.0.112"); //109 is lan, 112 is wlan
        socket = new DatagramSocket();
        socket.MessageReceived += MessageReceived;
        await socket.BindEndpointAsync(recvHostname, recvPort.ToString()); //lokaler Empfangs-Endpunkt und Port
        writer = new DataWriter(await
            socket.GetOutputStreamAsync(sendHostname, sendPort.ToString())); //erstellt Writer für Output       
        Monitor.status += "Initialized."; # this gets invoked
        Send(new byte[] { 0x60, 0x70 }); # sending test (fails)
    }
    public static async void Send(byte[] bytes)
    {
        writer.WriteBytes(bytes);
        await writer.StoreAsync();
    }
    private static void MessageReceived(DatagramSocket sender, DatagramSocketMessageReceivedEventArgs args)
    {
        Monitor.status= "msg rcv"; #gets never invoked
    }
#endif
}

The method Initialize() works completely but when I send a message, “MessageReceived” is not invoked.

Kind regards

I solved my issue. The mistake was that “IL2CPP” was selected as scripting backend in the player settings. Changing it to “.NET” made the UDP reception work.

Dear Dustin, I m having the same problem. But I m using unity 2019 were the scripting backend is only IL2CPP. Do you know if I can do anything else to make it work?