Free Data Structures and Algorithms Course









Subscribe below and get all best seller courses for free !!!










OR



Subscribe to all free courses

How to use java.net.InetAddress Class for Java Networking ?.

Hello friends,
Today I am presenting to you with a Java program which deals with Networking. It demonstrates functioning of java.net.InetAddress class in Java. This Class has many powerful methods which can provide Java developer a powerful API. One of the important method is getLocalHost( ). This method displays the local host address over the console. The User can also print the name of address by calling getByName method. This method takes an parameter of String which is the name of the domain name for which we want the name.
Program to demonstrate working of InetAddress class and the methods of the InetAddress class for Java Networking

package com.hubberspot.networking.example;

import java.net.*;

public class InetAddressDemo
{

  public static void main(String[] args) 
  {
   try
   { 
     InetAddress address = InetAddress.getLocalHost();
     System.out.println("Local host's address : "
                     + address);
     address = InetAddress.getByName("www.google.com");
     
     System.out.println("Google's address : "
                     + address);

     InetAddress[] allAddresses = InetAddress
          .getAllByName("www.google.com");
     
     for( int i=0 ; i < allAddresses.length ; i++)
       System.out.println("Address " + i+1 + " : "
                    + allAddresses[i]);
  }
   catch(UnknownHostException    e)
   {
      e.printStackTrace();
   }
 }   
}


Output of the program :


 
© 2021 Learn Java by Examples Template by Hubberspot