• 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);
                        }
                    }
                }
            }

        }
    }

  • 相关阅读:
    node.js 基础篇
    node.js 开发环境搭建
    Velocity VelocityEngine 支持多种loader 乱码问题
    tomcat集群及session共享
    上海支付宝终面后等了两周,没能收到offer却来了杭州淘宝的电话面试
    支付宝hr终面,忐忑的等待结果
    mysql 数据范围总结
    rsync同步文件(多台机器同步代码...)
    linux基础命令
    路飞学城项目之前后端交互,跨域问题
  • 原文地址:https://www.cnblogs.com/Mayvar/p/wanghonghua_201201171129.html
Copyright © 2020-2023  润新知