开发者

Does Windows Phone 7 Mango support UDP broadcast?

开发者 https://www.devze.com 2023-04-11 18:40 出处:网络
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# wo

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.SocketErro开发者_开发技巧r); 
  // 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号