尝试使用外部IP将我的Android客户端连接到我的计算机

时间:2015-09-23 18:40:07

标签: java android sockets communication serversocket

我试图建立一个服务器 - 客户端模型,它将是一个服务器的java应用程序,一个Android应用程序将与我的服务器计算机建立一个Socket连接,在监听端口(任意选择50000)< / p>

Android app注册,登录,发出一些信息请求,并发送一些回复。服务器程序侦听一个或多个客户端并从MySQL数据库中获取一些信息,当客户端结束时,请求会打印出他们已购买的内容的财务记录。

几乎所有的作品都是:

  • 在我的路由器中配置了uPnP端口转发
  • 我的java程序是打开的,正在侦听正确的端口(端口50000是LISTENING,在命令netstat中)
  • 我的外部IP在动态DNS主机中解析(我使用网站no-ip),当我ping我的主机时,返回正确的外部IP
  • 一切正常,几乎当我的路由器很开心时......

过了一会儿(几个小时,几分钟),外部访问变得无法访问,原因不明显:

  • 外部IP不会发生变化(也不会发生本地变化)
  • 端口保持可见(在许多端口测试器网站上测试过,我的程序打印出有人在检查端口时尝试连接)
  • 我可以ping我的主机并解析为正确的IP ...
  • 如果在同一个网络中使用本地IP(我的电脑是192.168.1.8),Android应用可以连接

但是当我尝试执行telnet时,事情变得不清楚了:

  • telnet 192.168.1.8 50000工作(本地主机)
  • telnet 127.0.0.1 50000也可以(也是本地主机?)
  • telnet localhost 50000也可以(显然是本地主机)
  • 但是对于外部ip,并且路由器ip(192.168.1.1)不起作用,打印如下:&#34;正在连接到123.123.123.123 ...无法打开与主机的连接,在端口50000上:连接失败&#34;
  • 并且在外部IP上使用telnet,没有指定端口,不能打印上面相同的
  • (很明显,当我的路由器很开心时,所有的telnet都可以使用)

使用ServerSocket打开服务器的服务器端应用程序的java代码:

@Override
public void run()
{
    try {
        Start();
    } 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 Start() throws InterruptedException
{
    connect();

    while(KeepWaiting)
    {
        WaitClient();
    }
} 
public void connect()
{
    try {
            serverSocket =new ServerSocket(50000);
            System.out.println("my ServerSocket started");
            System.out.println("listening:\n"+InetAddress.getLocalHost()+":"+serverSocket.getLocalPort());
        } catch (IOException e) {
            e.printStackTrace();
        }
}
public void WaitClient() throws InterruptedException{
    try {
            Inicio.LOG("Waiting Connection...");
            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("<Conected>");
            System.out.println("IP Client: " + socket.getInetAddress());
            Inicio.LOG("Conected -> IP Client: " + socket.getInetAddress());
            ReadThread reader_thread = new ReadThread(buff_Reader);
            WriteThread writer_thread = new WriteThread(buff_Writer);
            ConectionClient newconn = new ConectionClient(this,reader_thread,writer_thread,socket);
            Conections.add(newconn);
            System.out.println("Number of Conections:"+Conexoes.size());
            newconn.start(); // stats the thread thats holds the conection with that guest
        } catch (IOException e ){//SQLException e) {
            System.out.println("<Error>");
            Inicio.Erro("Server Stoped with Error:"+e.getMessage());
            e.printStackTrace();
            KeepWaiting=false;
        }
        finally{
            // ends the server
        }
}

我的客户端android应用程序的代码,它通过Socket与服务器连接:

   @Override
public void run()
{
    try {
        connect();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}
public void connect() throws InterruptedException
{
    try {
        exaustive_connect();
        if(connection.isConnected()&&Continue) {
            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);
            Communicate(); // method that listen and write messages.
        }
        else
        {
            ManageConection.E("ConexaoServidor","could not connect");
        }
    } catch (UnknownHostException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally{
        close(); // method that close the socket, reader and writer.
    }

}
private void exaustive_connect()
{
    try {
        if(!exaustive_tryconn(500))
        {
            if(!exaustive_tryconn(2000))
            {
                if(!exaustive_tryconn(5000))
                {
                    ManageConection.LOG("could not connect");
                }
            }
        }
    }
    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); // tries the first 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); // tries the second host
            } catch (Exception e) {
        e.printStackTrace();
    }
    return connection.isConnected();
}

所以我在这里试图找到解决问题的方法。任何帮助,以便我可以确定哪些问题是受欢迎的 - ISP,防火墙,NAT,越野车UPnP,阻止代理等?

让我的路由器再次开心的原因是&#39;重新开始&#39;一段时间(几个小时,有时几分钟......)所有工作,我的送货系统工作正常,但这使服务器离线5至10分钟,我希望这可以保持长达六个小时没有中断,或者一些客户会对我的错误服务器/客户端感到失望。

为了测试端口是否打开,我还使用了这个网站http://ping.eu/port-chk/他们说端口是打开的,我的程序检测到有人尝试连接...所以如果这个网站的服务器可以连接到我的服务器...为什么我不能?

Abhi,在测试simple things ...

后添加了结果
  

ARP条目:

  

NAT规则通过iptables:

这可能是一个unix命令,或者有点因为我只找到信息和支持来在unix系统中查看它,经过长时间的搜索后我找到了命令&#39; Route print&#39;打印有点像iptable

  

防火墙日志

当服务器正在工作时,允许连接,但是当不工作时,防火墙的日志根本不显示任何有关连接断开的信息...只显示许多允许的地方,但不是#39; t跟踪我的telnet,或者当我尝试从另一个客户端连接时......

当我禁用防火墙时,服务器仍无法访问。

  

路由器日志

在路由器日志中:

  • 从我的服务器机器telnet到外部ip或主机,说 21:24:12 (since last boot) **SYN Flood** 192.168.1.8, 7269->> 179.252.7.196, 50000 (from PPPoE1 Outbound)
  • 当telnet从另一台本地机器到外部ip或主机时,说 21:28:02 (since last boot) **SYN Flood** 192.168.1.5, 60810->> 179.252.7.196, 50000 (from PPPoE1 Outbound)
  • 当telnet从我的服务器机器或其他本地机器到我的本地ip时,简单的不记录任何事情。
  • 打开在线portchecker时,点击查看,表示已打开,但路由器日志显示为21:30:17 (since last boot) **SYN Flood** 88.198.46.51, 47508->> 192.168.1.8, 50000 (from PPPoE1 Inbound)

路由器日志说服务器何时不工作......

  

Microsoft消息分析工具

过滤日志的打印:https://uploaddeimagens.com.br/imagens/messageanalizer-png

要查看的内容:

  • 当从我的服务器机器telnet到外部ip或主机时,出现 Application\tTCP: Retransmitted SYN, original message is #1524
  • 当从另一台本地计算机telnet到外部ip或主机时,根本不记录任何内容
  • 当telnet从我的服务器机器或其他本地机器到我的本地ip时,一切正常
  • 当打开一个在线portchecker时,把我的外部IP和端口50000点击查看,说是可以访问的,并在消息analizer工具日志中作为一个成功的链接,有趣的是那个目的地&#39;似乎是我的本地IP,所以他们如何连接到我的本地IP?

0 个答案:

没有答案