Hello everyone.

The Mixed Reality Forums here are no longer being used or maintained.

There are a few other places we would like to direct you to for support, both from Microsoft and from the community.

The first way we want to connect with you is our mixed reality developer program, which you can sign up for at https://aka.ms/IWantMR.

For technical questions, please use Stack Overflow, and tag your questions using either hololens or windows-mixed-reality.

If you want to join in discussions, please do so in the HoloDevelopers Slack, which you can join by going to https://aka.ms/holodevelopers, or in our Microsoft Tech Communities forums at https://techcommunity.microsoft.com/t5/mixed-reality/ct-p/MicrosoftMixedReality.

And always feel free to hit us up on Twitter @MxdRealityDev.
Options

Socket communication between Hololens and PC

I am trying to communicate between Hololens and PC through UDP socket.
It worked well between PC and a UWP app,
but if I run almost the same code between PC and a Hololens app,
The hololens app doesn't receive the data and the app turns itself off.
I'm not sure, but it might be because of the way strings are handled.
If you know the reason, and if you know the solution, please let me know.
Thanks.

client>>

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.XR.WSA.Input;

# if !UNITY_EDITOR && UNITY_METRO
using Windows.Networking;
using Windows.Networking.Sockets;
using Windows.UI.Core;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.Foundation;
#endif

public class MessageManager : MonoBehaviour
{
    GestureRecognizer _gestureRecognizer;
    bool _isConnected = false;
    public Text _subtitleText;
    public Text _logDisplay;

#if !UNITY_EDITOR && UNITY_METRO
    private HostName serverHost = new HostName("127.0.0.1"); 
    private DatagramSocket socket = new DatagramSocket(); 

    private string serverPort = "9001";
    private string clientPort = "9002";

    private Windows.UI.Core.CoreDispatcher dispatcher = Windows.UI.Core.CoreWindow.GetForCurrentThread().Dispatcher;


    void Start () {
        _gestureRecognizer = new GestureRecognizer();
        _gestureRecognizer.TappedEvent += _gestureRecognizer_TappedEvent;
        _gestureRecognizer.StartCapturingGestures();
    }

    private async void _gestureRecognizer_TappedEvent(InteractionSourceKind source, int tapCount, Ray headRay)
    {
        if(!_isConnected)
        {
            try
            {
                socket.MessageReceived += Socket_MessageReceived; 
                await socket.BindServiceNameAsync(clientPort); 
                await socket.ConnectAsync(serverHost, serverPort); 
                string message = "Hello from the client";
                Send_messageAsync(socket, message);    
                _isConnected = true; 
                this._logDisplay.text = "Server Connected";

            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.ToString()); 
                this._logDisplay.text = "Exception1: " + ex.Message;
            }
        }
        else
        {
            try
            {              
                string new_message = this._subtitleText.text;
                Send_messageAsync(socket, new_message);                
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.ToString()); 
                this._logDisplay.text = "Exception2: " + ex.Message;
            }
        }
     }

    private async void Socket_MessageReceived(DatagramSocket sender, DatagramSocketMessageReceivedEventArgs args)
    {        
        try
        {
            Stream streamIn = args.GetDataStream().AsStreamForRead();            
            StreamReader reader = new StreamReader(streamIn); 
            string message = await reader.ReadLineAsync(); 
            await dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>            
            {
            this._subtitleText.text = "UDP server said: " + message;
            });
        }
        catch (Exception e)
        {
            System.Diagnostics.Debug.WriteLine(e.ToString()); 
            this._logDisplay.text = "Exception3: " + e.Message;
        }
    }

    private async void Send_messageAsync(DatagramSocket socket, string message)
    {
            try
            {
                Stream streamOut = (await socket.GetOutputStreamAsync(serverHost, serverPort)).AsStreamForWrite();
                StreamWriter writer = new StreamWriter(streamOut); 
                await writer.WriteLineAsync(message); 
                await writer.FlushAsync(); 
            }
            catch (Exception e)
            {
                System.Diagnostics.Debug.WriteLine(e.ToString()); 
                this._logDisplay.text = "Exception4: " + e.Message;
            }           
    }

#endif
}

server>>

    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Net;
    using System.Net.Sockets;

    namespace server_udp
    {
        class Program
        {
            static void Main(string[] args)
            {
                int recv; 
                byte[] data = new byte[1024];

                // Get local IP
                IPEndPoint ip = new IPEndPoint(IPAddress.Any, 9001); 
                Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); 

                // Bind IP
                socket.Bind(ip);
                Console.WriteLine("This is a server, Host name is {0}", Dns.GetHostName());

                // Waiting for client
                Console.WriteLine("Waiting for a client");

                // Get the client IP
                IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0); 
                EndPoint Remote = (EndPoint)sender;
                recv = socket.ReceiveFrom(data, ref Remote); 
                Console.WriteLine("Message received from {0}, {1} bytes.", Remote.ToString(), recv);
                Console.WriteLine(Encoding.UTF8.GetString(data, 0, recv)); 

                string welcome = Console.ReadLine();

                try
                {
                    data = Encoding.UTF8.GetBytes(welcome);                 
                    socket.SendTo(data, data.Length, SocketFlags.None, Remote); 
                }
                catch (Exception e)
                {
                    Console.WriteLine("Exception: " + e.Message);
                }

                while (true) 
                {
                    data = new byte[1024]; 
                    recv = socket.ReceiveFrom(data, ref Remote); 
                    Console.WriteLine(Encoding.UTF8.GetString(data, 0, recv)); 
                    data = Encoding.UTF8.GetBytes(Console.ReadLine()); 
                    socket.SendTo(data, data.Length, SocketFlags.None, Remote); 
                }
            }
        }
    }

I used the source code of this link

Tagged:

Answers

Sign In or Register to comment.