- Home /
Problem was solved, but undetermined why
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;
}
}
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
Assu$$anonymous$$g your ports are open properly on the host computer everything should "just work" with Udp$$anonymous$$it (and normal .Net socket)
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...
@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.
so, apparently this code works. not sure why it wasn't working before, but now it does. :D
Follow this Question
Related Questions
LLAPI vs. pure C#.NET sockets 3 Answers
C# code run in App Console but not on Unity console 1 Answer
Multiple Cars not working 1 Answer
Unity to Node.js UDP: datagrams received are all empty. 2 Answers
Distribute terrain in zones 3 Answers