Hololens TCP套接字 - Python客户端连接Hololens服务器

6

经过几周的挫败,我终于能够从Python客户端向Hololens服务器发送字符串。以下是代码并且完美运行。然而,我在想如果有经验丰富的套接字专家能否帮助我修改此代码以便从Python发送openCV摄像头帧(基本上只是发送一张图片)到Hololens。我一直在尝试,但似乎C#脚本中没有接收到图像数据。

非常感谢您的帮助。

Hololens服务器代码:

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

#if !UNITY_EDITOR
    using Windows.Networking;
    using Windows.Networking.Sockets;
    using Windows.Storage.Streams;
#endif

//Able to act as a reciever 
public class UniversalSampleTutorial : MonoBehaviour
{
    public String _input = "Waiting";

#if !UNITY_EDITOR
        StreamSocket socket;
        StreamSocketListener listener;
        String port;
        String message;
#endif

    // Use this for initialization
    void Start()
    {
#if !UNITY_EDITOR
        listener = new StreamSocketListener();
        port = "9090";
        listener.ConnectionReceived += Listener_ConnectionReceived;
        listener.Control.KeepAlive = false;

        Listener_Start();
#endif
    }

#if !UNITY_EDITOR
    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");

        try
        {
            while (true) {

                    using (var dw = new DataWriter(args.Socket.OutputStream))
                    {
                        dw.WriteString("Hello There");
                        await dw.StoreAsync();
                        dw.DetachStream();
                    }  

                    using (var dr = new DataReader(args.Socket.InputStream))
                    {
                        dr.InputStreamOptions = InputStreamOptions.Partial;
                        await dr.LoadAsync(12);
                        var input = dr.ReadString(12);
                        Debug.Log("received: " + input);
                        _input = input;

                    }
            }
        }
        catch (Exception e)
        {
            Debug.Log("disconnected!!!!!!!! " + e);
        }

    }

#endif

    void Update()
    {
        this.GetComponent<TextMesh>().text = _input;
    }
}

Python客户端(需要从此处发送图像而不是字符串)

import asyncio

#THIS CODE WORKS SENDING STRING MESSAGE TO HOLOLENS

async def tcp_echo_client(message, loop):
    reader, writer = await asyncio.open_connection('192.168.1.110', 9090, loop=loop)

    print('Send: %r' % message)
    writer.write(message.encode())

    data = await reader.read(100)
    print('Received: %r' % data.decode())

    print('Close the socket')
    writer.close()


message = 'hello there frend'
loop = asyncio.get_event_loop()
loop.run_until_complete(tcp_echo_client(message, loop))
loop.close()

编辑:这是我一直在努力编写的图像代码。它还没有运行,我不确定是否正确接收了字节数组:https://gist.github.com/jryebread/3961e890375fcc8a64c8ac3d279ec9fa

1个回答

2

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接