c# - 接收广播
问题描述
我已经测试了一个代码来接收广播消息,但它不起作用。这是代码:
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
public class SendReceiveRoomName : MonoBehaviour
{
const int PORT_NO = 15000;
private readonly UdpClient udpClient = new UdpClient();
IPEndPoint ipendpointSend = new IPEndPoint(IPAddress.Parse("255.255.255.255"), PORT_NO);
IPEndPoint ipendpointReceive = new IPEndPoint(IPAddress.Any, PORT_NO);
IAsyncResult asyncResult = null;
// Start is called before the first frame update
void Start()
{
StartListening();
}
private void StartListening()
{
asyncResult = udpClient.BeginReceive(Receive, new object());
}
private void Receive(IAsyncResult asr)
{
ipendpointReceive = new IPEndPoint(IPAddress.Any, PORT_NO);
byte[] bytes = udpClient.EndReceive(asr, ref ipendpointReceive);
string message = Encoding.ASCII.GetString(bytes);
Debug.Log("Received message:" + message);
StartListening();
}
}
此脚本附加到游戏对象。有人可以指出我的问题在哪里
解决方案
推荐阅读
- algorithm - 第二类斯特林数的递推关系
- python - 如何从 Pandas 数据帧为 LSTM 模型创建输入样本?
- ruby-on-rails - 如何避免此错误并正确测试 Rails 3 应用程序?
- powerbi - 返回包含特定列为空的行的表
- html - 从 MURAL 板上提取信息,提取 HTML 代码以查找属性/位置?
- cross-platform - 非工业环境中 OPC-UA 的最佳替代方案?
- database - 当我们在 Web UI 中发出一些请求时,有什么方法可以检查所有 dbs / 表都添加了新条目吗?
- php - 在 PHP 中获取受保护对象的值
- reactjs - 保持新行
材质界面 - c# - ASP.NET Core 2.2 WebAPI:如何使 PushStreamContent 工作