我目前正在一个项目中,我必须从Jetson TX1向PC发送和接收帧。 目前,我有一个在github上找到的客户,但问题出在接待部分。 框架必须统一存放,因为它们必须显示在VR头盔中。目前,这是我用于接收和显示框架的代码:
using System.Net;
using System.Net.Sockets;
using UnityEngine;
using System;
public class Test : MonoBehaviour
{
Socket socket;
int port = 5356;
private bool stopping = false;
byte[] buffer;
int recvMasgSize;
Texture2D texture;
Renderer renderer;
private object e;
void Start()
{
renderer = new Renderer();
renderer = GetComponent<Renderer>();`enter code here`
texture = new Texture2D(2, 2);
IPEndPoint endpointReceive = new IPEndPoint(IPAddress.Any, port);
socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
socket.Bind(endpointReceive);
socket.Blocking = false;
buffer = new byte[65540];
Debug.Log("start");
}
void Update()
{
do
{
Debug.Log("waiting");
recvMasgSize = socket.Receive(buffer);
} while (recvMasgSize > sizeof(int)); //wait for the first information containing image size
Debug.Log("received");
int total_pack = (int) buffer[0];
Debug.Log(total_pack);
byte[] longbuf = new byte[4096 * total_pack];
for (int i = 0; i< total_pack; i++)
{
recvMasgSize = socket.Receive(buffer);
if(recvMasgSize != 4096)
{
Debug.Log("received unexpected packet size");
continue;
}
Buffer.BlockCopy(longbuf, i * 4096, buffer, 0, 4096);
}
texture.LoadImage(longbuf);
renderer.material.mainTexture = texture;
}
}
将套接字设置为blocking = true只会导致统一崩溃,将其设置为false会生成错误(SocketException,无法完成非阻塞套接字操作。我听说了使用异步方法的可能性,但是由于所有这些都是对我来说真的很新,我真的不知道如何使用它们。 希望有人能提供帮助,以便我继续进行该项目。
谢谢大家。