-1

Trying to run a loop to check if a IP is open.

for (int z = 0; z < 100; z++) {
    String ip = "10.0.0." + z;
        try {
            log(ip);
            Socket socket = new Socket(ip, MainActivity._util.getPort());
            // Handle Stuff when Exception NOT throw
        } catch (Exception e) {
        }
}

log is a function I made (no errors there)

.getPort() is just a "global" port number I've defined EDIT : That returns 2683 (just a random number)

it is supposed to just catch the exception then move on with the for loop but that doesn't happen.

Currently this just prints:

10.0.0.0

10.0.0.1

and then freezes.

If anyone has any advice that would be great!

EDIT

  • using socket.connect(new InetSocketAddress(ip, port), timeout) method still didn't handle correctly

  • This is in an AsyncTask extended class, in the doInBackground() method

user3502489
  • 361
  • 1
  • 4
  • 11

1 Answers1

2

There is no socket server to listen those IP and ports. So your socket client will be freeze. As mentioned here you should use another constructor for the socket and set timeout to prevent freezing.

for (int z = 0; z < 100; z++) {
    String ip = "10.0.0." + z;
        try {
            log(ip);
            Socket socket = new Socket();
            socket.connect(new InetSocketAddress(ip, MainActivity._util.getPort()), 1000);
            // Handle Stuff when Exception NOT throw
        } catch (Exception e) {
        }
}
Community
  • 1
  • 1
Hossein Rashno
  • 3,073
  • 1
  • 25
  • 49