客户端应用Hololens

时间:2017-02-02 14:26:24

标签: uwp tcpclient hololens

我正在尝试为我的Hololens制作一个客户端应用程序。这是我的代码

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;
    }

}

但是我收到的错误如下:找不到类型或命名空间名称'TcpClient',而'NetworkStream'也是如此

我想主要的问题是,由于UWP,Hololens不支持这些库。 如果有人已经尝试过做这样的事情? 提前谢谢!

1 个答案:

答案 0 :(得分:0)

我们在项目期间遇到了同样的问题。我们通过注释掉代码然后在Unity中构建来解决这个问题。在将其部署到HoloLens之前,我们对代码进行了解除注释。问题是Unity不使用.net。