Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Unity Multiplayer Ask about UDP

Discussion in 'Multiplayer' started by Dekata, Dec 1, 2017.

  1. Dekata

    Dekata

    Joined:
    May 20, 2016
    Posts:
    47
    Guys, I'm currently work with UDP and i try to receive double array like [3, 4, 5] but when i use
    Code (CSharp):
    1.  
    2. UdpClient receivingUdpClient = new UdpClient(11000);
    3.  
    4. IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
    5. try{
    6.  
    7.      // Blocks until a message returns on this socket from a remote host.
    8.      Byte[] receiveBytes = receivingUdpClient.Receive(ref RemoteIpEndPoint);
    9.  
    10.      double returnData = BitConverter.ToDouble(receiveBytes); // i only receive the first element
    11. }
    12. catch ( Exception e ){
    13.      Console.WriteLine(e.ToString());
    14. }
    15.  
    Thanks guys.
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,825
    Where are you trying to get more than just the one double out of that byte array? "double returnData" is just a single double, not an array. I haven't done what you're doing before, but my first guess is you would create a list or array of doubles, and then step through receiveBytes every 8 bytes converting them into separate doubles that you add or append to the list or array. (A double is 8 bytes in Unity right? lol)

    Something like this might work?
    Code (csharp):
    1.  
    2. Byte[] receiveBytes = receivingUdpClient.Receive(ref RemoteIpEndPoint);
    3.  
    4. List<double> receiveList = new List<double>();
    5. int position = 0;
    6. while (position < receiveBytes.Length)
    7. {
    8.     double returnDouble = BitConverter.ToDouble(receiveBytes, position);
    9.     receiveList.Add(returnDouble);
    10.     position = position + 8;
    11. }
    12. double[] returnData = receiveList.ToArray();
    13.  
    Someone usually comes along and demonstrates that what I've done can already be done with just a single method call I was not aware of, so expect that too :p
     
    Dekata likes this.
  3. Dekata

    Dekata

    Joined:
    May 20, 2016
    Posts:
    47
    Thanks for your reply, i know where i misstake. The receive return an byte array of [3,4,5] so as you said position = position + 8; is a point :D
     
  4. Driiades

    Driiades

    Joined:
    Oct 27, 2015
    Posts:
    151
    And to not use a List (seriously ? :p allocated a list for that ? :p )

    Just send a first short in the beginning of the message and put in the length of the array.
    After that you can just read this short to allocate a new array with the great size and fill your array with your method.

    :) cheers :)

    EDIT: you can also just call sizeof(double) instead of putting "8" directly in code =) .
     
  5. Dekata

    Dekata

    Joined:
    May 20, 2016
    Posts:
    47
    thanks for tip!
    I have other question, that is i read position data from UDP and put in Obj to move :
    Code (CSharp):
    1. transform.position = udpReceivedVector3;
    every 0.01 secs but object move isn't smooth.
     
  6. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,825
    You'll want to interpolate between the old vector3 and the new vector3 position instead of just setting the position. Just setting the position will always be jumpy.