title: Send end receive UDP in unity3d
body: <p>I want to send a package from my computer to the unity app installed on my smartphone. I refer from this tutorial <a href="http://forum.unity3d.com/threads/simple-udp-implementation-send-read-via-mono-c.15900/" rel="nofollow">http://forum.unity3d.com/threads/simple-udp-implementation-send-read-via-mono-c.15900/</a> . The codes is very good when I run on unity virtual machine but when I change IP to my local IP (192.168.0.102). My phone can not receive data. Can you help me solve this problem. 
  Thank you very much!</p>
  
  <p>UDPSend.cs</p>
  
  <pre><code>/*
  
      -----------------------
      UDP-Send
      -----------------------
      // [url]http://msdn.microsoft.com/de-de/library/bb979228.aspx#ID0E3BAC[/url]
  
      // &gt; gesendetes unter
      // 127.0.0.1 : 8050 empfangen
  
      // nc -lu 127.0.0.1 8050
  
          // todo: shutdown thread at the end
  */
  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()
      {
          // Endpunkt definieren, von dem die Nachrichten gesendet werden.
          print("UDPSend.init()");
  
          // define
          //IP="127.0.0.1";
          IP = "192.168.0.102";
          port=8051;
  
          // ----------------------------
          // Senden
          // ----------------------------
          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();
  
                  // Den Text zum Remote-Client senden.
                  if (text != "")
                  {
  
                      // Daten mit der UTF8-Kodierung in das Binärformat kodieren.
                      byte[] data = Encoding.UTF8.GetBytes(text);
  
                      // Den Text zum Remote-Client senden.
                      client.Send(data, data.Length, remoteEndPoint);
                  }
              } while (text != "");
          }
          catch (Exception err)
          {
              print(err.ToString());
          }
  
      }
  
      // sendData
      private void sendString(string message)
      {
          try
          {
              //if (message != "")
              //{
  
              // Daten mit der UTF8-Kodierung in das Binärformat kodieren.
              byte[] data = Encoding.UTF8.GetBytes(message);
  
              // Den message zum Remote-Client senden.
              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>
  
  <p>UDPReceive.cs</p>
  
  <pre><code>/*
  
          -----------------------
          UDP-Receive (send to)
          -----------------------
          // [url]http://msdn.microsoft.com/de-de/library/bb979228.aspx#ID0E3BAC[/url]
  
  
          // &gt; receive
          // 127.0.0.1 : 8051
  
          // send
          // nc -u 127.0.0.1 8051
  
      */
  using UnityEngine;
  using System.Collections;
  
  using System;
  using System.Text;
  using System.Net;
  using System.Net.Sockets;
  using System.Threading;
  
  public class UDPReceive : MonoBehaviour {
  
      // receiving Thread
      Thread receiveThread;
  
      // udpclient object
      UdpClient client;
  
      // public
      // public string IP = "127.0.0.1"; default local
      public int port; // define &gt; init
  
      // infos
      public string lastReceivedUDPPacket="";
      public string allReceivedUDPPackets=""; // clean up this from time to time!
  
  
      // start from shell
      private static void Main()
      {
          UDPReceive receiveObj=new UDPReceive();
          receiveObj.init();
  
          string text="";
          do
          {
              text = Console.ReadLine();
          }
          while(!text.Equals("exit"));
      }
      // start from unity3d
      public void Start()
      {
  
          init();
      }
  
      // OnGUI
      void OnGUI()
      {
          Rect rectObj=new Rect(40,10,200,400);
          GUIStyle style = new GUIStyle();
          style.alignment = TextAnchor.UpperLeft;
          GUI.Box(rectObj,"# UDPReceive\n127.0.0.1 "+port+" #\n"
                  + "shell&gt; nc -u 127.0.0.1 : "+port+" \n"
                  + "\nLast Packet: \n"+ lastReceivedUDPPacket
                  + "\n\nAll Messages: \n"+allReceivedUDPPackets
                  ,style);
      }
  
      // init
      private void init()
      {
          // Endpunkt definieren, von dem die Nachrichten gesendet werden.
          print("UDPSend.init()");
  
          // define port
          port = 8051;
  
          // status
          print("Sending to 127.0.0.1 : "+port);
          print("Test-Sending to this Port: nc -u 127.0.0.1  "+port+"");
  
  
          // ----------------------------
          // Abhören
          // ----------------------------
          // Lokalen Endpunkt definieren (wo Nachrichten empfangen werden).
          // Einen neuen Thread für den Empfang eingehender Nachrichten erstellen.
          receiveThread = new Thread(
              new ThreadStart(ReceiveData));
          receiveThread.IsBackground = true;
          receiveThread.Start();
  
      }
  
      // receive thread
      private  void ReceiveData()
      {
  
          client = new UdpClient(port);
          while (true)
          {
  
              try
              {
                  // Bytes empfangen.
                  IPEndPoint anyIP = new IPEndPoint(IPAddress.Any, 0);
                  byte[] data = client.Receive(ref anyIP);
  
                  // Bytes mit der UTF8-Kodierung in das Textformat kodieren.
                  string text = Encoding.UTF8.GetString(data);
  
                  // Den abgerufenen Text anzeigen.
                  print("&gt;&gt; " + text);
  
                  // latest UDPpacket
                  lastReceivedUDPPacket=text;
  
                  // ....
                  allReceivedUDPPackets=allReceivedUDPPackets+text;
  
              }
              catch (Exception err)
              {
                  print(err.ToString());
              }
          }
      }
  
      // getLatestUDPPacket
      // cleans up the rest
      public string getLatestUDPPacket()
      {
          allReceivedUDPPackets="";
          return lastReceivedUDPPacket;
      }
  }
  </code></pre>