Wednesday, March 9, 2016

Java Networking Example : How To Get IP Address Of A Host

Intro


To get IP address of a host, you can use the InetAddress class. By calling getHostAddress() method, you can get an IP address of the host.

Example


package com.freesamplecode.java.networking;

import java.net.InetAddress;
import java.net.UnknownHostException;

public class GetIPAddressByHost {
 public static void main(String[] args){
  try {
   InetAddress host = InetAddress.getByName("free-samplecode.blogspot.com");
   System.out.println("IP Address : "+host.getHostAddress());
  } catch (UnknownHostException e) {
   e.printStackTrace();
  }
 }
}


Ouput


IP Address : 74.125.200.132

Screenshot

How To Get IP Address In Java


Related Posts:

0 comments: