I cannot get the Editor, in Play mode, to receive UDP datagrams over a LAN

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.

Sorry, this is not a hard & fast case. This morning, after my PCs got a good night’s sleep, it worked perfectly… then stopped working, then started again and stopped again. I think I need to do more investigation.

I have another test case, which sends a datagram every frame. You can see on the receiving PC a received counter, rapidly counting up. After a short time, the received counter stops. Then, for no apparent reason a while later, it starts again.

I will update this post if I find anything, in the hope that it helps someone else

I have encountered this exact problem. I want to be able to test from what we’ll name PC2, using controls with a Unity standalone build, to send messages to my project on PC1, which I am still working on in the Editor. The Editor sends messages to PC2 just fine; PC2 receives them. The same script running on PC1 can send out, and the same script on PC2 can also receive. On PC1, I have set up to send and receive from the IP address of PC2. On PC2, it’s the other way around. Is this an UnityEditor restriction?