1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Development Calling all Java gurus

Discussion in 'Software' started by Spiral Architect, 10 Jan 2007.

  1. Spiral Architect

    Spiral Architect Cooked on Phonics

    Joined:
    4 Sep 2003
    Posts:
    1,209
    Likes Received:
    2
    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.
     
  2. koola

    koola Minimodder

    Joined:
    11 Jul 2004
    Posts:
    2,462
    Likes Received:
    10
    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.
     
  3. JazzXP

    JazzXP Eh! Steve

    Joined:
    30 Apr 2002
    Posts:
    1,669
    Likes Received:
    13
    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
    }
    
     
  4. Spiral Architect

    Spiral Architect Cooked on Phonics

    Joined:
    4 Sep 2003
    Posts:
    1,209
    Likes Received:
    2
    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.
     
  5. RTT

    RTT #parp

    Joined:
    12 Mar 2001
    Posts:
    14,120
    Likes Received:
    74
    5000ms is not 5 mins :D
     
  6. SteveyG

    SteveyG Electromodder

    Joined:
    23 Nov 2002
    Posts:
    3,049
    Likes Received:
    8
    He's metricised time! Genius! :lol:
     
  7. JazzXP

    JazzXP Eh! Steve

    Joined:
    30 Apr 2002
    Posts:
    1,669
    Likes Received:
    13
    D'oh... whoops :blush: Chuck a *60 in there :)
     

Share This Page