简体   繁体   English

Android客户端套接字无法连接到服务器

[英]Android client socket unable to connect to server

I have a server socket listening in my local computer, and Im trying to connect to this from my android phone. 我有一个服务器套接字在我的本地计算机上侦听,我试图从我的android手机连接到此服务器。 I already tested the server socket with a client on the same computer, and they were able to connect. 我已经在同一台计算机上使用客户端测试了服务器套接字,并且它们能够连接。

However, the client from my android phone isnt able to connect to the socket. 但是,我的android手机上的客户端无法连接到套接字。 My computer's firewall is turned off. 我的计算机的防火墙已关闭。 Both my computer and the phone are connected to the same wifi network. 我的计算机和电话都连接到相同的wifi网络。 Can someone please help? 有人可以帮忙吗?

Code for the client socket below: 下面的客户端套接字代码:

  public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Thread fst = new Thread(new startConnection());
        fst.start();
    }

 public class startConnection implements Runnable {
        public void run() {
            try {           
                 final InetAddress hostAddr = InetAddress.getByName("192.168.2.1");//This is the local IP of my computer where the serversocket is listening
                        clientSocket = new Socket();
                        clientSocket.bind(null);
                        clientSocket.connect(new InetSocketAddress(hostAddr, 12555),30000);
               } catch (Exception e) {
                        e.printStackTrace();
               } // end TryCatch block 
         }
}

The error that I keep getting is socket connection timeout.Help? 我一直收到的错误是套接字连接超时。帮助?

Thanks a lot! 非常感谢!

FOund the issue. 发现了这个问题。 I was using this piece of code to get the Local IP of the server: 我正在使用这段代码来获取服务器的本地IP:

 try {
    ip = InetAddress.getLocalHost();
 }catch (UnknownHostException e) {
      e.printStackTrace();
 }

This was giving something like 192.168.2.1 for the local IP of the server. 这为服务器的本地IP提供了类似192.168.2.1的信息。 I then used this address for connecting the client to the server, and the connection never worked. 然后,我使用该地址将客户端连接到服务器,并且该连接从未起作用。

I had to instead use a number like 10.0.0.9, which then worked. 我不得不改用像10.0.0.9这样的数字,然后才可以使用。 For getting that correct IP, I had to use this code: 为了获得正确的IP,我必须使用以下代码:

  Enumeration<NetworkInterface> nis = null;
                try {
                    nis = NetworkInterface.getNetworkInterfaces();
                } catch (SocketException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
                NetworkInterface ni;
                while (nis.hasMoreElements()) {
                    ni = nis.nextElement();
                    try {
                        if (!ni.isLoopback() && ni.isUp()) {
                            for (InterfaceAddress ia : ni.getInterfaceAddresses()) {
                                //filter for ipv4/ipv6
                                if (ia.getAddress().getAddress().length == 4) {
                                    //4 for ipv4, 16 for ipv6
                                    System.out.println(ia.getAddress());
                                }
                            }
                        }
                    } catch (SocketException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }

THis gives me: /10.0.0.9 /192.168.2.1 这给了我:/10.0.0.9 /192.168.2.1

The first number is the correct one. 第一个数字是正确的数字。

Can someone explain from a networking standpoint, whats the difference in these 2 numbers/the code that gets them and why one works? 有人可以从网络的角度解释一下,这两个数字/获得它们的代码有什么区别以及为什么一个起作用?

Thank you! 谢谢!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM