unity udp dgram 套接字不适用于全息镜头

unity udp dgram socket not working on hololens

我尝试在 hololens 和 python 服务器之间建立连接。所以我使用了 dgram 套接字,但这不适用于全息镜头。 这是我的代码示例。

hololens 客户端

public string conHost = "192.168.0.58";
public int conPort = 3174;
 void Start()
    {
            ipep = new IPEndPoint(IPAddress.Parse(conHost), conPort);            
            clientThread = new Thread(setupSocket);
            clientThread.Start();
    }
public void setupSocket()
    {
        mySocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);


    }
 void Update()
    {
        if(Send)
        {
                Send = false;                   
                byte[] bytes = Encoding.UTF8.GetBytes("124306324602435");
                byte[] buffer = Encoding.UTF8.GetBytes(bytes.Length.ToString());                   
                mySocket.SendTo(buffer, buffer.Length, SocketFlags.None, ipep);
        }
    }

python 服务器

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
#server_address = "192.168.0.18", 9029
#print('starting up on {} port {}'.format(*server_address))
sock.bind(("192.168.0.58", 3174))

ready=True
while(ready):        
    try:  
        epoch_time = time.time()
        data,address = sock.recvfrom(100)        
        fileLength = data.decode("utf-8")
        print(fileLength)
    except:                
        continue

当我在unity项目中播放这段代码时,它起作用了。但在全息镜中,不起作用。

我使用了 try-catch,我在更新时收到了这个错误消息 System.NullReferenceException: 对象引用未设置到 <000000000000000000000000000000000> 中对象 [0x00000] 的实例:0

这是因为变量mySocket没有被初始化为期望值。实际上,System.Threading 的实现已在 .NET 4.x 中以不向后兼容的方式发生变化,并且 HoloLens 应用程序使用 IL2CPP 脚本后端和 .NET4.x 但 Unity 编辑器使用Mono 脚本后端 with.NET2.x。因此,它在 Unity 编辑器中工作但无法在 .NET 中启动您的线程4.x,我们建议使用 System.Threading.Tasks class instead. Besides, to use WinRT APIs in Unity projects built for the UWP you need to use preprocessor directives, more information please see:WinRT APIs with Unity for HoloLens