How to Get List of All IPs of a Host in Java


In this example, we can use the static method getAllByName() in the InetAddress class to get all the IPs given to the host.

Source Code

package com.beginner.example;

import java.net.InetAddress;

public class getAll_IpTest {

	public static void main(String[] args) {
		
		try {
			
		//Get all the IP of this host
		InetAddress[] ips = InetAddress.getAllByName("www.baidu.com");
		
		for(InetAddress i : ips) {
			
			System.out.println(i.getHostAddress());
			
		}
		
		
		}catch (Exception e)  {    
			e.printStackTrace();
		}

	}

}

Output:

61.135.169.125
61.135.169.121
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments