udp unity 通过端口通讯_Unity C#UDP接收的SocketException

在Unity中尝试通过端口30020监听UDP数据包时遇到问题,脚本在启动接收线程后不断收到SocketException。错误信息表明连接尝试超时,可能由于没有接收到UDP数据包。作者怀疑这可能是由于缺少UDP数据包导致的,同时注意到SocketException的频率与ReceiveTimeout设置有关。
摘要由CSDN通过智能技术生成

我正在使用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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值