Search code examples
uwptcpclienthololens

Client side app Hololens


I'm trying to make a client side app to my Hololens. Here is my code

using UnityEngine;
using System.Collections;
using System;
using System.IO;
using System.Net.Sockets;



public class networkSocket : MonoBehaviour
{
    public String host = "127.0.0.1";
    public Int32 port = 8030;

    internal Boolean socket_ready = false;
    internal String input_buffer = "";
    TcpClient tcp_socket;
    NetworkStream net_stream;

    StreamWriter socket_writer;
    StreamReader socket_reader;



    void Update()
    {
        string received_data = readSocket();
        string key_stroke = Input.inputString;

        // Collects keystrokes into a buffer
        if (key_stroke != ""){
            input_buffer += key_stroke;

            if (key_stroke == "\n"){
                // Send the buffer, clean it
                Debug.Log("Sending: " + input_buffer);
                writeSocket(input_buffer);
                input_buffer = "";
            }

        }


        if (received_data != "")
        {
            // Do something with the received data,
            // print it in the log for now
            Debug.Log(received_data);
        }
    }


    void Awake()
    {
        setupSocket();
    }

    void OnApplicationQuit()
    {
        closeSocket();
    }

    public void setupSocket()
    {
        try
        {
            tcp_socket = new TcpClient(host, port);

            net_stream = tcp_socket.GetStream();
            socket_writer = new StreamWriter(net_stream);
            socket_reader = new StreamReader(net_stream);

            socket_ready = true;
        }
        catch (Exception e)
        {
            // Something went wrong
            Debug.Log("Socket error: " + e);
        }
    }

    public void writeSocket(string line)
    {
        if (!socket_ready)
            return;

        line = line + "\r\n";
        socket_writer.Write(line);
        socket_writer.Flush();
    }

    public String readSocket()
    {
        if (!socket_ready)
            return "";

        if (net_stream.DataAvailable)
            return socket_reader.ReadLine();

        return "";
    }

    public void closeSocket()
    {
        if (!socket_ready)
            return;

        socket_writer.Close();
        socket_reader.Close();
        tcp_socket.Close();
        socket_ready = false;
    }

}

However I receive errors like : The type or namespace name 'TcpClient' could not be found and the same for 'NetworkStream'

I guess that the main issue is that Hololens doesn't support these libraries because of UWP. If someone already tried to do such a thing ? Thanks in advance!


Solution

  • We had the same problem during a project. We solved this by commenting out the code, then build in Unity. Before deploying it onto the HoloLens we decommented the code. The problem is that Unity does not use .net.