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.
  2. Dismiss Notice

Question Socket send array to unity have some problem

Discussion in 'Scripting' started by h8476532, Apr 14, 2022.

  1. h8476532

    h8476532

    Joined:
    Mar 14, 2022
    Posts:
    17
    Server code:
    c_px = 311.2;
    c_py = 18.38;
    c_pz = 137.44;
    c_rx = -0.07;
    c_ry = 179.93;
    c_rz = 11.53;

    Socket severSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

    EndPoint endPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 10001);
    severSocket.Bind(endPoint);

    severSocket.Listen(1000);

    Console.WriteLine("server already opened!");
    Console.WriteLine("Waiting for Client connect!");
    Socket cilentSocket = severSocket.Accept();
    Console.WriteLine("client is connect.");
    while (true)
    {
    byte [] sendMsg = { Convert.ToByte(c_px), Convert.ToByte(c_py), Convert.ToByte(c_pz), Convert.ToByte(c_rx), Convert.ToByte(c_ry), Convert.ToByte(c_rz) };
    int sendLength = cilentSocket.Send(sendMsg, SocketFlags.None);
    }

    //ConsoleKey key;
    //do
    //{
    // key = Console.ReadKey(true).Key;
    //}
    //while (key != ConsoleKey.Q);
    }

    When I run the server on visual studio and then run the client on unity, it jumps the problem as pic shown below.
    The error show about:
    System.OverflowException: 'The value is too large or too small for an unsigned byte. '
    upload_2022-4-14_15-55-38.png

    How can fix it?
     
  2. nijnstein

    nijnstein

    Joined:
    Feb 6, 2021
    Posts:
    78
    you are casting a byte with a conversion utility that throws errors if the value is not a byte which you valuesr clearly ar not. - 0.07 wont convert to an unsigned byte as its negative.
     
  3. h8476532

    h8476532

    Joined:
    Mar 14, 2022
    Posts:
    17
    Thank you!!
    But I try to change the -0.07 to 0.07 is also jump out the same error.
    I want to send all these 6 information merges to an array and send this array to Unity
    Best regard
     
    Last edited: Apr 25, 2022
  4. nijnstein

    nijnstein

    Joined:
    Feb 6, 2021
    Posts:
    78
    https://docs.microsoft.com/en-us/dotnet/api/system.convert.tobyte?view=net-6.0

    a byte is a value between 0 and 255, a whole positive value while you can round a number to a byte you cant use the convert class because its not a valid number to put in a byte, from the documentation:

    Code (CSharp):
    1. using System;
    2.  
    3. public class Example
    4. {
    5.    public static void Main()
    6.    {
    7.       String[] values = { null, "", "0xC9", "C9", "101", "16.3",
    8.                           "$12", "$12.01", "-4", "1,032", "255",
    9.                           "   16  " };
    10.       foreach (var value in values) {
    11.          try {
    12.             byte number = Convert.ToByte(value);
    13.             Console.WriteLine("'{0}' --> {1}",
    14.                               value == null ? "<null>" : value, number);
    15.          }
    16.          catch (FormatException) {
    17.             Console.WriteLine("Bad Format: '{0}'",
    18.                               value == null ? "<null>" : value);
    19.          }
    20.          catch (OverflowException) {
    21.             Console.WriteLine("OverflowException: '{0}'", value);
    22.          }
    23.       }
    24.    }
    25. }
    26. // The example displays the following output:
    27. //     '<null>' --> 0
    28. //     Bad Format: ''
    29. //     Bad Format: '0xC9'
    30. //     Bad Format: 'C9'
    31. //     '101' --> 101
    32. //     Bad Format: '16.3'
    33. //     Bad Format: '$12'
    34. //     Bad Format: '$12.01'
    35. //     OverflowException: '-4'
    36. //     Bad Format: '1,032'
    37. //     '255' --> 255
    38. //     '   16  ' --> 16
    The values you try to put in bytes dont fit in bytes, overflow means exactly that (the error you get), you could just cast it to bytes after minmaxing the value: byte b = (byte)Mathf.Min(Mathf.Max(value, 0), 255) instead of using the convert class.
     
  5. h8476532

    h8476532

    Joined:
    Mar 14, 2022
    Posts:
    17
    Thanks for your reply, and sorry for my reply late.
    To be honest, I'm a little bit don't understand what you describe.