Free Data Structures and Algorithms Course

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


Subscribe to all free courses

How to use Class for Java Networking ?.

Hello friends,
Today I am presenting to you with a Java program which deals with Networking. It demonstrates functioning of 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;


public class InetAddressDemo

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

     InetAddress[] allAddresses = InetAddress
     for( int i=0 ; i < allAddresses.length ; i++)
       System.out.println("Address " + i+1 + " : "
                    + allAddresses[i]);
   catch(UnknownHostException    e)

Output of the program :

© 2021 Learn Java by Examples Template by Hubberspot