reverse connection in C #

1

I'm having difficulty using reverse connection between client and server. Whenever I try to connect using Dns or Ip does not connect, if I use LocalHost, 127.0.0.1, it connects perfectly. It can already "resolve" the DNS IP What do I need to do to be able to connect?

private static Socket _clientSocket = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);



        public static TcpClient client;
        private const int _PORT = 28562; //  porta


         public static string connectTo = "decodertm.ddns.net";// Não conecta
        // public static string connectTo = "127.0.0.1";// conecta

        public static IPAddress ipaddress = null;

.

 private static void ConnectToServer()
        {

            int attempts = 0;


            bool IsValidIP = IPAddress.TryParse(connectTo, out ipaddress);

            if (IsValidIP == false)
            {

                ipaddress = Dns.GetHostAddresses(connectTo)[0];
                //   Console.WriteLine(Dns.GetHostAddresses(connectTo)[0]);


            }  
            client = new TcpClient();


            while (!_clientSocket.Connected)
            {                 
                try 
                {                     
                    attempts++;
                    Console.WriteLine("Tentativas de conectar " + attempts); 
               _clientSocket.Connect("177.201.126.8", _PORT);         
                    Thread.Sleep(100);  

                }
                catch (SocketException)  
                {
                    Console.Clear();
                }
            }
            Console.Clear();
            Console.WriteLine("Conectado !");

            }
    
asked by anonymous 26.07.2018 / 07:29

1 answer

1

If in localhost it connects perfectly and dynamic dns does not connect (probably it is configured with your IP), this is because of your network!

If the DDNS IP is yours:

You must configure your network correctly, it must be in NAT mode to have a connection. If you are not in NAT mode, you will not connect!

I do not teach you to do this, because the routers, modens and more, are different configurations, look at the model of the same and give a researched about NAT!

If you are trying to connect directly to the host:

Try using:

var ips = System.Net.Dns.GetHostEntry("microsoft.com").AddressList;
foreach (var ip in ips)
    Console.WriteLine(ip);

It will return an ip or more for you for you!

.

I do not know if I can help you, but I hope so!

    
26.07.2018 / 07:52