首页 windows phone 7上获取本地IP的方法

windows phone 7上获取本地IP的方法

举报
开通vip

windows phone 7上获取本地IP的方法windows phone 7上获取本地IP的方法 using System; using System.Net; using System.Net.Sockets; using System.Diagnostics; using System.Linq; using System.Text; namespace FindMyIP { public class MyIPAddress { Action FoundCallback; UdpAnySourceMulticastClient Mu...

windows phone 7上获取本地IP的方法
windows phone 7上获取本地IP的方法 using System; using System.Net; using System.Net.Sockets; using System.Diagnostics; using System.Linq; using System.Text; namespace FindMyIP { public class MyIPAddress { Action FoundCallback; UdpAnySourceMulticastClient MulticastSocket; const int PortNumber = 50000; // pick a number, any number string MulticastMessage = "FIND-MY-IP-PLEASE" + new Random().Next().ToString(); public void Find(Action callback) { FoundCallback = callback; MulticastSocket = new UdpAnySourceMulticastClient(IPAddress.Parse("239.255.255.250"), PortNumber); MulticastSocket.BeginJoinGroup((result) => { try { MulticastSocket.EndJoinGroup(result); GroupJoined(result); } catch (Exception ex) { Debug.WriteLine("EndjoinGroup exception {0}", ex.Message); // This can happen eg when wifi is off FoundCallback(null); } }, null); } void callback_send(IAsyncResult result) { } byte[] MulticastData; bool keepsearching; void GroupJoined(IAsyncResult result) { MulticastData = Encoding.UTF8.GetBytes(MulticastMessage); keepsearching = true; MulticastSocket.BeginSendToGroup(MulticastData, 0, MulticastData.Length, callback_send, null); while (keepsearching) { try { byte[] buffer = new byte[MulticastData.Length]; MulticastSocket.BeginReceiveFromGroup(buffer, 0, buffer.Length, DoneReceiveFromGroup, buffer); } catch (Exception ex) { Debug.WriteLine("Stopped Group read due to " + ex.Message); keepsearching = false; } } } void DoneReceiveFromGroup(IAsyncResult result) { IPEndPoint where; int responselength = MulticastSocket.EndReceiveFromGroup(result, out where); byte[] buffer = result.AsyncState as byte[]; if (responselength == MulticastData.Length && buffer.SequenceEqual(MulticastData)) { Debug.WriteLine("FOUND myself at " + where.Address.ToString()); keepsearching = false; FoundCallback(where.Address); } } } } Here is a sample button handler to put in a trivial Silverlight test app: private void button1_Click(object sender, RoutedEventArgs e) { MyIPAddress finder = new MyIPAddress(); finder.Find((address) => { Dispatcher.BeginInvoke(() => { button1.Content = address == null ? "Unknown" : address.ToString(); }); }); }
本文档为【windows phone 7上获取本地IP的方法】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
该文档来自用户分享,如有侵权行为请发邮件ishare@vip.sina.com联系网站客服,我们会及时删除。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。
下载需要: 免费 已有0 人下载
最新资料
资料动态
专题动态
is_477730
暂无简介~
格式:doc
大小:16KB
软件:Word
页数:0
分类:
上传时间:2017-09-30
浏览量:6