Hololens2 cannot synchronize messages when using Socket communication

question

When Hololens uses Socket communication, the glasses end cannot synchronize messages, but it can be used normally when using UNet communication

solve:

It is found that HoloLens cannot perform synchronous communication, and it is necessary to change synchronous communication to asynchronous communication:

//同步通信
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socket.Connect(ipAddress, port);  // 同步连接
byte[] buffer = new byte[1024];
int bytesRead = socket.Receive(buffer);  // 同步接收
socket.Send(data);  // 同步发送

Change to:

Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socket.BeginConnect(ipAddress, port, ConnectCallback, socket);  // 异步连接
private void ConnectCallback(IAsyncResult result)
{
    
    
    Socket socket = (Socket)result.AsyncState;
    socket.EndConnect(result);
}
byte[] buffer = new byte[1024];
socket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, ReceiveCallback, socket);  // 异步接收
private void ReceiveCallback(IAsyncResult result)
{
    
    
    Socket socket = (Socket)result.AsyncState;
    int bytesRead = socket.EndReceive(result);
}
socket.BeginSend(data, 0, data.Length, SocketFlags.None, SendCallback, socket);  // 异步发送
private void SendCallback(IAsyncResult result)
{
    
    
    Socket socket = (Socket)result.AsyncState;
    int bytesSent = socket.EndSend(result);
}

Guess you like

Origin blog.csdn.net/KIDS333/article/details/131293772