• 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 /
This question was closed Apr 10, 2014 at 10:44 PM by zombience for the following reason:

Problem was solved, but undetermined why

avatar image
0
Question by zombience · Mar 27, 2014 at 04:34 PM · c#iosnetworksocketudp

UDPSend not working with iPad over LAN

I'm having issues broadcasting over LAN.

I don't yet know much about network best practices in .NET, so perhaps I'm taking the wrong approach. I basically took this code from an answer to another question here

I'm attempting to have two machines talk directly over a local network (trying to avoid internet connections, and hopefully avoiding MasterServer just for installation time).

I'm able to use this code to have two machines, mac or pc, talk to each other over the network. But loading the exact same project on an ipad yields nothing. I have a UDPReceiver set up, and the iPad can receive UDP messages, but is not broadcasting a return.

anyone have any insight on this? I understand that .NET sockets is restricted on iOS free version of Unity. I am using Unity Pro with iOS package.

 using UnityEngine;
 using System.Collections;
 using System.Net;
 using System.Net.Sockets;
 
 
 public class UDPSender : MonoBehaviour 
 {
 
     public int remotePort = 19800;
     public int localPort = 19801;
     public string name;
     public string IP;
     public string gameName;
 
     public string broadcastSig;
     public string broadcastMsg;
 
     protected bool refreshing;
 
     protected UdpClient sender;
 
     void OnEnable () 
     {
         sender = new UdpClient(localPort, AddressFamily.InterNetwork);
         IPEndPoint groupEP = new IPEndPoint(IPAddress.Broadcast, remotePort);
         sender.Connect(groupEP);
         IP = GetLocalIP();
         InvokeRepeating("SendData", 0, 5f);
 
     }
 
 
     void OnDisable()
     {
         if(sender != null)
             sender.Close();
     }
 
     protected void SendData()
     {
         broadcastMsg = broadcastSig + " TIME: " + Time.time.ToString();
         sender.Send(System.Text.Encoding.ASCII.GetBytes(broadcastMsg), broadcastMsg.Length);  
         Quaternion test = Quaternion.identity;
 
     }
 
 
     public string GetLocalIP()
     {
         IPHostEntry host;
         string localIP = "";
         host = Dns.GetHostEntry(Dns.GetHostName());
         foreach (IPAddress ip in host.AddressList)
         {
             if (ip.AddressFamily == AddressFamily.InterNetwork)
             {
                 localIP = ip.ToString();
                 break;
             }
         }
         Debug.Log("setting IP: " + localIP);
         return localIP;
     }
 }
 


Comment
Add comment · Show 4
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
avatar image fholm · Mar 28, 2014 at 05:24 AM 0
Share

Try using the normal System.Net.Sockets.Socket ins$$anonymous$$d of the UdpClient stuff, if you only want udp communication you can try my Udp$$anonymous$$it library, which can be found here: https://github.com/fholm/udpkit

Assuming your ports are open properly on the host computer everything should "just work" with Udp$$anonymous$$it (and normal .Net socket)

avatar image flamy · Mar 28, 2014 at 05:35 AM 0
Share

Hi @Zombience,

Are you sure you are able to send the data from PC to iPad, because usually when udp broadcast is happening, the data will be send to all the systems in the network including the one that has sent it. so if the systems and ipad are on different networks one might think that data is being recived on ipad but not send! Please confirm this...

avatar image zombience · Mar 28, 2014 at 02:27 PM 0
Share

@fholm thanks! I'll check that library out. @flamy Yup, I'm certain that it is receiving from a different source. This code right here:

 broadcast$$anonymous$$sg = broadcastSig + " TI$$anonymous$$E: " + Time.time.ToString();

uses an assignable broadcast signature, which I assign in inspector for each machine. so, sending from the PC I set it to "PC", and sending from iPad I set it to "IPAD" so I can be sure who is receiving what from whom at what time.

avatar image zombience · Apr 10, 2014 at 10:45 PM 0
Share

so, apparently this code works. not sure why it wasn't working before, but now it does. :D

0 Replies

  • Sort: 

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

[C#] Sockets on mobile devices 0 Answers

LLAPI vs. pure C#.NET sockets 3 Answers

Unity to Node.js UDP: datagrams received are all empty. 2 Answers

Multiple Cars not working 1 Answer

How can I get the WiFis name? 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges