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.

Can't Read Data from external Server to Hololens

I have written some code to transfer data from external server to Hololens. I am able to connect Hololens to the server. But I am facing problem in sending the data from server to Hololens. Whenever I call ReadData function it isn't even connected(it prints Not connected).
I am quite new to c# and unity and isn't able to sort out this matter yet.
I am using StreamSocket and DataReader classes to connect and read the data respectively. Function Connect() connects with the server in start() method and then I call ReadData function to get the data from the server. I am attaching my code file.
Can you help me out in solving my problem Thanks in advance.

`

using System.Collections;
using System.Collections.Generic;
using System.Net.Sockets;
using System.Threading;
using System.Text;
using System.Net;
using System;
using UnityEngine;

if !UNITY_EDITOR && UNITY_METRO

using Windows.Networking.Sockets;
using Windows.Storage.Streams;
using Windows.Networking;
using Windows.Foundation;

endif

public class TCPclientRead : MonoBehaviour
{
public string ServerIP = "10.1.2.35";

[Tooltip("The connection port on the machine to use.")]
public int ConnectionPort = 11000;

private string data = "connected" ;

public TextMesh mesh;
private bool connected = false;

if !UNITY_EDITOR && UNITY_METRO

private StreamSocket networkConnection;

    /// <summary>
    /// Temporary buffer for the data we are recieving.
    /// </summary>
//private byte[] dataBuffer;


public void Connect( )
{
      // Setup a connection to the server.
           HostName networkHost = new HostName(ServerIP.Trim());

          //HostName networkHost = new HostName( IPAddress.Any.ToString());    
          networkConnection = new StreamSocket();

        // Connections are asynchronous.  
        // !!! NOTE These do not arrive on the main Unity Thread. Most Unity operations will throw in the callback !!!
        IAsyncAction outstandingAction = networkConnection.ConnectAsync(networkHost, ConnectionPort.ToString());
        AsyncActionCompletedHandler aach = new AsyncActionCompletedHandler(NetworkConnectedHandler);
        outstandingAction.Completed = aach;    
}

public void NetworkConnectedHandler(IAsyncAction asyncInfo, AsyncStatus status)
{
        if (status == AsyncStatus.Completed)
        {
            connected = true;
             // Here Just display connected   

        }
        else
        {
            connected = false;    
            Debug.Log("Failed to establish connection. Error Code: " + asyncInfo.ErrorCode);
            // In the failure case we'll requeue the data and wait before trying again.
            networkConnection.Dispose();

        }    

}


public void ReadData()
{
    if(connected)
    {  
        DataReader reader = new DataReader(networkConnection.InputStream);
        reader.InputStreamOptions = InputStreamOptions.Partial;
        reader.LoadAsync(512);
        data = reader.ReadString(512);

     }

}


private void Start()
{
    mesh = gameObject.GetComponent<TextMesh>(); 
    Connect();
}


private void Update()
{

    if (connected)
    {     
        mesh.text = data;


    } 
     else
        mesh.text = "Not Connected";
     ReadData();
}

endif

}
`

Answers

Sign In or Register to comment.