我正在使用 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);
}
}
});
uj5u.com熱心網友回復:
檢查 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;} } }
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/466104.html
