Xamarin MulticastLock 获取不允许多播
Xamarin MulticastLock Acquiring doesn't allow multicast
我正在使用 Xamarin 创建一个 Android 应用程序。此应用程序必须接收多播流。服务器发送正常,但我的应用程序接收不正确。如果我获得多播锁,这就是我的研究表明我需要做的所有事情,我可以看到我有锁,但应用程序仍然没有收到数据。
var wifiManager = var wifiManager = (WifiManager)Application.Context.GetSystemService(Context.WifiService);
// Acquire the lock
if (wifiManager != null)
{
// _lock is a private member WifiManager.MulticastLock _lock
_lock = wifiManager.CreateMulticastLock("PiEar");
if (_lock != null)
{
_lock.Acquire();
}
else
{
Debug.WriteLine("Could not acquire multicast lock"); // Does not print, meaning I do acquire the lock (and _lock.IsHeld() returns true)
}
}
else
{
Debug.WriteLine("WifiManager is null"); // Does not print, meaning it is not null
}
如果我随后使用不同的应用程序 MSniffer,它不仅会接收数据,而且还允许我的应用程序接收数据。我在尝试获取锁时做错了什么吗?
另外,我有以下权限:
- ACCESS_WIFI_STATE
- CHANGE_WIFI_STATE
- ACCESS_NETWORK_STATE
- 互联网
- CHANGE_WIFI_MULTICAST_STATE
编辑 1:更多信息
.cs
页面的相关代码如下。 _receivedMessages
是一个 ListView
,让我可以直观地看到接收到的数据。上面的代码就是下面line 2
上的运行。依赖服务中唯一的代码就是获取锁的代码。
var service = DependencyService.Get<IMulticastLock>();
service.Acquire();
Task.Run(async () =>
{
while (true)
{
try
{
var endpoint = new IPEndPoint(IPAddress.Parse("224.0.0.69"), 6666);
var multicast = new UdpClient(endpoint);
while (true)
{
var multicastBytes = await multicast.ReceiveAsync();
var message = Encoding.UTF8.GetString(multicastBytes.Buffer);
Debug.WriteLine($"Received: {message}");
Device.BeginInvokeOnMainThread(() =>
{
_receivedMessages.Add(message);
});
}
}
catch (Exception e)
{
Debug.WriteLine(e);
}
}
});
检查 IP 地址和端口。
您可以尝试使用下面关于如何创建和配置 UdpClient 的代码。
public class MulticastUdpClient
{
UdpClient _udpclient;
int _port;
IPAddress _multicastIPaddress;
IPAddress _localIPaddress;
IPEndPoint _localEndPoint;
IPEndPoint _remoteEndPoint;
public MulticastUdpClient(IPAddress multicastIPaddress, int port, IPAddress localIPaddress = null)
{
// Store params
_multicastIPaddress = multicastIPaddress;
_port = port;
_localIPaddress = localIPaddress;
if (localIPaddress == null)
_localIPaddress = IPAddress.Any;
// Create endpoints
_remoteEndPoint = new IPEndPoint(_multicastIPaddress, port);
_localEndPoint = new IPEndPoint(_localIPaddress, port);
// Create and configure UdpClient
_udpclient = new UdpClient();
// The following three lines allow multiple clients on the same PC
_udpclient.ExclusiveAddressUse = false;
_udpclient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
_udpclient.ExclusiveAddressUse = false;
// Bind, Join
_udpclient.Client.Bind(_localEndPoint);
_udpclient.JoinMulticastGroup(_multicastIPaddress, _localIPaddress);
// Start listening for incoming data
_udpclient.BeginReceive(new AsyncCallback(ReceivedCallback), null);
}
/// <summary>
/// Send the buffer by UDP to multicast address
/// </summary>
/// <param name="bufferToSend"></param>
public void SendMulticast(byte[] bufferToSend)
{
_udpclient.Send(bufferToSend, bufferToSend.Length, _remoteEndPoint);
}
/// <summary>
/// Callback which is called when UDP packet is received
/// </summary>
/// <param name="ar"></param>
private void ReceivedCallback(IAsyncResult ar)
{
// Get received data
IPEndPoint sender = new IPEndPoint(0, 0);
Byte[] receivedBytes = _udpclient.EndReceive(ar, ref sender);
// fire event if defined
if (UdpMessageReceived != null)
UdpMessageReceived(this, new UdpMessageReceivedEventArgs() { Buffer = receivedBytes });
// Restart listening for udp data packages
_udpclient.BeginReceive(new AsyncCallback(ReceivedCallback), null);
}
/// <summary>
/// Event handler which will be invoked when UDP message is received
/// </summary>
public event EventHandler<UdpMessageReceivedEventArgs> UdpMessageReceived;
/// <summary>
/// Arguments for UdpMessageReceived event handler
/// </summary>
public class UdpMessageReceivedEventArgs: EventArgs
{
public byte[] Buffer {get;set;}
}
}
我正在使用 Xamarin 创建一个 Android 应用程序。此应用程序必须接收多播流。服务器发送正常,但我的应用程序接收不正确。如果我获得多播锁,这就是我的研究表明我需要做的所有事情,我可以看到我有锁,但应用程序仍然没有收到数据。
var wifiManager = var wifiManager = (WifiManager)Application.Context.GetSystemService(Context.WifiService);
// Acquire the lock
if (wifiManager != null)
{
// _lock is a private member WifiManager.MulticastLock _lock
_lock = wifiManager.CreateMulticastLock("PiEar");
if (_lock != null)
{
_lock.Acquire();
}
else
{
Debug.WriteLine("Could not acquire multicast lock"); // Does not print, meaning I do acquire the lock (and _lock.IsHeld() returns true)
}
}
else
{
Debug.WriteLine("WifiManager is null"); // Does not print, meaning it is not null
}
如果我随后使用不同的应用程序 MSniffer,它不仅会接收数据,而且还允许我的应用程序接收数据。我在尝试获取锁时做错了什么吗?
另外,我有以下权限:
- ACCESS_WIFI_STATE
- CHANGE_WIFI_STATE
- ACCESS_NETWORK_STATE
- 互联网
- CHANGE_WIFI_MULTICAST_STATE
编辑 1:更多信息
.cs
页面的相关代码如下。 _receivedMessages
是一个 ListView
,让我可以直观地看到接收到的数据。上面的代码就是下面line 2
上的运行。依赖服务中唯一的代码就是获取锁的代码。
var service = DependencyService.Get<IMulticastLock>();
service.Acquire();
Task.Run(async () =>
{
while (true)
{
try
{
var endpoint = new IPEndPoint(IPAddress.Parse("224.0.0.69"), 6666);
var multicast = new UdpClient(endpoint);
while (true)
{
var multicastBytes = await multicast.ReceiveAsync();
var message = Encoding.UTF8.GetString(multicastBytes.Buffer);
Debug.WriteLine($"Received: {message}");
Device.BeginInvokeOnMainThread(() =>
{
_receivedMessages.Add(message);
});
}
}
catch (Exception e)
{
Debug.WriteLine(e);
}
}
});
检查 IP 地址和端口。
您可以尝试使用下面关于如何创建和配置 UdpClient 的代码。
public class MulticastUdpClient { UdpClient _udpclient; int _port; IPAddress _multicastIPaddress; IPAddress _localIPaddress; IPEndPoint _localEndPoint; IPEndPoint _remoteEndPoint; public MulticastUdpClient(IPAddress multicastIPaddress, int port, IPAddress localIPaddress = null) { // Store params _multicastIPaddress = multicastIPaddress; _port = port; _localIPaddress = localIPaddress; if (localIPaddress == null) _localIPaddress = IPAddress.Any; // Create endpoints _remoteEndPoint = new IPEndPoint(_multicastIPaddress, port); _localEndPoint = new IPEndPoint(_localIPaddress, port); // Create and configure UdpClient _udpclient = new UdpClient(); // The following three lines allow multiple clients on the same PC _udpclient.ExclusiveAddressUse = false; _udpclient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); _udpclient.ExclusiveAddressUse = false; // Bind, Join _udpclient.Client.Bind(_localEndPoint); _udpclient.JoinMulticastGroup(_multicastIPaddress, _localIPaddress); // Start listening for incoming data _udpclient.BeginReceive(new AsyncCallback(ReceivedCallback), null); } /// <summary> /// Send the buffer by UDP to multicast address /// </summary> /// <param name="bufferToSend"></param> public void SendMulticast(byte[] bufferToSend) { _udpclient.Send(bufferToSend, bufferToSend.Length, _remoteEndPoint); } /// <summary> /// Callback which is called when UDP packet is received /// </summary> /// <param name="ar"></param> private void ReceivedCallback(IAsyncResult ar) { // Get received data IPEndPoint sender = new IPEndPoint(0, 0); Byte[] receivedBytes = _udpclient.EndReceive(ar, ref sender); // fire event if defined if (UdpMessageReceived != null) UdpMessageReceived(this, new UdpMessageReceivedEventArgs() { Buffer = receivedBytes }); // Restart listening for udp data packages _udpclient.BeginReceive(new AsyncCallback(ReceivedCallback), null); } /// <summary> /// Event handler which will be invoked when UDP message is received /// </summary> public event EventHandler<UdpMessageReceivedEventArgs> UdpMessageReceived; /// <summary> /// Arguments for UdpMessageReceived event handler /// </summary> public class UdpMessageReceivedEventArgs: EventArgs { public byte[] Buffer {get;set;} } }