Android的扫描本地子网 [英] Android Scan Local Subnet

查看:162
本文介绍了Android的扫描本地子网的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在写一个小的测试Android应用程序,并运行遇到了一个小(大)的问题模拟器。

I'm currently writing a small test Android app, and have run across a small (large) problem with the emulator.

在code表示熄灭扫描本地子网的计算机上运行我的服务器一块软件不返回任何东西!这code完美的功能在桌面上的部分,所以我知道什么是错的我模拟器里面。

The code that goes out and scans the local subnet for computers running my server piece of the software does not return anything! This code functions perfectly on the desktop portion, so I know something is wrong inside of my emulator.

我只好硬code的IP扫描首先是因为我不能确定仿真器内的IP地址,所以我知道我至少扫描的权利范围。

I had to hardcode the IP scan first because I cannot determine the IP address within the emulator, so I know I'm at least scanning the right range.

摘要:如何从我的仿真器内部通过套接字连接到服务器上的本地子网

Summary: How can I connect to servers via sockets from inside my emulator on the local subnet?

谢谢大家!

下面是所要求的code:

Here's the requested code:

public static ArrayList<String> serviceScanner() {
    ArrayList<String> servers = new ArrayList<String>();

    // Get the IP of the local machine
    String iIPv4 = "";
    String test = "";


    //getLocalIpAddress();
    //System.out.println(test);

    try {
        // Get localhost
        InetAddress addr = InetAddress.getLocalHost();

        // Get IP Address
        byte[] ipAddr = addr.getAddress();

        iIPv4 = addr.toString();
        iIPv4 = iIPv4.substring(iIPv4.indexOf("/") + 1);
        iIPv4 = "10.0.2.1";
    } catch (UnknownHostException e) {
        // Exception output
    }
    // IP stuff.
    String IPv4Start = "", IPv4End = "";
    iIPv4 = iIPv4.substring(0, iIPv4.lastIndexOf("."));
    iIPv4 += ".";
    IPv4Start = iIPv4 + "1";
    IPv4End = iIPv4 + "254";


    PrintWriter out = null;
    BufferedReader in = null;

    // Loop to scan each address on the local subnet
    for (int i = 1; i < 255; i++) {

        try {
            System.out.println(iIPv4+i);
            Socket mySocket = new Socket();
            SocketAddress address = new InetSocketAddress(iIPv4 + i, port);

            mySocket.connect(address, 5);

            out = new PrintWriter(mySocket.getOutputStream(), true);
            in = new BufferedReader(new InputStreamReader(
                    mySocket.getInputStream()));
            out.println("Scanning!");
            String fromServer;
            while ((fromServer = in.readLine()) != null) {
                System.out.println("Server: " + fromServer);
                if (fromServer.equals("Server here!")) {
                    servers.add(iIPv4 + i);
                    mySocket.close();
                    break;
                }
            }
            mySocket.close();
            out.close();
            in.close();

        } catch (UnknownHostException e) {
        } catch (IOException e) {
        }
    }
    return servers;
}

}

推荐答案

模拟器不在同一子网计算机。它是在它自己的虚拟子网经由其自己的NAT路由器连接到计算机。有一种解释一下:的http://developer.android.com/guide/developing/devices/emulator.html#emulatornetworking

The emulator is not on the same subnet as your computer. It's on it's own virtual subnet connected to the computer via its own NAT router. There is an explanation here: http://developer.android.com/guide/developing/devices/emulator.html#emulatornetworking

然而,仿真器,通过其路由器,应能连接到任何插座随地在互联网上。什么是你正在试图连接到的地址?仿真器将因为它使用它们本身不是路由10.0.0.0私有地址。不知道168.192.0.0。你能后的code,它失败的原因?

However, the emulator, via its router, should be able to connect to any socket anywhere on the Internet. What is the address you are trying to connect to? The emulator won't route 10.0.0.0 private addresses because it uses them for itself. Not sure about 168.192.0.0. Can you post the code that is failing?

这篇关于Android的扫描本地子网的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆