I want to send a UDP packet from a phone to the limited broadcast address (IPAddress.Broadcast = 255.255.255.255).
This is what I have so far, and it works in a Windows app:
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, true);
byte[] data = Encoding.UTF8.GetBytes("test data");
SocketAsyncEventArgs a = new SocketAsyncEventArgs();
a.RemoteEndPoint = new IPEndPoint(IPAddress.Broadcast, 11000);
a.SetBuffer(data, 0, data.Length);
a.Completed += new EventHandler<SocketAsyncEventArgs>(delegate(object s, SocketAsyncEventArgs e)
{
Console.WriteLine(e.SocketError);
});
socket.SendToAsync(a);
The SetSocketOption call is required in order to prevent an "access denied" exception. Unfortunately that method doesn't seem to be available on WP7. The UDP sample code given on the App Hub community site is using multicast to achieve similar results, but the device I'm trying to contact isn't able to deal with multicast.
Is there any way to do this sort of broadcast on Mango?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…