Unity UDP Receiver Not Receiving Data from External Device

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!

New contributor

kinwon is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật