Unity UDP - Can't send and receive at the same time?

Multi tool use
Unity UDP - Can't send and receive at the same time?
I've been trying to send coordinates of the players vector to the UDP server which is written in NodeJS. However, when I try to send the coordinates and receive them modified afterwards nothing shows. If I modify the server to send me info constantly, it works. Seems as if I can't send and receive at the same time.
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);
}
}
}
Would be thankful if anyone has some input or possible solutions. Searched almost everywhere but nothing seems to work accordingly.
1 Answer
1
You can send and recieve at the same time, but do not use the same port for both directions
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.