Problems connecting to my computer using external ip

1

I am trying to make a java application that will serve as a server for an android application to establish a Socket communication with the computer that my program is open on the port that it is listening to (I chose 50000). everything is correct:

  • A port forwarding is configured on the router using the uPnP protocol
  • The program is open and listening on the correct port (port 50000 is LISTENING in the netstat command)
  • My external ip is resolved on a no-ip site host, and when I ping the host, the correct external ip appears
  • Currently, even the application is able to connect and at first (while my modem is 'happy' or something like that) everything works, including external networks and everything seems ok.

but after a few hours with the program running the computer magically becomes inaccessible. I checked all possible (will it really be all?) causes

  • no IP change
  • the port remains accessible (I've tested it on several sites that check if the ports are open)
  • I can ping my host and it still directs to the correct external ip ...
  • The application can still connect if host ip (192.168.1.8) is used

But things get complicated when I try telnet:

  • telnet to 192.168.1.8 50000 works (local host)
  • for 127.0.0.1 50000 also works (also local host?)
  • for localhost 50000 also works (obviously localhost)
  • but for the external ip, or modem ip (192.168.1.1) does not, it says:

"Connecting to 177.203.28.xxx .... Could not open host connection on port 50000: Connection failed" and telnet on the modem address, it also does not work (I think it should work since the port is forwarded) including telnet without specifying the port of the same message ... (when everything is working, telnet command without specifying port connects in my modem and asks to put a username and password)

The server-side java code that opens ServerSocket looks like this:

@Override
public void run()
{
    try {
        Inicie();
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        Inicio.Erro("Erro no Server:"+e.getMessage());
        e.printStackTrace();
    }
    System.out.println("SocketServerThread Parou");
    Inicio.LOG("Server Parou");
}   
public void Inicie() throws InterruptedException
{
    connect();

    while(KeepWaiting)
    {
        WaitClient();
    }
} 
public void connect()
{
    try {
        //  serverSocket = new ServerSocket(50000);
            serverSocket =new ServerSocket(50000);//,0,InetAddress.getByAddress(new byte[]{(byte)172,0,0,1}));
            System.out.println("Socket Servidor Pplware");
            System.out.println("A escuta na porta:\n"+InetAddress.getLocalHost()+":"+serverSocket.getLocalPort());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
}
public void WaitClient() throws InterruptedException{
    try {
            Inicio.LOG("Aguardando Conexao...");
            Socket socket = serverSocket.accept(); // blocking wait ( 99.9999% of the time in this line )
            BufferedReader buff_Reader = new BufferedReader(new InputStreamReader(socket.getInputStream(),Charset.forName("UTF-8")));
            BufferedWriter buff_Writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(),Charset.forName("UTF-8")));
            System.out.println("<Conectou>");
            System.out.println("IP Cliente: " + socket.getInetAddress());
            Inicio.LOG("Conectou -> IP Cliente: " + socket.getInetAddress());
            ReadThread reader_thread = new ReadThread(buff_Reader);
            WriteThread writer_thread = new WriteThread(buff_Writer);
            ConexaoCliente nova = new ConexaoCliente(this,reader_thread,writer_thread,socket);
            Conexoes.add(nova);
            System.out.println("Conexões atuais:"+Conexoes.size());
            nova.start(); // starta a thread que mantem a conexão com aquele cliente
        } catch (IOException e ){//SQLException e) {
            System.out.println("<Erro>");
            Inicio.Erro("Server Parou com Erro:"+e.getMessage());
            e.printStackTrace();
            KeepWaiting=false;
        }
        finally{

        }
}

The code in the android application that establishes the Socket connection looks like this:

   @Override
public void run()
{
    try {
        connect();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}
public void connect() throws InterruptedException
{
    try {
        exaustive_connect();
        if(connection.isConnected()&&Continuar) {
            BufferedReader buff_reader = new BufferedReader(new InputStreamReader(new myInputStream(connection.getInputStream()), Charset.forName("UTF-8")));
            BufferedWriter buff_writer = new BufferedWriter(new OutputStreamWriter(new myOutputStream(connection.getOutputStream()),Charset.forName("UTF-8")));
            Reader = new ReadThread(buff_reader);
            Writer = new WriteThread(buff_writer);
            Comunicar(); // método que escuta e envia mensagens usando esse Reader e Writer.
        }
        else
        {
            ManageConection.E("ConexaoServidor","Não foi possivel conectar");
        }
    } catch (UnknownHostException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally{
        close(); // método que fecha o socket, o Reader e Writer
    }

}
private void exaustive_connect()
{
    try {
        if(!exaustive_tryconn(500))
        {
            if(!exaustive_tryconn(2000))
            {
                if(!exaustive_tryconn(5000))
                {
                    ManageConection.LOG("Não conseguiu");
                }
            }
        }
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
}
private boolean exaustive_tryconn(int timeout)
{
    connection = new Socket();
    try {
        connection.connect(new InetSocketAddress(ManageConection.IP, ip_holder.port), timeout); // tenta um host
    } catch (Exception e) {
        e.printStackTrace();
    }
    if(connection.isConnected())return true;
    connection = new Socket();
    try {
        connection.connect(new InetSocketAddress(ManageConection.IP2, ip_holder.port), timeout); // tenta um segundo host
            } catch (Exception e) {
        e.printStackTrace();
    }
    return connection.isConnected();
}

So here I am perplexed trying to find some solution to this. I searched in almost half of the Web, some say the problem might be ISP, Firewall, NAT, uPnP bugged, Proxy with connection blocking ....

But I just do not know how to check these things, others do not even know what they mean ....

What really worked so far was restarting the modem and things return to normal ... but this causes the server to be offline for 5 to 10 minutes because the dynamic dns of the no-ip takes a while until it is up to date, in addition to my modem takes about 3 minutes to re-connect to the internet after restarting. I'm pretty sure there is a way to solve this without having to interfere manually, after all this has to stay at least one night connected directly, since it's going to be a kettle delivery system.

I'm sorry, I'm sorry to specify so many things, but I think even detailing as much as I can with it is still going to be hard to figure out what might be going on, I'm particularly kind of 'lay' when it involves internet communication.

I even tried several internet access checks, including this one: link where I tested the host and port, and my program even detects that there was a connection ... so if the server of this site can connect to my program remotely .... why does not telnet work?

    
asked by anonymous 23.09.2015 / 16:53

1 answer

0

Unfortunately the port remapping of UPNP technology is stopped because most routers do not perform this operation.

Many serious flaws justify this, however one should always take into account that if every android phone could serve as a server, file-sharing (mostly pirated) applications would increase greatly.

    
05.01.2016 / 12:54