When using the Editor in Play mode, my app receives broadcast UDP datagrams, but not when they are sent directly to my IpAddress
I have a UdpClient listening for datagrams using BeginReceive & EndReceive
From another PC, I can use a UdpClient to Send to IPAddress.Broadcast, which is happily received by the app running in play mode. But if, from the same PC, I send to the ipAddress of the Unity Editor, it does not get received by the Editor.
I can set up a group of PCs, all with standalone builds. Any can send a broadcast and they all receive it perfectly. And if any of them sends a datagram to the ipAddress of any other PC, they receive that perfectly too.
It’s only the Editor that doesn’t receive datagrams addressed directly to it.
using UnityEngine;
using System.Net;
using System.Net.Sockets;
using System.Collections.Generic;
public class NetTest : MonoBehaviour
{
UdpClient m_sender;
UdpClient m_receiver;
int m_port = 9000;
List<IPEndPoint> m_received = new List<IPEndPoint>();
List<string> m_log = new List<string>();
string m_destIpAddress;
byte []m_data = new byte[4]; // 4 bytes, for no particular reason
//=======================================================================================
void Start()
{
m_sender = new UdpClient();
m_receiver = new UdpClient(m_port);
m_receiver.BeginReceive(new System.AsyncCallback(ReceiverCallback), null);
}
//=======================================================================================
void OnDestroy()
{
if (m_sender != null)
{ m_sender.Close(); }
if (m_receiver != null)
{ m_receiver.Close(); }
}
//=======================================================================================
void Update()
{
while (m_received.Count > 0)
{
IPEndPoint ep = m_received[0];
m_log.Add("Received datagram from "+ep.Address+" : "+ep.Port);
m_received.RemoveAt(0);
}
}
//=======================================================================================
void ReceiverCallback(System.IAsyncResult res)
{
IPEndPoint ep = new IPEndPoint(IPAddress.Any, 0);
byte[] data = m_receiver.EndReceive(res, ref ep);
m_received.Add(ep);
m_receiver.BeginReceive(new System.AsyncCallback(ReceiverCallback), null);
}
//=======================================================================================
void OnGUI()
{
if (GUILayout.Button("Send Broadcast"))
{
IPEndPoint ep = new IPEndPoint(IPAddress.Broadcast, m_port);
int sent = m_sender.Send(m_data, m_data.Length, ep);
m_log.Add("Sent "+sent+" bytes to "+ep.Address+" : "+ep.Port);
}
GUILayout.Space(10);
m_destIpAddress = GUILayout.TextField(m_destIpAddress);
if (GUILayout.Button("Send to "+m_destIpAddress))
{
IPEndPoint ep = new IPEndPoint(IPAddress.Parse(m_destIpAddress), m_port);
int sent = m_sender.Send(m_data, m_data.Length, ep);
m_log.Add("Sent "+sent+" bytes to "+ep.Address+" : "+ep.Port);
}
GUILayout.Space(10);
// Only display the most recent 20 events
while (m_log.Count > 20)
{ m_log.RemoveAt(0); }
for (int i=0; i<m_log.Count; i++)
{
GUILayout.Label(m_log[i]);
}
}
//=======================================================================================
}
Drop the code above onto a GameObject in a blank scene and build it. Then run it on 2 or more PCs on a LAN
There is a button to Broadcast, which each of the PCs will receive (including the sender)
Also, you can enter the IpAddress of one of the other Pcs and send directly to that.
From the messages that are displayed on screen it’s easy enough to work out each of the PCs IpAddresses when you receive broadcasts from them.
However, if you hit Play in the Editor, it will not receive datagrams addressed directly to it from other PCs.