- Home /
 
UDP implementation in Unity, unable to send to two different machines
Been trying to build upon a simple udp implementation in Unity that I came across on the unity forums: http://forum.unity3d.com/threads/simple-udp-implementation-send-read-via-mono-c.15900/
There are two scripts, one send and one receive. Testing it out in uni and it works perfect (i type in a message and the machine with the receive script prints it to the screen). I would like to build upon it and be able to send the same message to two different machines.
The new send script is below. Have I went about this entirely the wrong way? Apologies for the long code, but I am unsure as to what I can remove! Any help would be greatly appreciated, tearing my hair out.
 public class UDPSend2 : MonoBehaviour
 {
     private static int localPort;
     private string IP, IP2;
         public int port;
     IPEndPoint remoteEndPoint, remoteEndPoint2;
     UdpClient client, client2;
     string strMessage="";
     
 
     private static void Main()
     {
         UDPSend2 sendObj=new UDPSend2();
         sendObj.init();
         sendObj.sendEndless(" endless infos \n");
         
     }
 
     public void Start()
     {
         init();
     }
     
     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> nc -lu 127.0.0.1  "+port+" \n"
                 ,style);
         
 
         strMessage=GUI.TextField(new Rect(40,420,140,20),strMessage);
         if (GUI.Button(new Rect(190,420,40,20),"send"))
         {
             sendString(strMessage+"\n");
         }      
     }
     
     public void init()
     {
         print("UDPSend.init()");
         //IP="127.0.0.1"; local
         IP = ""; //adam
         IP2 = ""; //conor
             port = 80;
         remoteEndPoint = newIPEndPoint(IPAddress.Parse(IP), port);
         client = new UdpClient();
             remoteEndPoint2 = new IPEndPoint(IPAddress.Parse(IP2), port);
         client2 = new UdpClient();    
     }
 
     private void sendString(string message)
     {
         try
         {
 
             byte[] data = Encoding.UTF8.GetBytes(message);
             print("sent to 1");
             client.Send(data, data.Length, remoteEndPoint);
                     client2.Send(data, data.Length, remoteEndPoint2);
             print("sent to 2");
 
         }
         catch (Exception err)
         {
             print(err.ToString());
         }
     }
     
     
     private void sendEndless(string testStr)
     {
         do
         {
             sendString(testStr);    
         }
         while(true);
         
     }
 
 
     
 }
 
 
              Your answer
 
             Follow this Question
Related Questions
Multiple Cars not working 1 Answer
New to Unity, Need Help. 1 Answer
Weird GUILayout Scrollview Behavior 0 Answers
Facebook SDK and ADColony Compatible in iOS? 2 Answers
Player instantiates Backwords 0 Answers