PeerCollaboration.GetPeersNearMe 方法

定义

返回一个集合,其中包含当前在调用对等方的网络子网中登录的所有 PeerCollaboration 终结点。

public:
 static System::Net::PeerToPeer::Collaboration::PeerNearMeCollection ^ GetPeersNearMe();
[System.Security.SecurityCritical]
public static System.Net.PeerToPeer.Collaboration.PeerNearMeCollection GetPeersNearMe ();
[<System.Security.SecurityCritical>]
static member GetPeersNearMe : unit -> System.Net.PeerToPeer.Collaboration.PeerNearMeCollection
Public Shared Function GetPeersNearMe () As PeerNearMeCollection

返回

一个 PeerNearMeCollection,其中包含当前在“网络邻居”范围中登录的所有 PeerCollaboration 终结点。

如果未在子网上发现对等方,将返回一个大小为零 (0) 的集合。

属性

例外

该对等方尚未登录“网络邻居”范围。

此方法在尝试枚举所有已知的网络邻居终结点时返回了无效的值。

示例

以下代码示例演示如何将 PeersNearMe 检索为 PeerNearMeCollection:

//This function retrieves the peers near me as a PeerNearMeCollection.
public static PeerNearMeCollection GetPeersNearMe()
{
    PeerNearMeCollection peers = null;
    try
    {
        peers = PeerCollaboration.GetPeersNearMe();
        if (peers == null ||
            peers.Count == 0)
        {
            Console.WriteLine("There are no peers near me.");
        }
        foreach (PeerNearMe pnm in peers)
        {
            Console.WriteLine("Getting the peers near me: {0}, nickname {1}, isOnline {2}",
                pnm.ToString(),
                pnm.Nickname,
                pnm.IsOnline);
        }
    }
    catch (PeerToPeerException p2pEx)
    {
        Console.WriteLine("Could not obtain an enumeration of the peers near me: {0}", p2pEx.Message);
    }
    catch (InvalidOperationException ioEx)
    {
        Console.WriteLine("The application is no longer signed into the Peer Collaboration Infrastructure: {0}",
            ioEx.Message);
    }
    catch (Exception ex)
    {
        Console.WriteLine("Unexpected exception caught when trying to enumerate the peers near me: {0}",
            ex.Message);
    }

    return peers;
}

注解

通过此方法返回完全填充 PeerNearMeCollection 的 可能需要在登录后不确定的时间量,具体取决于网络拓扑和延迟。

调用此方法需要 PermissionStateUnrestricted。 协作会话开始时会创建 Peer 此状态。

适用于