Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

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

Discussion in 'AR/VR (XR) Discussion' started by Dustin2711, Aug 24, 2018.

  1. Dustin2711

    Dustin2711

    Joined:
    Apr 12, 2018
    Posts:
    8
    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:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Net;
    3. #if !UNITY_EDITOR
    4. using System;
    5. using System.Threading.Tasks;
    6. using System.IO;
    7. using Windows.Networking;
    8. using Windows.Networking.Sockets;
    9. using Windows.Storage.Streams;
    10. #endif
    11.  
    12. public static class Receiver
    13. {
    14.     static int recvPort = 1337;
    15.     static int sendPort = 1338;
    16.     public static string log = "";
    17.     static DatagramSocket socket;
    18.     static DataWriter writer;  
    19. #if !UNITY_EDITOR
    20.     static DatagramSocket socket;
    21.     static DataWriter writer;      
    22.  
    23.     public static async void Initialize() # gets invoked from outside
    24.     {
    25.         HostName recvHostname = new HostName("192.168.0.148");
    26.         HostName sendHostname = new HostName("192.168.0.112"); //109 is lan, 112 is wlan
    27.         socket = new DatagramSocket();
    28.         socket.MessageReceived += MessageReceived;
    29.         await socket.BindEndpointAsync(recvHostname, recvPort.ToString()); //lokaler Empfangs-Endpunkt und Port
    30.         writer = new DataWriter(await
    31.             socket.GetOutputStreamAsync(sendHostname, sendPort.ToString())); //erstellt Writer für Output      
    32.         Monitor.status += "Initialized."; # this gets invoked
    33.         Send(new byte[] { 0x60, 0x70 }); # sending test (fails)
    34.     }
    35.     public static async void Send(byte[] bytes)
    36.     {
    37.         writer.WriteBytes(bytes);
    38.         await writer.StoreAsync();
    39.     }
    40.     private static void MessageReceived(DatagramSocket sender, DatagramSocketMessageReceivedEventArgs args)
    41.     {
    42.         Monitor.status= "msg rcv"; #gets never invoked
    43.     }
    44. #endif
    45. }
    The method Initialize() works completely but when I send a message, "MessageReceived" is not invoked.

    Kind regards
     
  2. Dustin2711

    Dustin2711

    Joined:
    Apr 12, 2018
    Posts:
    8
    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.
     
  3. chiarapalu

    chiarapalu

    Joined:
    Nov 5, 2019
    Posts:
    3
    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?