Reputation: 68
I'm developing an android app which is used to remote control PC from an android phone. I managed to develop a fully functional app (client and server(java) app) except i have to specify for the client on which IP to connect. Both client and server are opening a socket and listening on port 8998. Is there a way of getting a list of servers (on client side) listening on that specific port?
Thank you in advance,
Kristian
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!");
}
Upvotes: 0
Views: 869
Reputation: 1007584
Is there a way of getting a list of servers (on client side) listening on that specific port?
Iterate over all relevant IP addresses and see if you can connect on that port for each of them. It is up to you to decide your algorithm for "all relevant IP addresses", though a common one is to check all on the /24 subnet (e.g., if the device's IP address is 192.168.1.57, check all 192.168.1.* addresses except 57).
Upvotes: 1