Unity UDP-无法同时发送和接收?

时间:2018-07-26 10:50:03

标签: c# unity3d udp

我一直在尝试将玩家矢量的坐标发送到用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);
            }
    }

}

1 个答案:

答案 0 :(得分:0)

您可以同时发送和接收,但不要在两个方向上使用相同的端口