Android 扫描本地子网
我目前正在编写一个小型测试 Android 应用程序,并且遇到了模拟器的一个小(大)问题。
扫描本地子网以查找运行我的软件服务器部分的计算机的代码不会返回任何内容!这段代码在桌面部分运行完美,所以我知道我的模拟器内部出了问题。
我必须首先对 IP 扫描进行硬编码,因为我无法确定模拟器中的 IP 地址,所以我知道我至少扫描了正确的范围。
摘要: 如何通过本地子网上的模拟器内部的套接字连接到服务器?
谢谢大家!
这是请求的代码:
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;
}
}
I'm currently writing a small test Android app, and have run across a small (large) problem with the emulator.
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.
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?
Thanks all!
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;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
模拟器与您的计算机不在同一子网中。它位于自己的虚拟子网上,通过自己的 NAT 路由器连接到计算机。这里有一个解释: http://developer.android.com/guide /developing/devices/emulator.html#emulatornetworking
但是,模拟器通过其路由器应该能够连接到 Internet 上任何位置的任何套接字。您尝试连接的地址是什么?模拟器不会路由 10.0.0.0 私有地址,因为它自己使用它们。不确定 168.192.0.0。您可以发布失败的代码吗?
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
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?