代码:
using System.Threading.Tasks;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
using System.Net.Sockets;
using System.Net;
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class ItemsPage : ContentPage
{
public async static Task<String> UDP_receive()
{
var myUDP = new UdpClient(8888);
myUDP.EnableBroadcast = true;
var myResult = await myUDP.ReceiveAsync();
myUDP.Close();
return Encoding.ASCII.GetString(myResult.Buffer) + " : " + myResult.RemoteEndPoint.Address.ToString();
}
public async static Task UDP_Send()
{
var myUDP = new UdpClient();
var RequestData = Encoding.ASCII.GetBytes(DateTime.Now.ToLongTimeString());
myUDP.EnableBroadcast = true;
await myUDP.SendAsync(RequestData, RequestData.Length, new IPEndPoint(IPAddress.Broadcast, 8888));
myUDP.Close();
}
void Button1_click(object sender, EventArgs e)
{
StartListen();
StartSending();
}
async Task StartListen()
{
Exception eXX = null;
while (eXX == null)
{
try
{
myText.Text += Environment.NewLine + await UDP_receive();
}
catch (Exception ex2)
{
eXX = ex2;
log(ex2);
}
}
}
async Task StartSending()
{
Exception eXX = null;
while (eXX == null)
{
try
{
await MyData.UDP_Send();
await Task.Delay(1000);
}
catch (Exception ex2)
{
eXX = ex2;
log(ex2);
}
}
}
}
Android输出:
上午11:57:11:192.168.1.155
上午11:57:11:192.168.1。 100
上午11:57:12:192.168.1.155
上午11:57:12:192.168.1。 100
上午11:57:13:192.168.1.155
上午11:57:13:192.168.1。 100
....
注意:192.168.1.100是Windows PC IP
UWP输出:
上午11:57:11:192.168.1.100
上午11:57:12:192.168.1.100
上午11:57:13:192.168.1.100
上午11:57:14:192.168.1.100
上午11:57:15:192.168.1.100
....
答案 0 :(得分:1)
要接收UDP广播,请尝试将创建与绑定分开。例如,
var myUDP = new UdpClient()
{
ExclusiveAddressUse = false,
EnableBroadcast = true
};
IPEndPoint localIPEndPoint = new IPEndPoint(myIP.BroadcastIPAddress, 8888);
myUDP.Client.Bind(localIPEndPoint);
根据我的经验,看起来像UdpClient(8888)这样的短格式构造函数过早地绑定了底层套接字。这样可以防止更改EnableBroadcast属性。
在各个平台之间,我发现接收广播的唯一可靠方法是使用子网的实际广播地址。捕获信息的陷阱很容易(?),如下所示。
public class BroadcastAddress
{
public IPAddress IPAddress { get; set; }
public IPAddress BroadcastIPAddress { get; set; }
public NetworkInterfaceType NetworkInterfaceType { get; set; }
}
public static IEnumerable<BroadcastAddress> GetBroadcastAddresses()
{
List<BroadcastAddress> broadcastAddresses = new List<BroadcastAddress>();
NetworkInterface[] networkInterfaces = NetworkInterface.GetAllNetworkInterfaces();
foreach (NetworkInterface networkInterface in networkInterfaces)
{
if (networkInterface != null
&& (networkInterface.NetworkInterfaceType == NetworkInterfaceType.Wireless80211
|| networkInterface.NetworkInterfaceType == NetworkInterfaceType.Ethernet))
{
IPInterfaceProperties iPInterfaceProperties = networkInterface.GetIPProperties();
UnicastIPAddressInformationCollection unicastIPAddressInformationCollection = iPInterfaceProperties.UnicastAddresses;
foreach (UnicastIPAddressInformation unicastIPAddressInformation in unicastIPAddressInformationCollection)
{
if (unicastIPAddressInformation.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
{
IPAddress broadcastIPAddress =
new IPAddress(unicastIPAddressInformation.Address.Address | (UInt32.MaxValue ^ (UInt32)(unicastIPAddressInformation.IPv4Mask.Address)));
broadcastAddresses.Add(new BroadcastAddress()
{
IPAddress = unicastIPAddressInformation.Address,
BroadcastIPAddress = broadcastIPAddress,
NetworkInterfaceType = networkInterface.NetworkInterfaceType
});
}
}
}
}
return broadcastAddresses;
}
在处理移动设备时,只需使用Wireless80211广播地址。在笔记本电脑或工作站上,它可能变得稍微复杂一些。这些设备可以具有多个网络接口以及每个接口上的多个子网。因此,返回列表的原因。而且,这意味着您可能需要具有多个UdpClient实例,每个子网一个。
这对于OP来说可能为时已晚。希望这些信息可以帮助其他疲倦的搜索者尝试使广播正常工作(并且他们可以停止在桌子上敲打额头)。