我正在开发一个 Android 应用程序,用于从 Android 手机远程控制 PC。我设法开发了一个功能齐全的应用程序(客户端和服务器(java)应用程序),除了我必须为客户端指定要连接的IP。客户端和服务器都打开一个套接字并监听端口 8998。有没有办法获取监听该特定端口的服务器列表(在客户端)?
提前谢谢您,
克里斯蒂安
try {
InetAddress serverAddress = InetAddress.getByName(Constants.SERVER_IP); // getting the inet adress of the server
NetworkInterface netInterface = NetworkInterface.getByInetAddress(serverAddress); // getting the interface name where all other servers should be
Enumeration<InetAddress> addresses = netInterface.getInetAddresses();
for (InetAddress inetAddress : Collections.list(addresses)) {
out.println("InetAddress: " + inetAddress);
Log.d("adresses", String.valueOf(inetAddress));
}
}catch(IOException e){
Log.e("err", "Unknown hostname!");
}
最佳答案
Is there a way of getting a list of servers (on client side) listening on that specific port?
遍历所有相关的 IP 地址,看看是否可以在每个地址的该端口上进行连接。由您决定“所有相关 IP 地址”的算法,但常见的一种是检查/24 子网上的所有地址(例如,如果设备的 IP 地址是 192.168.1.57,则检查所有 192.168.1.*除 57 之外的地址)。
关于java - 获取android中监听特定端口的服务器列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30834406/