title: Sending data as double float over UDP C#
body: <p><strong>Hi everybody,</strong></p>
  
  <p><strong>Brief of post:</strong> I need help converting the script below to send data over UDP as a double float rather than in UTF8, with the data being sent acquired through the command 'transform.position.x' in Unity 3D     :) If you would like to know more please read on.</p>
  
  <p>My university Masters project has been to develop a racing simulator, I've had the basics all working by sending vehicle position and rotation data from the simulation software to the visuals over UDP, and then in the visuals assigning the incoming data streams to each position or rotation vector for a 3D vehicle model. And now, as part of some system analysis I'm trying to measure any lag or latency caused by the communication or by the visual package running at a much lower frequency to the simulation software.</p>
  
  <p>To measure this I want to control an object within the visual environment like normal but then have another separate script getting the coordinates of the object and sending them straight back to the simulation software on another port and that's the script I'm having trouble with as the data needs to be sent back as a double float.</p>
  
  <p>Both the send and receive UDP scripts were originally sourced from a forum:
  <a href="https://community.unity.com/t5/Multiplayer-Networking/simple-udp-implementation-send-read-via-mono-c/td-p/154952" rel="nofollow">https://community.unity.com/t5/Multiplayer-Networking/simple-udp-implementation-send-read-via-mono-c/td-p/154952</a>
  The UDP Send script I am trying to modify is also shown below</p>
  
  <p>At the moment, out of these two, only the receive script is being used and when I came across it it had already been modified to receive a double float and what I'm trying to do is modify the send script to send a double float as well, they were both originally written to send and receive UTF8 encoded text.</p>
  
  <p>Also, the visual package is Unity 3D and the command 'transform.position.x' is one which gets the coordinate of an object and so this will be what is being sent, and then I will add the other position and rotation vectors once I understand the method.</p>
  
  <p>Also, I should probably mention I have very little experience with coding as this isn't the main part of my project but would really appreciate some help as I'm getting no where with this tiny bit and am now running out of time.</p>
  
  <p>Many thanks in advance!!</p>
  
  <pre><code>using UnityEngine;
  using System.Collections;
  
  using System;
  using System.Text;
  using System.Net;
  using System.Net.Sockets;
  using System.Threading;
  
  public class UDPSend : MonoBehaviour {
  
  private static int localPort;
  
  // prefs
  private string IP;  // define in init
  public int port;  // define in init
  
  // "connection" things
  IPEndPoint remoteEndPoint;
  UdpClient client;
  
  // gui
  string strMessage="";
  
  
  // call it from shell (as program)
  private static void Main()
  {
      UDPSend sendObj=new UDPSend();
      sendObj.init();
  
      // testing via console
      // sendObj.inputFromConsole();
  
      // as server sending endless
      sendObj.sendEndless(" endless infos \n");
  
  }
  // start from unity3d
  public void Start()
  {
      init();
  }
  
  // OnGUI
  void OnGUI()
  {
      Rect rectObj=new Rect(40,380,200,400);
      GUIStyle style = new GUIStyle();
      style.alignment = TextAnchor.UpperLeft;
      GUI.Box(rectObj,"# UDPSend-Data\n127.0.0.1 "+port+" #\n"
              + "shell&gt; nc -lu 127.0.0.1  "+port+" \n"
              ,style);
  
      // ------------------------
      // send it
      // ------------------------
      strMessage=GUI.TextField(new Rect(40,420,140,20),strMessage);
      if (GUI.Button(new Rect(190,420,40,20),"send"))
      {
          sendString(strMessage+"\n");
      }      
  }
  
  // init
  public void init()
  {
      // Define end point , from which the messages are sent.
      print("UDPSend.init()");
  
      // define
      IP="127.0.0.1";
      port=16;
  
      // ----------------------------
      // Send
      // ----------------------------
      remoteEndPoint = new IPEndPoint(IPAddress.Parse(IP), port);
      client = new UdpClient();
  
      // status
      print("Sending to "+IP+" : "+port);
      print("Testing: nc -lu "+IP+" : "+port);
  
  }
  
  // inputFromConsole
  private void inputFromConsole()
  {
      try
      {
          string text;
          do
          {
              text = Console.ReadLine();
  
              // Send the text to the remote client .
              if (text != "")
              {
  
                  // encode data with the UTF8 encoding to binary .
                  byte[] data = Encoding.UTF8.GetBytes(text);
  
                  // Send the text to the remote client .
                  client.Send(data, data.Length, remoteEndPoint);
              }
          } while (text != "");
      }
      catch (Exception err)
      {
          print(err.ToString());
      }
  
  }
  
  // sendData
  private void sendString(string message)
  {
      try
      {
          //if (message != "")
          //{
  
          // encode data with the UTF8 encoding to binary .
          byte[] data = Encoding.UTF8.GetBytes(message);
  
          // Send the message to the remote client .
          client.Send(data, data.Length, remoteEndPoint);
          //}
      }
      catch (Exception err)
      {
          print(err.ToString());
      }
  }
  
  
  // endless test
  private void sendEndless(string testStr)
  {
      do
      {
          sendString(testStr);
  
  
      }
      while(true);
  
  }
  
  }
  </code></pre>