Java – increment IP address in string format

I'm a novice in Java. I'm trying to find a way to increment the IP address range by user input

For example, from 192.168 0.1 to 192.168 0.255. However, my application currently works by using the from and to IP addresses as strings

Is there any way to add all IP addresses entered and entered by users?

I hope this makes sense. Please don't annoy me. I've found the answer!

Edit!

It is actually pinging through the address range, so here is a small code. So far, 'host' is passed in from another class. I want to loop through the address:

public static String stringPing(String stringPing,String host){

    String ipAddress;
    ipAddress = host;

    try
    {
        InetAddress inet = InetAddress.getByName(ipAddress);

        boolean status = inet.isReachable(2000); 

        if (status)
        {
            stringPing = "\n" +host +" is reachable";
            return stringPing;
        }
        else
        {
            stringPing = "\n" +host +" is unreachable";
            return stringPing;
        }
    }
    catch (UnkNownHostException e)
    {
        System.err.println("Host does not exists");
    }
    catch (IOException e)
    {
        System.err.println("Error in reaching the Host");
    }

    return stringPing;

}

Solution

The holding address should be - as a 32 - bit integer and incremented in this form Convert it from or to string only when needed Examples are as follows My IPAddress class is complete and fully functional

class IPAddress {

    private final int value;

    public IPAddress(int value) {
        this.value = value;
    }

    public IPAddress(String stringValue) {
        String[] parts = stringValue.split("\\.");
        if( parts.length != 4 ) {
            throw new IllegalArgumentException();
        }
        value = 
                (Integer.parseInt(parts[0],10) << (8*3)) & 0xFF000000 | 
                (Integer.parseInt(parts[1],10) << (8*2)) & 0x00FF0000 |
                (Integer.parseInt(parts[2],10) << (8*1)) & 0x0000FF00 |
                (Integer.parseInt(parts[3],10) << (8*0)) & 0x000000FF;
    }

    public int getOctet(int i) {

        if( i<0 || i>=4 ) throw new indexoutofboundsexception();

        return (value >> (i*8)) & 0x000000FF;
    }

    public String toString() {
        StringBuilder sb = new StringBuilder();

        for(int i=3; i>=0; --i) {
            sb.append(getOctet(i));
            if( i!= 0) sb.append(".");
        }

        return sb.toString();

    }

    @Override
    public boolean equals(Object obj) {
        if( obj instanceof IPAddress ) {
            return value==((IPAddress)obj).value;
        }
        return false;
    }

    @Override
    public int hashCode() {
        return value;
    }

    public int getValue() {
        return value;
    }

    public IPAddress next() {
        return new IPAddress(value+1);
    }


}

public class App13792784 {

    /**
     * @param args
     */
    public static void main(String[] args) {


        IPAddress ip1 = new IPAddress("192.168.0.1");

        System.out.println("ip1 = " + ip1);

        IPAddress ip2 = new IPAddress("192.168.0.255");

        System.out.println("ip2 = " + ip2);

        System.out.println("Looping:");

        do {

            ip1 = ip1.next();

            System.out.println(ip1);

        } while(!ip1.equals(ip2));


    }

}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>