Checking server port availability with Android

I am trying to connect to a remote server from my Android device. How to check if any port is open on my server? For instance. how to check if port 80 is open on my server 11.11.11.11?

I am currently using InetAddress to ping if the host is available, but that does not tell me if port 80 is open.

Current code

 boolean isAvailable = false; try { isAvailable = InetAddress.getByName("11.11.11.11").isReachable(2000); if (isAvailable == true) { //host is reachable doSomething(); } } catch (Exception e) { } 
+6
source share
2 answers

Create a Socket that will verify that a given ip with a specific port can be connected or not.

 public static boolean isPortOpen(final String ip, final int port, final int timeout) { try { Socket socket = new Socket(); socket.connect(new InetSocketAddress(ip, port), timeout); socket.close(); return true; } catch(ConnectException ce){ ce.printStackTrace(); return false; } catch (Exception ex) { ex.printStackTrace(); return false; } } 
+7
source

Runtime.getRuntime (). exec () and pass the command to it to get a different port entry with pid. The following are the commands:

"cat / proc / net / tcp", "cat / proc / net / tcp6", "cat / proc / net / udp", "cat / proc / net / udp6".

Here is an explanation with the source code. I tested it.

http://kickwe.com/tutorial/android-port-scanner-tutorial/

0
source

Source: https://habr.com/ru/post/901347/


All Articles