gpt4 book ai didi

c# - Unity UDP - 不能同时发送和接收?

转载 作者:太空宇宙 更新时间:2023-11-03 14:51:35 27 4
gpt4 key购买 nike

我一直在尝试将玩家矢量的坐标发送到用 NodeJS 编写的 UDP 服务器。但是,当我尝试发送坐标并接收修改后的坐标时,什么也没有显示。如果我修改服务器以不断向我发送信息,它就可以工作。好像我不能同时发送和接收。

using System.Collections.Generic;
using UnityEngine;
using System;
using System.Collections;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;


public class PlayerBehavior : MonoBehaviour {

private UdpClient udpServer;
public GameObject cube;
private Vector3 tempPos;
private Thread t;
public float movementSpeed;
private long lastSend;
private IPEndPoint remoteEP;

void Start()
{
udpServer = new UdpClient(41234);
t = new Thread(() => {
while (true) {
this.receiveData();
}
});
t.Start();
t.IsBackground = true;
remoteEP = new IPEndPoint(IPAddress.Parse("46.101.102.243"), 41234);
}


private long UnixTimeNow()
{
var timeSpan = (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0));
return (long)timeSpan.TotalMilliseconds;
}

private void OnApplicationQuit()
{
udpServer.Close();
t.Abort();
}

void Update()
{
var isShift = Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift);
if (isShift)
{
Debug.Log("Shift");
}
var x = Input.GetAxis("Horizontal") * Time.deltaTime * this.movementSpeed;
var z = Input.GetAxis("Vertical") * Time.deltaTime * this.movementSpeed;
cube.transform.Translate(x, 0, 0);
cube.transform.Translate(0, 0, z);
if (cube.transform.position != tempPos)
{
if (UnixTimeNow() - this.lastSend > 1000 / 24)
{
this.lastSend = UnixTimeNow();
byte[] arr = Encoding.ASCII.GetBytes(cube.transform.position.x + ";" + cube.transform.position.y + ";" + cube.transform.position.z);
udpServer.Send(arr, arr.Length, remoteEP);
}
}
tempPos = cube.transform.position;
}

private void receiveData() {
Debug.Log("Trying to receive data...");
byte[] data = udpServer.Receive(ref remoteEP);
if (data.Length > 0)
{
var str = System.Text.Encoding.Default.GetString(data);
Debug.Log("Received Data" + str);
}
}

}

如果有人有一些意见或可能的解决方案,我们将不胜感激。几乎到处搜索,但似乎没有任何相应的工作。

编辑我还尝试使用不同的 UpdClients 进行接收和发送,以及 IP 端点。问题仍然存在。

public class PlayerBehavior : MonoBehaviour {

private UdpClient udpServer;
private UdpClient udpServer2;
public GameObject cube;
private Vector3 tempPos;
private Thread t;
public float movementSpeed;
private long lastSend;
private IPEndPoint remoteEP;
private IPEndPoint remoteEP2;

void Start()
{
udpServer = new UdpClient();
remoteEP = new IPEndPoint(IPAddress.Parse("46.101.102.243"), 41234);
remoteEP2 = new IPEndPoint(IPAddress.Parse("46.101.102.243"), 41234);
udpServer.Connect(remoteEP);

udpServer2 = new UdpClient();
udpServer2.Connect(remoteEP2);

t = new Thread(() => {
while (true) {
this.receiveData();
}
});
t.Start();
t.IsBackground = true;

}


private long UnixTimeNow()
{
var timeSpan = (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0));
return (long)timeSpan.TotalMilliseconds;
}

private void OnApplicationQuit()
{
udpServer.Close();
t.Abort();
}

void Update()
{
var isShift = Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift);
if (isShift)
{
Debug.Log("Shift");
}
var x = Input.GetAxis("Horizontal") * Time.deltaTime * this.movementSpeed;
var z = Input.GetAxis("Vertical") * Time.deltaTime * this.movementSpeed;
cube.transform.Translate(x, 0, 0);
cube.transform.Translate(0, 0, z);
if (cube.transform.position != tempPos)
{
if (UnixTimeNow() - this.lastSend > 1000 / 24)
{
this.lastSend = UnixTimeNow();
byte[] arr = Encoding.ASCII.GetBytes(cube.transform.position.x + ";" + cube.transform.position.y + ";" + cube.transform.position.z);
udpServer.Send(arr, arr.Length);
}
}
tempPos = cube.transform.position;
}

private void receiveData() {
Debug.Log("Trying to receive data...");
byte[] data = udpServer2.Receive(ref remoteEP2);
if (data.Length > 0)
{
var str = System.Text.Encoding.Default.GetString(data);
Debug.Log("Received Data: " + str);
}
}

}

最佳答案

可以同时发送和接收,但不要双向使用同一个端口

关于c# - Unity UDP - 不能同时发送和接收?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51537059/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com