Пример #1
0
        static void Main(string[] args)
        {
            ConsoleHelper.WriteLine("输入s启动服务器", ConsoleColor.DarkGreen);

            var input = ConsoleHelper.ReadLine();

            if (string.IsNullOrEmpty(input) || input.ToLower() != "s")
            {
                ConsoleHelper.WriteLine("输入服务器地址,例如:180.122.325.21:39654", ConsoleColor.DarkGreen);

                var ipPort = ConsoleHelper.ReadLine();

                Peer peer = new Peer();
                peer.OnPublicNatInfoResponse += Peer_OnPublicNatInfoResponse;;
                peer.OnP2pSucess             += Peer_OnP2PSucess;
                peer.OnP2pFailed             += Peer_OnP2pFailed;
                peer.OnMessage            += Peer_OnMessage;
                peer.OnServerDisconnected += Peer_OnServerDisconnected;
                peer.OnP2pDisconnected    += Peer_OnP2pDisconnected;
                peer.ConnectPeerServer(ipPort);

                ConsoleHelper.WriteLine("回车查看当前的NatInfo", ConsoleColor.DarkGreen);
                ConsoleHelper.ReadLine();
                peer.RequestPublicNatInfo();

                ConsoleHelper.WriteLine("输入PeerB地址发起p2p请求,例如:180.122.325.21:21541", ConsoleColor.DarkGreen);

                var pIPPort = ConsoleHelper.ReadLine();

                ConsoleHelper.WriteLine($"正在向{pIPPort}发起P2p请求。。。", ConsoleColor.DarkGreen);
                peer.RequestP2p(pIPPort);

                ConsoleHelper.WriteLine($"与{peer.RemoteNatInfo.ToString()} P2p建立{(peer.IsConnected ? "成功" : "失败")}", ConsoleColor.DarkGreen);

                TaskHelper.Start(() =>
                {
                    while (peer.IsConnected)
                    {
                        peer.SendMessage("hello p2p");

                        ThreadHelper.Sleep(1000);
                    }
                });
            }
            else
            {
                NATServer p2pServer = new NATServer();
                p2pServer.Start();
                ConsoleHelper.WriteLine("回车关闭测试", ConsoleColor.DarkGreen);
            }

            ConsoleHelper.ReadLine();
        }
Пример #2
0
        private static void NATServer()
        {
            NATServer host = new NATServer(new NetConfiguration(5001)
            {
                Port            = 5000,
                AllowConnectors = true,
                MaxConnections  = 5,
                Name            = "NATServer",
                Timeout         = 1,
                NetworkRate     = 50
            }, 1024);

            host.Start();


            while (isPooling)
            {
                host.Tick();
                Thread.Sleep(50);
            }
        }