Unity C#UDP接收的SocketException

时间:2013-08-14 18:34:52

标签: c# sockets exception udp

我正在使用Unity,我正在尝试侦听特定端口上的UDP数据包。在Unity中,我有一个附加脚本的对象,该脚本旨在简单地监听和记录UDP数据包中的数据。在脚本中我有我的启动功能:

public void Start() {
    //  Setup listener.
    this.mListener = new IPEndPoint(IPAddress.Loopback, 0);

    //  Setup background UDP listener thread.
    this.mReceiveThread = new Thread(new ThreadStart(ReceiveData));
    this.mReceiveThread.IsBackground = true;
    this.mReceiveThread.Start();
}

该函数设置IPEndPoint和接收线程,然后启动接收线程,定义为:

private void ReceiveData() {
    try {
        //  Setup UDP client.
        this.mClient = new UdpClient(30020);
        this.mClient.Client.ReceiveTimeout = 250;

        //  While thread is still alive.
        while(Thread.CurrentThread.IsAlive) {
            try {
                //  Grab the data.
                byte[] data = this.mClient.Receive(ref this.mListener);

                //  Convert the data.
                /* REDACTED */

                //  Separate out the data.
                /* REDACTED */

                //  Store data in the DataSource.
                /* REDACTED */
            } catch(SocketException e) {
                Debug.Log(e.ToString());
                continue;
            }
        }
    } catch(Exception e) {
        Debug.Log(e.ToString());
    }
}

然而,当我尝试运行它时,我不断收到以下SocketException:

System.Net.Sockets.SocketException: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.

这段代码已经工作了几个月,我以前从未见过这个例外。这对我来说真的没有意义。我已经尝试更改端口但它似乎没有任何影响。我不确定发生了什么,或者我做了什么改变让它表现得这样。自从我开始这个项目以来,我没有更新Unity。我的其他程序可能不会发送任何UDP数据包吗?缺少UDP数据包可能是导致这种情况的原因吗?此SocketException在我的日志中出现的频率与ReceiveTimeout属性相关...

0 个答案:

没有答案