I want to output the ip address value using the java InetAddress class, but how can I output the ip value of several urls instead of one?

Asked 2 years ago, Updated 2 years ago, 37 views

package getParser.java;
import java.net.*;

class InetAddressTest{
    public static void main(String[] args){
        InetAddress addr = null;
        InetAddress[] addrArr = null;
        String name = "www.naver.com";

        addr = InetAddress.getByName(name);
        System.out.println ("domain name: "+addr.getHostName()));
        System.out.println ("IP address: "+addr.getHostAddress()));
        System.out.println();

        addrArr = InetAddress.getAllByName(name);
        for(int i =0; i < addrArr.length;i++){
            System.out.println ("IP List["+i+"]): "+addrArr[i]);
        }

        System.out.println();
        addr = InetAddress.getLocalHost();
        System.out.println ("Local Host Domain Name:" +addr.getHostName()));
        System.out.println ("Local Host IP Address :" +addr.getHostAddress()));
        System.out.println();
    } } catch(UnknownHostException e){
        e.printStackTrace();
    }
}
}

The output is showing like this, and there's a question here! Currently, I searched only the address of Naver url and printed out the ip How do I print out hundreds of different url ip addresses at once? It would be better if it's a way to save url in txt and print it outcrying I'm really a beginner at java, so I'd appreciate it if you could attach the source code as well!

java

2022-09-22 16:15

1 Answers

First of all, you have to have an array of urls.

And repeat the current method by the number of arrays.

String[] names = {"www.naver.com", "www.daum.net"};

//First iteration
for (String url : names) {
    addr = InetAddress.getByName(url);
    System.out.println ("domain name: " + addr.getHostName()));
    System.out.println ("IP address: " + addr.getHostAddress()));
    System.out.println();
    addrArr = InetAddress.getAllByName(url);

    //2nd iteration
    for (int i = 0; i < addrArr.length; i++) {
        System.out.println ("IP List[" + i + "]): " + addrArr[i]);
    }
}

See here for instructions on how to save the file.


2022-09-22 16:15

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.