Skip to toolbar

Getting IP address and Hostname using InetAddress Class.

Frequently Asked Programs in Java.

InetAddress class is used to represent the Internet Protocol address.
getByName(hostName) returns the all the details of a given hostname.

InetAddress Class Usage

In the below program TM-RXTUVC4 is the name of your Computer. Once we created an object of InetAddress class. And pass hostname as an argument via getByName() all the details like IP address, hostname, etc. can be accessed by using the object created.

Get your computer name

  1. Open Control Panel
  2. Click on Systems and Security
  3. Click on System link
  4. Locate for the Computer Name field
Locating Computer Name using Control Panel.

Selenium Code

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

/**
 * @author ashok.kumar
 *
 */
public class Sample4 {

	public static void main(String[] args) throws UnknownHostException 
       {
	InetAddress address = InetAddress.getByName("TM-RXTUVC4");
		
		System.out.println(address);
		System.out.println(address.getCanonicalHostName());
		System.out.println(address.getHostAddress());
	}
}

Output:
TM-RXTUVC4/10.0.65.2
TM-RXTUVC4.ashok.com
10.0.65.2

Java Programs:

TestNG:

Computer Basics:

Java Basics:

Java File Handling:

OOPs Concept:

Java Question And Answer:

Selenium Tutorials:

JIRA:

Agile:


1379total visits,3visits today

Leave a Reply

Your email address will not be published. Required fields are marked *

Site Statistics

  • Users online: 0 
  • Visitors today : 36
  • Page views today : 37
  • Total visitors : 48,232
  • Total page view: 66,379

   YouTube ChannelQuora

            Ashok Kumar is working in an IT Company as a QA Consultant. He has started his career as a Test Trainee in manual testing in August 2010. Then he moves towards the automation testing after 4 years. He started learning JAVA and Selenium by self to get the knowledge of automation.

       While learning these tools and working on multiple projects, he found that sometimes people get stuck in live scenarios in their project and they have to do lots of RnD to get out of it. So he decided to start blogging only for such scenarios, where anyone facing any problem in their project, can ask any question or give a solution or you can say an alternate solution to achieve the goal successfully.

Later on, he observed that some people want to learn Java but they have few questions in their mind like how to start Java, whether we should go for the online or offline course. So he started writing tutorials on Java, Jira, Selenium, Excel etc.