small ip scanner code in java

13,759

Solution 1

So, what you need is to convert an IPv4 address to Int and back.

See if this helps:

http://teneo.wordpress.com/2008/12/23/java-ip-address-to-integer-and-back/

Solution 2

This tends to be achieved using broadcast/multicast. This is not something I have ever played with so can not offer you any code, but this link offers a good explanation.

Edit: it transcends there is a MulticastSocket class which you may be able to put to use.

Share:
13,759
Airlike
Author by

Airlike

Updated on June 30, 2022

Comments

  • Airlike
    Airlike almost 2 years

    I'm writing a small game in java. There's a server and a client module. At the moment every client has to enter the server IP (in a local network) manually. Thats why I have written this code here:

    import java.net.*;
    
    
    public class myIP {
    
    public static void main (String argv[]) 
    { 
        try{
              InetAddress ownIP=InetAddress.getLocalHost();
              String myIP = ownIP.getHostAddress();
              System.out.println( "IP of my system is := "+ myIP );
            }catch (Exception e){
              System.out.println( "Exception caught ="+e.getMessage() );
            }
          }
    }
    

    This piece of code returns the IP address of the machine. With this information (my own IP address) I'd like to check now for other IP addresses in this range to find the server automatically.

    Now I don't know how to iterate over this IP range. For example: if "myIP" is 10.0.0.5, how can I modify that string so I would have 10.0.0.6 for example? If it would be an integer value, it would be easy to add 1 every time - but since it's a string - separated by dots - I'm not sure how to solve that :) any idea?

    cheers