Airlike
Airlike

Reputation: 243

small ip scanner code in java

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

Upvotes: 3

Views: 14708

Answers (4)

Paul Ruane
Paul Ruane

Reputation: 38590

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.

Upvotes: 4

Babar
Babar

Reputation: 2826

Here is the code sample from TechnoJeeves to do this.

import java.net.InetAddress;

public class ScanNet {
    public static void main(String[] args) throws Exception {
    int[] bounds = ScanNet.rangeFromCidr("192.168.1.255/24");

    for (int i = bounds[0]; i <= bounds[1]; i++) {
        String address = InetRange.intToIp(i);
        InetAddress ip = InetAddress.getByName(address);

        if (ip.isReachable(100)) { // Try for one tenth of a second
            System.out.printf("Address %s is reachable\n", ip);
        }
    }
}

public static int[] rangeFromCidr(String cidrIp) {
    int maskStub = 1 << 31;
    String[] atoms = cidrIp.split("/");
    int mask = Integer.parseInt(atoms[1]);
    System.out.println(mask);

    int[] result = new int[2];
    result[0] = InetRange.ipToInt(atoms[0]) & (maskStub >> (mask - 1)); // lower bound
    result[1] = InetRange.ipToInt(atoms[0]); // upper bound
    System.out.println(InetRange.intToIp(result[0]));
    System.out.println(InetRange.intToIp(result[1]));

    return result;
}

static class InetRange {
    public static int ipToInt(String ipAddress) {
        try {
            byte[] bytes = InetAddress.getByName(ipAddress).getAddress();
            int octet1 = (bytes[0] & 0xFF) << 24;
            int octet2 = (bytes[1] & 0xFF) << 16;
            int octet3 = (bytes[2] & 0xFF) << 8;
            int octet4 = bytes[3] & 0xFF;
            int address = octet1 | octet2 | octet3 | octet4;

            return address;
        } catch (Exception e) {
            e.printStackTrace();

            return 0;
        }
    }

    public static String intToIp(int ipAddress) {
        int octet1 = (ipAddress & 0xFF000000) >>> 24;
        int octet2 = (ipAddress & 0xFF0000) >>> 16;
        int octet3 = (ipAddress & 0xFF00) >>> 8;
        int octet4 = ipAddress & 0xFF;

        return new StringBuffer().append(octet1).append('.').append(octet2)
                                 .append('.').append(octet3).append('.')
                                 .append(octet4).toString();
    }
} }

Upvotes: 0

xappymah
xappymah

Reputation: 1644

You should you getAddress which returns ip as byte array instead of getHostAdress.

Upvotes: 0

OscarRyz
OscarRyz

Reputation: 199244

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/

Upvotes: 2

Related Questions