zoukankan      html  css  js  c++  java
  • WCF 第十二章 对等网 使用PNRP注册名字

    WCF可以使用PNRP在一个网状网络上寻找其他参与者。在它的实现,WCF对等信道从使用PNRP中抽象出来所以一个应用程序不需要直接与PNRP打交道。然而,一些对等应用程序可能想要在WCF对等信道外面自己发布并解决标识符(对等名字)。不幸的是,在.NET Framework3.5之前没有任何方式从托管代码中注册PNRP名字。.NET Framework3.5中添加了一个新的叫做System.Net.Peer命名空间来使用托管代码使用PNRP结构。

    System.Net.Peer

    正如之前提到的,PNRP用来发布并解决对等名称。为了发布一个对等名称,我们首先需要创建类PeerName的一个实例。PeerName确定了标识符(对等名字)以及标识符是安全的还是不安全的。我们使用PeerNameRegistration类来注册对等名字。为了实现这个我们需要设置PeerName和Port属性然后调用Start方法。Stop方法用来反注册对等名字。列表12.5系那是注册一个对等名字的例子。

    注意 应用程序拥有对等名字
    应用程序拥有一个对等名字并注册它。如果应用程序因为一些原因退出了,对等名字会被反注册。这意味着应用程序为了解决一个对等名字必须一致运行。

    列表12.5 发布一个对等名字

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Net.PeerToPeer;
    
    namespace PublishName
    {
        class Program
        {
            static void Main(string[] args)
            {
                PeerName peerName = new PeerName("PeerChat", PeerNameType.Unsecured);
                PeerNameRegistration pnReg = new PeerNameRegistration();
                pnReg.PeerName = peerName;
                pnReg.Port = 8080;
                pnReg.Comment = "My Registration";
                pnReg.Data = Encoding.UTF8.GetBytes("Some data to include with my registration.");
                pnReg.Start();
                Console.WriteLine("Hit [Enter] to exit.");
                Console.ReadLine();
                pnReg.Stop();
            }
        }
    }

      列表12.6显示了如果解析列表12.5中显示的同样的对等名字。在这个例子中我们使用PeerNameResolver类来获得一个PeerNameRecord实例集合。我们接下来遍历这个集合并输出包含在每条记录中的信息。

    列表12.6 解析一个对等名字

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Net.PeerToPeer;
    using System.Net;
    
    namespace ResolveName
    {
        class Program
        {
            static void Main(string[] args)
            {
                PeerNameResolver resolver = new PeerNameResolver();
                PeerName peerName = new PeerName("0.PeerChat");
                PeerNameRecordCollection results = resolver.Resolve(peerName);
                PeerNameRecord record;
    
                for (int i = 0; i < results.Count; i++)
                {
                    record = results[i];
                    Console.WriteLine("Record #{0}", i);
                    if (record.Comment != null)
                    {
                        Console.WriteLine(record.Comment);
                    }
                    Console.WriteLine("Data: ");
                    if (record.Data != null)
                    {
                        Console.WriteLine(Encoding.ASCII.GetString(record.Data));
                    }
                    else
                    {
                        Console.WriteLine();
                    }
                    Console.WriteLine("Endpoints:");
                    foreach (IPEndPoint endpoint in record.EndPointCollection)
                    {
                        Console.WriteLine("Endpoint:{0}", endpoint);
                    }
                    Console.WriteLine();
                }
                Console.WriteLine("Hit [Enter] to exit.");
                Console.ReadLine();
            }
        }
    }


    作者:DanielWise
    出处:http://www.cnblogs.com/danielWise/
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

  • 相关阅读:
    GoogLeNet学习笔记
    ResNet学习笔记
    VGG学习笔记
    AlexNet学习笔记
    目标检测SSD
    YOLO系列(1)—— YOLO v1
    YOLO系列(2)—— YOLO v2
    R-CNN系列(4)—— Faster R-CNN
    如何打开.ipynb文件
    机器学习中的采样
  • 原文地址:https://www.cnblogs.com/danielWise/p/2028606.html
Copyright © 2011-2022 走看看