Hi,
I’m trying to receive data from a wireless hotspot via UDP. Like the title says, both a Python script and Wireshark receive the data. The wireless network itself is local and only works for anyone who connects to it. It’s sending USP packets via the broadcast address.
I’ve been searching for a couple of hours and haven’t found anything that works yet. I’ve tried several things that are easily found via Google. Currently I have this small piece of code, but like I said I’ve tried many more.
For me it comes down to the Receive() method not receiving any data and thus hanging the thread. I’ve tried using Sockets with a timeout set as well, which will result in a timeout.
using System.Collections;
using System.Collections.Generic;
using System;
using System.Text;
using System.Threading;
using System.Net;
using System.Net.Sockets;
using UnityEngine;
using UnityEngine.UI;
public class GatherData : MonoBehaviour
{
public Text text;
Thread m_Thread;
UdpClient m_Client;
string m_LastKnownPacket;
void Start()
{
m_Thread = new Thread(new ThreadStart(ReceiveData));
m_Thread.IsBackground = true;
m_Thread.Start();
}
void ReceiveData()
{
try
{
m_Client = new UdpClient(2390);
m_Client.EnableBroadcast = true;
while (true)
{
IPEndPoint hostIP = new IPEndPoint(IPAddress.Any, 0);
byte[] data = m_Client.Receive(ref hostIP);
Debug.Log(data.Length);
}
}
catch (Exception e)
{
Debug.Log(e);
OnApplicationQuit();
}
}
private void OnApplicationQuit()
{
if (m_Thread != null)
{
m_Thread.Abort();
}
if (m_Client != null)
{
m_Client.Close();
}
}
private void Update()
{
text.text = m_LastKnownPacket;
}
}
The Python code can be viewed here: https://pastebin.com/6pqur9pq
As well as a screenshot of Wireshark:
Any help or tips would be appreciated. Feel free to ask about anything that might be unclear. Thanks in advance!

EDIT: I just tried building for the first time, and this seems to work. I’m currently using the following code which does not work in the editor and will not print the number 2:
using System.Collections;
using System.Collections.Generic;
using System;
using System.Text;
using System.Threading;
using System.Net;
using System.Net.Sockets;
using UnityEngine;
using UnityEngine.UI;
public class GatherData : MonoBehaviour
{
public Text text;
Thread m_Thread;
UdpClient m_Client;
string m_LastKnownPacket;
void Start()
{
m_Client = new UdpClient(new IPEndPoint(IPAddress.Any, 2390));
m_Client.EnableBroadcast = true;
m_Client.BeginReceive(new AsyncCallback(ReceiveData), null);
Debug.Log(1);
}
void ReceiveData(IAsyncResult result)
{
Debug.Log(2);
IPEndPoint senderEP = new IPEndPoint(IPAddress.Any, 0);
byte[] data = m_Client.EndReceive(result, ref senderEP);
Debug.Log(3);
Debug.Log(data.Length);
m_LastKnownPacket = data.Length.ToString();
m_Client.BeginReceive(new AsyncCallback(ReceiveData), null);
Debug.Log(3);
}
private void OnApplicationQuit()
{
if (m_Thread != null)
{
m_Thread.Abort();
}
if (m_Client != null)
{
m_Client.Close();
}
}
private void Update()
{
text.text = m_LastKnownPacket;
}
}
Is there a way to make it work inside the editor as well?