Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by CHull · Oct 08, 2014 at 01:36 PM · c#networkingudp

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);
         
     }
 
 
     
 }
 
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges