Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

(UDP) Can't send and receive at the same time?

Discussion in 'Scripting' started by williampire, Jul 26, 2018.

  1. williampire

    williampire

    Joined:
    Jul 24, 2018
    Posts:
    9
    I've written the following:


    Code (CSharp):
    1. using System.Collections.Generic;
    2. using UnityEngine;
    3. using System;
    4. using System.Collections;
    5. using System.Net;
    6. using System.Net.Sockets;
    7. using System.Text;
    8. using System.Threading;
    9.  
    10.  
    11. public class PlayerBehavior : MonoBehaviour {
    12.  
    13.     private UdpClient udpServer;
    14.     public GameObject cube;
    15.     private Vector3 tempPos;
    16.     private Thread t;
    17.     public float movementSpeed;
    18.     private long lastSend;
    19.     private IPEndPoint remoteEP;
    20.  
    21.     void Start()
    22.     {
    23.         udpServer = new UdpClient(3000);
    24.         t = new Thread(() => {
    25.             while (true) {
    26.                 this.receiveData();
    27.             }
    28.         });
    29.         t.Start();
    30.         t.IsBackground = true;
    31.         remoteEP = new IPEndPoint(IPAddress.Parse("46.101.102.243"), 41234);
    32.     }
    33.  
    34.  
    35.     private long UnixTimeNow()
    36.     {
    37.         var timeSpan = (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0));
    38.         return (long)timeSpan.TotalMilliseconds;
    39.     }
    40.  
    41.     private void OnApplicationQuit()
    42.     {
    43.         udpServer.Close();
    44.         t.Abort();
    45.     }
    46.  
    47.     void Update()
    48.     {
    49.         var isShift = Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift);
    50.         if (isShift)
    51.         {
    52.             Debug.Log("Shift");
    53.         }
    54.         var x = Input.GetAxis("Horizontal") * Time.deltaTime * this.movementSpeed;
    55.         var z = Input.GetAxis("Vertical") * Time.deltaTime * this.movementSpeed;
    56.         cube.transform.Translate(x, 0, 0);
    57.         cube.transform.Translate(0, 0, z);
    58.         if (cube.transform.position != tempPos)
    59.         {
    60.             if (UnixTimeNow() - this.lastSend > 1000 / 24)
    61.             {
    62.                 this.lastSend = UnixTimeNow();
    63.                 byte[] arr = Encoding.ASCII.GetBytes(cube.transform.position.x + ";" + cube.transform.position.y + ";" + cube.transform.position.z);
    64.                 udpServer.Send(arr, arr.Length, remoteEP);
    65.             }
    66.         }
    67.         tempPos = cube.transform.position;
    68.     }
    69.  
    70.     private void receiveData() {
    71.             Debug.Log("Trying to receive data...");
    72.             byte[] data = udpServer.Receive(ref remoteEP);
    73.             if (data.Length > 0)
    74.             {
    75.                 var str = System.Text.Encoding.Default.GetString(data);
    76.                 Debug.Log("Received Data" + str);
    77.             }
    78.     }
    79.    
    80. }
    81.  
    However, I can only receive data if I don't send it. If I just receive without sending, it works fine, however if I want to send my current coordinates to the server, and receive it back again, it won't work. The server sends the package just fine, but something seems to be wrong with the client.