首先,我是 Android 和 dot42 的新手。我已经构建了一个 UDP 侦听器类,它在 Windows 应用程序中运行良好。我正在使用 System.Net.Sockets.UdpClient 类。
现在,我尝试在 dot42 项目中使用我的 UDP 侦听器类,但我收到错误消息
找不到类型 System.Net.Sockets.UdpClient。
我想,这个类在 dot42 中不可用?
有没有办法可以为 Android 应用程序使用下面的相同代码(或仅进行一些修改)?
using System;
using System.Net;
using System.Net.Sockets;
namespace FSInterface
{
public class UDPReceiver
{
private UdpClient _udpRx;
private IPEndPoint ep;
private int _port;
public delegate void BytesReceive(byte[] buffer);
public event BytesReceive OnBytesReceive;
public UDPReceiver(int port)
{
_port = port;
_udpRx = new UdpClient();
_udpRx.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
_udpRx.ExclusiveAddressUse = false;
ep = new IPEndPoint(IPAddress.Any, _port);
_udpRx.Client.Bind(ep);
_udpRx.BeginReceive(new AsyncCallback(ReceiveCallback), null);
}
private void ReceiveCallback(IAsyncResult ar)
{
_udpRx.BeginReceive(new AsyncCallback(ReceiveCallback), null);
byte[] buffer = _udpRx.EndReceive(ar, ref ep);
if (OnBytesReceive != null)
{
OnBytesReceive(buffer);
}
}
}
}