Wednesday, March 9, 2016

Java Networking Example : How To Get Localhost Address And Localhost Name

Intro


To get localhost address and localhost name, you can use getLocalHost() method of InetAddress class.

Example


package com.freesamplecode.java.networking;

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

public class GetLocalhostDemo {
 public static void main(String[] args){
  InetAddress address;
  try {
   address = InetAddress.getLocalHost();
   System.out.println("Localhost Address :" +address.getHostAddress());
   System.out.println("Localhost Name :" +address.getHostName());
  } catch (UnknownHostException e) {
   e.printStackTrace();
  }
  
 }
}

Output


Localhost Address :192.168.1.101
Localhost Name :mobile214

Screenshot




Related Posts:

0 comments: