C#脚本上的UDP服务器统一启动但无法接收数据包

UDP Server on C# script in unity started but can't receive packets

我的 android phone 上有一个应用程序发送一个包含一些信息的 UDP 数据包,我想将这个 UDP 数据包发送到我的 Unity 应用程序。 android 应用程序工作并发送 UDP 数据包(使用 wireshark 检查)但不知何故我的 C# 脚本无法接收任何数据包。我用谷歌搜索并查看了类似的问题,但其中 none 给了我一个答案。真不知道为什么收不到包

我正在广播 UDP 数据包以使事情变得更容易,但它似乎不起作用。我也检查了端口。

这是我的 C# 代码:

public class ControllerListener : MonoBehaviour
{
    Thread receiveThread;
    UdpClient client;

    public int port; 
    public string lastReceivedUDPPacket = "";
    public string allReceivedUDPPackets = "";

    public void Start()
    {
        init();
    }

    // OnGUI
    void OnGUI()
    {
        Rect rectObj = new Rect(40, 10, 200, 400);
        GUIStyle style = new GUIStyle();
        style.alignment = TextAnchor.UpperLeft;
        GUI.Box(rectObj, "# UDPReceive\n127.0.0.1 " + port + " #\n"
                    + "shell> nc -u 127.0.0.1 : " + port + " \n"
                    + "\nLast Packet: \n" + lastReceivedUDPPacket
                    + "\n\nAll Messages: \n" + allReceivedUDPPackets
                , style);
    }

    public void Update()
    {

    }

    // init
    private void init()
    {
        print("UDPSend.init()");

        // define port
        port = 5678;

        // status
        print("Sending to 127.0.0.1 : " + port);
        print("Test-Sending to this Port: nc -u 127.0.0.1  " + port + "");

        receiveThread = new Thread(
            new ThreadStart(ReceiveData));
        receiveThread.IsBackground = true;
        receiveThread.Start();
    }

    // receive thread
    private void ReceiveData()
    {
        client = new UdpClient(port);
        while (true)
        {
            try
            {
                // Bytes empfangen.
                IPEndPoint anyIP = new IPEndPoint(IPAddress.Broadcast, 5678);
                byte[] data = client.Receive(ref anyIP);

                // Bytes mit der UTF8-Kodierung in das Textformat kodieren.
                string text = Encoding.UTF8.GetString(data);

                // Den abgerufenen Text anzeigen.
                print(">> " + text);
                // latest UDPpacket
                lastReceivedUDPPacket = text;
                Thread.Sleep(8);
                Debug.Log("Hier");
                // ....
                allReceivedUDPPackets = allReceivedUDPPackets + text;

            }
            catch (Exception err)
            {
                print(err.ToString());
            }
        }
    }

    // getLatestUDPPacket
    // cleans up the rest
    public string getLatestUDPPacket()
    {
        allReceivedUDPPackets = "";
        return lastReceivedUDPPacket;
    }

    void OnDisable()
    {
        if (receiveThread != null)
            receiveThread.Abort();

        client.Close();
    }
}

我只是想知道为什么我的代码没有从 .Receive(...) 方法中退出,即使广播了 UDP 数据包。

提前致谢(我第一次 post 在这个网站上)

干杯

Udpclient.Receive will block until a datagram arrives from a remote host.

因此,如果没有 return,则表示尚未收到任何内容。

这可能有多种原因。

IPEndPoint 的 Afaik 你可能更应该使用

IPEndPoint anyIP = new IPEndPoint(IPAddress.Any, 0);

这意味着发件人可以拥有任何IP地址并从任何端口发送。您将发件人地址限制为 Broadcast 这可能永远不会是这种情况,因为发件人无法拥有广播地址。此外,发件人的传出端口可能与 5678 不匹配。 0 允许发件人从 任何端口发送 .


旁注:Thread.Sleep 以毫秒为单位,因此仅使用 8 看起来也有点奇怪;)