Right guys, using Java i'm trying to scan computers connected to the network and report whether they are running. All computers on the network will be running a client program which will report back to the server program (the one scanning the network) its IP address and host name. If certain computers on the network aren't running the client program or are unavailable then a message on the server will appear saying it cannot establish a connection. The way I want it to scan is to start off at 192.168.1.100 for example and try to establish a connection. If a connection can be established then the server will report "Connected to localhost/192.168.1.100" before closing the connection and moving onto 192.168.1.101 to repeat this process. Once the server reaches say 192.168.1.120 it will stop for 1 minute before trying to re-establish a connection with 192.168.1.100 etc. Now, the question is, how can I implement this count from 192.168.1.100 to 192.168.1.120 in order to try and establish a connection with each in term? To reiterate I want to open and close the connection before moving onto the next IP address. The code I have so far is as follows: As you can see, at the moment this will only except connections from 192.168.1.104. If you have any ideas or suggestions I will more than appreciate hearing them.
Use a For loop to iterate through the IP addesses (e.g. 192.169.$i) nested inside a while loop that has Sleep(5*1000) for 5 mins at the bottom. Code: while { for (i=100,120,i++) { try { blabla..toString("192.168.1."..i) } } Sleep(5*1000) ; 5 mins } Forgive me if the syntax is all wrong, but I havn't coded (scripted) in Java for a long time. [Edit] Stick your try,catch inside the for loop as well.
Just cleaning up your code a little Code: while (true) // or whatever { for (int i=100; i <= 120; i++) { socket = new Socket("192.168.1." + i, 5432); // Put all the rest of your code in here/function call/etc } try { Thread.sleep(5*1000); // 5 mins } catch (InterruptedException ie) {} // No need to really handle this }
Cheers guys. IP in incrementing now. Never thought it would be as simple as a while loop. Just about to try the Thread.sleep now.