reverse connection in C#

Asked

Viewed 133 times

1

I’m having a hard time using a 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 "solve" the DNS IP What I need to do 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 !");

            }

1 answer

1

If localhost 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 for there to be a connection. If it is not in NAT mode, it will not connect!

I don’t teach you how to do this, because routers, modems and more are different settings, look at the model of it and do a NAT search!

If you are trying to connect directly to the host:

Try to use:

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

Will return an ip or more to you for you!

.

I don’t know if I can help you, but I hope so!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.