• C# p2p(1)


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Net;
    using System.Net.Sockets;
    using System.Net.PeerToPeer;

    namespace ConsoleApplication4
    {
        class Program
        {
            static string strMYPeername = "myApplicationName";
            static string strAllMyPeername = "";
            static int port;
            static PeerNameRegistration peernameregistration;

            static void Main(string[] args)
            {
                registerPeer();
                resoveName("0."+strMYPeername);
                Console.Read();
            }

            public static void getPort()
            {
                while (true)
                {
                    port = new Random().Next(50000,51000);
                    try
                    {
                        TcpListener listener = new TcpListener(IPAddress.Any, port);
                        listener.Start();
                    }
                    catch
                    {
                        continue;
                    }
                    break;
                }
            }

            //把PNRP NAME 注册到Cloud中

            public static void registerPeer()
            {
                getPort();//获取要连线的IP&PORT

                PeerName peername = new PeerName(strMYPeername, PeerNameType.Unsecured);
               

                //用peername创建注册群,然后把peername注册到群中

                peernameregistration = new PeerNameRegistration(peername, port);
                peernameregistration.Comment = "pnrp peer name 的其他描述信息";
                peernameregistration.Data = Encoding.UTF8.GetBytes(string.Format("註冊時間{0}",DateTime.Now.ToString()));
                strAllMyPeername = peername.ToString();
                peernameregistration.Start();
            }

            public static void resolverPeer(string myPeername)
            {
                PeerName peerName = new PeerName("0." + myPeername);
                PeerNameResolver resolver = new PeerNameResolver();
                PeerNameRecordCollection pmrcs = resolver.Resolve(peerName);
                foreach (PeerNameRecord pmrc in pmrcs)
                {
                    foreach (IPEndPoint endpoint in pmrc.EndPointCollection)
                    {
                        Console.WriteLine(endpoint);
                    }
                }
            }

           ///解析对等名称

            public static void resoveName(string strPeerName)
            {
                PeerName mypeer = new PeerName(strPeerName);
                PeerNameResolver myRes = new PeerNameResolver();
                PeerNameRecordCollection recColl = myRes.Resolve(mypeer);
                foreach (var record in recColl)
                {
                    foreach (var endp in record.EndPointCollection)
                    {
                        if (endp.AddressFamily.Equals(AddressFamily.InterNetwork))
                        {
                            Console.WriteLine(record.PeerName.ToString());
                            Console.WriteLine(endp.ToString());
                            Console.WriteLine(Encoding.UTF8.GetString(record.Data));
                            Console.WriteLine(record.PeerName.PeerHostName);
                        }
                    }
                }
            }

        }
    }

  • 相关阅读:
    noi 1944 吃糖果
    noi 6049 买书
    noi 2985 数字组合
    noi 2728 摘花生
    noi 2718 移动路线
    noi 4977 怪盗基德的滑翔翼
    noi 8780 拦截导弹
    noi 1996 登山
    NOI 动态规划题集
    图的色数
  • 原文地址:https://www.cnblogs.com/Mayvar/p/wanghonghua_201201171129.html
Copyright © 2020-2023  润新知