#眉標=.NET 3.5、P2P、PNRP #副標=.NET Framework 3.5應用開發(2) #大標=實作註冊PNRP Peer Name #作者=文/圖 黃嘉輝 ====<反灰>=========== using System.Net.PeerToPeer; ================= ====<反灰>=========== using System.Net.PeerToPeer.Collaboration; ================= ====<反灰>=========== public PeerName(string remotePeerName) public PeerName(string classifier, PeerNameType peerNameType) ================= ====<反灰>=========== public PeerNameRegistration() public PeerNameRegistration(PeerName name, int port) public PeerNameRegistration(PeerName name, int port, Cloud cloud) ================= =============== 程式1 ================= =============== 程式2 // 建立PeerName物件 // 設定PNRP Peer Name為未受保護的名稱 PeerName peerName = new PeerName(txtPeerName.Text, PeerNameType.Unsecured); // 以PNRP Peer Name與通訊埠建立PeerNameRegistration peerNameRegistration = new PeerNameRegistration(peerName, Int32.Parse(txtPort.Text)); // 設定PNRP Peer Name的其他資訊 peerNameRegistration.Comment = "PNRP Peer Name的其他資訊"; // 設定PeerNameRegistration物件之二進位資料 peerNameRegistration.Data = System.Text.Encoding.UTF8.GetBytes("二進位資料"); // 設定用戶端或Peer端點目前參與的所有連結本機之PNRP Cloud peerNameRegistration.Cloud = Cloud.AllLinkLocal; // 設定是否自動選取IP位址和通訊埠 peerNameRegistration.UseAutoEndPointSelection = true; // 將PNRP Peer Name註冊至PNRP Cloud中 peerNameRegistration.Start(); ================= =============== 程式3 // 取得目前所有有效的PNRP Cloud CloudCollection clouds = Cloud.GetAvailableClouds(); if (clouds.Count > 0) lstCloud.Items.Clear(); else MessageBox.Show("No Clouds Found", "P2P", MessageBoxButton.OK, MessageBoxImage.Error); foreach (Cloud cloud in clouds) { // 取得PNRP Cloud的名稱與網路範圍 lstCloud.Items.Add(cloud.Name + ", " + cloud.Scope); } ================= =============== 程式4 private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e){ // 自PNRP Cloud中移除PNRP Peer Name之註冊 peerNameRegistration.Stop(); } ================= 2