简体   繁体   中英

Does Windows Phone 7 Mango support UDP broadcast?

According to the MSDN documentation Mango does not support UDP broadcast. According to this thread it is somehow possible. Does anybody have any experience with UDP on Phone 7? A code snippet in C# would be appreciated.

EDIT: We made some further investigations. The following code seems to work

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); 
  // here you can call socket.SendToAsync(sendEventArgs);
}); 

socket.ConnectToAsync(a);    

It is essential to call ConnectToAsync before SendToAsync, otherwise you get an access denied exception. UDP seems to work somehow, at least in the emulator. The question is, will it work in real live and why the documentation says it doesn't?

For Windows Phone OS 7.1, TCP unicast, UDP unicast, and UDP multicast clients are supported (OS 7.1 means Windows Phone 7.5/Mango)

Here is link to documentation about the Socket Class: http://msdn.microsoft.com/en-us/library/attbb8f5(v=VS.95).aspx

Here is link to a blog with sample code: http://www.pitorque.de/MisterGoodcat/post/Windows-Phone-7-Mango-Sockets.aspx

And even more sample code under "09-DemoCode Networking" in http://borntolearn.mslearn.net/wpmango/m/mediagallery/default.aspx

Here is another message that might inspire you: How to broadcast a UDP packet on WP7 Mango?

There has been reported som OS firmware with bad UDP performance: http://connect.microsoft.com/VisualStudio/feedback/details/690198/poor-udp-performance-in-windows-phone-7-mango

The code above doesn't work in WP7, this for the simple reason that SetSocketOption is defined in C# but not in Silverlight. Therefore the above might work in your computer but it won't even compile on WP7!

As for the practical answer I think Ronny has answered well "TCP unicast, UDP unicast, and UDP multicast clients are supported", unless someone can post some code which supports UDP broadcast on WP7 the answer remains "NO".

I think this thread should be closed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM