I’m having trouble with a UDP receiver in my Unity project. My goal is to receive data sent from an external device (Amadeo robot) using UDP to port 4444 on IP “10.100.4.30”. The data should be received by my Unity server, but it seems to be stuck on UdpReceiveResult result = await udpClient.ReceiveAsync();.
Key Details:
- External Device (Amadeo robot): Sends UDP packets to port 4444 on IP “10.100.4.30”.
- Unity Server: Should listen for incoming UDP packets and process them.
- Network Setup: The robot is connected to the computer with LAN.
Observations:
-
I can see the traffic in WireShark and the protocol used is NetBIOS.
-
The Unity receiver is not capturing the data, while a similar console application works correctly.
enter image description here
The Unity Server Code:
using System; using System.Net; using System.Net.Sockets; using System.Text; using System.Threading.Tasks; using UnityEditor; using UnityEngine; public class UDPReceiver : MonoBehaviour { private UdpClient udpClient; private Task receiveTask; private bool canReceive = true; private int localPort = 4444; // The port to receive data private IPAddress localAddress; void Start() { localAddress = IPAddress.Parse("10.100.4.30"); udpClient = new UdpClient(); udpClient.MulticastLoopback = true; // Bind to the specified port udpClient.Client.Bind(new IPEndPoint(localAddress, localPort)); // Start receiving data receiveTask = Task.Run(() => ReceiveData()); } private void OnEnable() { EditorApplication.playModeStateChanged += OnPlayModeStateChanged; } private void OnPlayModeStateChanged(PlayModeStateChange state) { if (state == PlayModeStateChange.ExitingPlayMode) { Debug.Log("Exiting Play Mode: Cleanup actions here."); canReceive = false; receiveTask.Wait(); Debug.Log($"On Destroy :: Received the async task."); udpClient.Close(); } } void OnDestroy() { // Stop the receive loop udpClient.Close(); canReceive = false; receiveTask.Wait(); Debug.Log($"On Destroy :: Received the async task."); udpClient.Close(); } private async void ReceiveData() { // IP address and port IPAddress ipAddress = IPAddress.Parse("10.100.4.30"); Debug.Log($"Listening ..."); // Create a UDP client // Enable multicast loopback // udpClient.JoinMulticastGroup(ipAddress); Debug.Log($"Listening on {ipAddress}:{localPort}..."); while (canReceive) { try { // Receive data Debug.Log("Waiting for data.. "); //IPEndPoint endpoint = new IPEndPoint(ipAddress, port); //byte[] receivedData = udpClient.Receive(ref endpoint); //string receivedString = Encoding.ASCII.GetString(receivedData); //Console.WriteLine($"Received: {receivedString}"); //Debug.Log($"Recieved data :: {receivedString}"); // Acync reveive UdpReceiveResult result = await udpClient.ReceiveAsync(); string receivedText = Encoding.ASCII.GetString(result.Buffer); Debug.Log("Received data from " + result.RemoteEndPoint.ToString() + ": " + receivedText); } catch (Exception e) { udpClient.Close(); Debug.LogError(e.ToString()); } } udpClient.Close(); } }
Additional Information:
Console Application: I also have a console application that successfully receives the same data and forwards it to another port (I mean I changed the port of the Unity server to 5555 to see if it gets the data and it works). Here’s the console application code:
using System; using System.Net; using System.Net.Sockets; using System.Text; class Program { static void Main() { // IP address and port IPAddress ipAddress = IPAddress.Parse("10.100.4.30"); int port = 4444; // Create a UDP client using (UdpClient udpClient = new UdpClient(), udpClientSender = new UdpClient(5555)) { // Enable multicast loopback udpClient.MulticastLoopback = true; IPEndPoint iPEndPointToSend = new IPEndPoint(ipAddress, 5555); // Bind to the specified port udpClient.Client.Bind(new IPEndPoint(ipAddress, port)); Console.WriteLine($"Listening on {ipAddress}:{port}..."); // Receive data while (true) { IPEndPoint endpoint = new IPEndPoint(ipAddress, port); byte[] receivedData = udpClient.Receive(ref endpoint); string receivedString = Encoding.ASCII.GetString(receivedData); Console.WriteLine($"Received: {receivedString}"); udpClientSender.Send(receivedData, receivedData.Length, iPEndPointToSend); } } } }
I would appreciate any help or suggestions on what might be going wrong and how to fix this issue. Thank you!
Network Configuration: I verified that the Unity application has network permissions and there are no firewalls blocking UDP traffic on port 4444.
Debug Logging: The Unity logs show that the script is starting and attempting to receive data, but it never logs any received data.
WireShark: I can see the UDP traffic in WireShark, and the protocol used is NetBIOS.
I expected the Unity server to receive UDP packets sent by the Amadeo robot to port 4444 on IP “10.100.4.30”, similar to how the console application successfully receives and processes the data.
I would appreciate any help or suggestions on what might be going wrong and how to fix this issue. Thank you!
kinwon is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.