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.

TCP

Ok this is my code for TCP socket that should run on Hololens. The problem in my code is that the last message I can see is Debug.Log("Connect"); and on the server I see no connection. Someone can tell me why?

public class UWPTcpClient : MonoBehaviour
{

public delegate void OnReadFinished(string result);
public event OnReadFinished ReadFinishedEvent;

public delegate void OnConnectFinished();
public event OnConnectFinished ConnectFinishedEvent;

private StreamSocket socket;
public bool isConnected = false;

public void Connect(string host, int port) {
     Debug.Log("Connect");
    var task = Task.Run(async () => { await ConnectAsync(host, port); });
}

private async Task ConnectAsync(string host, int port)
{
    Debug.Log("ConnectAsynch");
    socket = new StreamSocket();
    HostName hostName = new HostName(host);

    // Set NoDelay to false so that the Nagle algorithm is not disabled
    socket.Control.NoDelay = false;

    try
    {
        await socket.ConnectAsync(hostName, port.ToString());
        isConnected = true;
Debug.Log("Connected.");
    }
    catch (Exception exception)
    {
        switch (SocketError.GetStatus(exception.HResult))
        {
            case SocketErrorStatus.HostNotFound:
                Debug.Log("Host not found");
                throw;
            default:
                Debug.Log("exception on socket connection: " + exception.Message);
                throw;
        }
    }

    ConnectFinishedEvent();
}

public void Read() {

Debug.Log("Read()");
Task.Run(async () => { await ReadAsync(); });
}

private async Task ReadAsync()
{
    if (! isConnected)
    {
        return;
    }

    DataReader reader;
    StringBuilder strBuilder;

    using (reader = new DataReader(socket.InputStream))
    {
        strBuilder = new StringBuilder();
        reader.InputStreamOptions = InputStreamOptions.Partial;
        reader.UnicodeEncoding = Windows.Storage.Streams.UnicodeEncoding.Utf8;
        reader.ByteOrder = ByteOrder.LittleEndian;

        var count = await reader.LoadAsync(1024);
        if (count > 0)
        {
            strBuilder.Append(reader.ReadString(count));
            reader.DetachStream(); 
            ReadFinishedEvent(strBuilder.ToString());
        } else
        {
            ReadFinishedEvent("");
        }
    }
}

public void Close()
{
    socket.Dispose();
}

}

endif

Tagged:
Sign In or Register to comment.