Exemple #1
0
        /*Pings the entered address to see if the address is on the network.*/
        public static bool CheckAddress(string addr)
        {
            Net pinger = new Net();

            pinger.PingAddress(addr); // ping the supplied address.

            if (scount >= 1)
            {
                return true;
            }

            return false;
        }
Exemple #2
0
        public override void Run()
        {
            var     connected = false;
            NetPeer?peer      = null;

            Console.WriteLine("Stating Client...");
            RequestRelayServerIp();

            var serverIp = RequestServerIp();

            var globalServer = new IPEndPoint(IPAddress.Parse(RelayServerIp), 4240);

            var natPunchListener = new EventBasedNatPunchListener();

            natPunchListener.NatIntroductionSuccess += (point, _, _) =>
            {
                Console.WriteLine("Nat Introduction Success, Connecting to " + point);
                peer      = Net.Connect(point, "CSM");
                connected = true;
            };

            var netListener = new EventBasedNetListener();

            netListener.NetworkReceiveEvent += (peer, reader, method) =>
            {
                var message = reader.GetString();
                Console.WriteLine($"[{peer}] {message}");
            };

            netListener.PeerConnectedEvent += netPeer =>
            {
                Console.WriteLine("Connected to server: " + netPeer.EndPoint);
            };

            netListener.NetworkErrorEvent += (point, error) =>
            {
                Console.WriteLine("Network Error: " + error);
            };

            Net = new NetManager(netListener);
            Net.NatPunchEnabled            = true;
            Net.UnconnectedMessagesEnabled = true;
            Net.NatPunchModule.Init(natPunchListener);

            Net.Start();
            Net.NatPunchModule.SendNatIntroduceRequest(globalServer, $"client_{serverIp}");

            var running = true;

            while (running)
            {
                Net.NatPunchModule.PollEvents();
                Net.PollEvents();

                // Wait till connected
                if (peer == null)
                {
                    Thread.Sleep(100);
                    continue;
                }

                Console.WriteLine("Sending ping:");
                peer.Send(NetDataWriter.FromString("Ping from client"), DeliveryMethod.Unreliable);

                Thread.Sleep(100);
            }

            Net.Stop();
        }