我想从手机发送UDP数据包到有限的广播地址(IPAddress.Broadcast = 255.255.255.255).
这是我到目前为止,它适用于Windows应用程序:
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(delegate(object s, SocketAsyncEventArgs e) { Console.WriteLine(e.SocketError); }); socket.SendToAsync(a);
需要SetSocketOption调用以防止"拒绝访问"异常.不幸的是,这种方法似乎不适用于WP7.App Hub社区站点上提供的UDP示例代码使用多播来实现类似的结果,但我尝试联系的设备无法处理多播.
有没有办法在芒果上做这种广播?