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

Use StreamSocketListener in Hololens-App with Unity

Hi,
I'm trying to build a very basic network-Connection with a StreamSocketListener.
Now I get an error in Unity: "Assets/Scripts/Networking.cs(27,17): error CS1644: Feature `asynchronous functions' cannot be used because it is not part of the C# 4.0 language specification".

I'm new to Hololens-Development, so maybe it's just a simple trick, but I can't really figure it out...

This is my script:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;
using UnityEngine;
using Windows.Networking;
using Windows.Networking.Sockets;
using Windows.Storage.Streams;

public class Networking : MonoBehaviour {

    StreamSocket socket;
    StreamSocketListener listener;
    String port;

// Use this for initialization
void Start () {

    listener = new StreamSocketListener();
    port = "8888";
    listener.ConnectionReceived += Listener_ConnectionReceived;
    listener.Control.KeepAlive = false;

    Listener_Start();
}

private async void Listener_Start()
{
    Debug.Log("Listener started");
    try
    {
        await listener.BindServiceNameAsync(port);
    }
    catch (Exception e)
    {
        Debug.Log("Error: " + e.Message);
    }

    Debug.Log("Listening");
}

private async void Listener_ConnectionReceived(StreamSocketListener sender, StreamSocketListenerConnectionReceivedEventArgs args)
{
    Debug.Log("Connection received");
    DataReader reader = new DataReader(args.Socket.InputStream);
    try
    {
        while (true)
        {
            // Read first 4 bytes (length of the subsequent string). 
            uint sizeFieldCount = await reader.LoadAsync(sizeof(uint));
            if (sizeFieldCount != sizeof(uint))
            {
                // The underlying socket was closed before we were able to read the whole data. 
                return;
            }

            // Read the string. 
            uint stringLength = reader.ReadUInt32();
            uint actualStringLength = await reader.LoadAsync(stringLength);
            if (stringLength != actualStringLength)
            {
                // The underlying socket was closed before we were able to read the whole data.
                return;
            }

            // dump data
            Debug.Log("Received: " + reader.ReadString(actualStringLength));
        }
    }
    catch (Exception exception)
    {
        // If this is an unknown status it means that the error is fatal and retry will likely fail. 
        if (SocketError.GetStatus(exception.HResult) == SocketErrorStatus.Unknown)
        {
            throw;
        }

        // dump data
        Debug.Log("Read Stream failed: " + exception.Message);
    }
}

// Update is called once per frame
void Update () {

}

}

Best Answer

Answers

Sign In or Register to comment.