arungeek
Hacks and Tweaks



Uncategorized

February 28, 2010

Java Program To Display IP Address And Domain Name

More articles by »
Written by: arunenigma

Handling Internet Addresses (Domain Names, and IP addresses) is made easy with Java.

Internet addresses are represented in Java by the InetAddress class. InetAddress provides simple methods to convert between domain names, and numbered addresses.

We start by importing the java.net package, which contains a set of pre-written networking routines (including InetAddress).

import java.net.*;

Next, we declare a new variable of type InetAddress, which we assign the value of the local host machine (for machines not connected to a network, this should represent 127.0.0.1). Due to the fact that InetAddresses can generate exceptions, we must place this code between a try .. catch UnknownHostException block.

// Obtain the InetAddress of the computer on which this program is running
InetAddress localaddr = InetAddress.getLocalHost();

The InetAddress class has methods that return the IP address as an array of bytes (which can be easily converted into a string), as well as a string representation of its domain name (e.g. mydomain.org ).

We can print out the InternetAddress, as well as the domain name of the local address.

System.out.println (“Local IP Address : ” + localaddr );
System.out.println (“Local hostname : ” + localaddr.getHostName());


Program :

public class MyFirstInternetAddress
{
public static void main(String args[])
{
try
{
InetAddress localaddr = InetAddress.getLocalHost();

System.out.println (“Local IP Address : ” + localaddr );
System.out.println (“Local hostname : ” + localaddr.getHostName());
}
catch (UnknownHostException e)
{
System.err.println (“Can’t detect localhost : ” + e);
}

}

/** Converts a byte_array of octets into a string */
public static String byteToStr( byte[] byte_arr )
{
StringBuffer internal_buffer = new StringBuffer();

// Keep looping, and adding octets to the IP Address
for (int index = 0; index < byte_arr.length -1; index++)
{
internal_buffer.append ( String.valueOf(byte_arr[index]) + “.”);
}

// Add the final octet, but no trailing ‘.’
internal_buffer.append ( String.valueOf (byte_arr.length) );

return internal_buffer.toString();
}
}

Compile and run this application, and you should be told your local IP address, and hostname.

Don’t worry if your computer isn’t connected to the Internet, though. Providing your system has a TCP stack, it should give you back an IP address even if you aren’t currently connected. On most systems, you can refer to your local machine (which often has the hostname “localhost”) as IP address 127.0.0.1

Why would every machine that’s not connected to the Internet have the same address?

This address is known as a Loopback Address. Every time you connect to this address, you’re actually connected to your local machine. So, if you were running a local webserver, and you pointed your browser to http://127.0.0.1, you should see your web-site.

But if I were to go to the same address, I’d connect to a different site – that of my own machine.

This is great when developing Java applications. You don’t need a permanent connection to the Internet – you can run client and server applications on your own machine.

This is handy, because writing and testing client/server applications can take some time, and unless you have a permanant connection, you wouldn’t want to be billed on an hourly rate by your ISP !



About the Author

arunenigma
Computer Science Graduate Student @ Case Western Reserve University, Cleveland, USA




 
 

 
Factory_1

Python Factory Design Patterns using Switch Case

I googled for Factory Method Design Pattern in Python but couldn’t find a good resource. So, I  am sharing an example program to demonstrate this design pattern in Python which I frequently use. The factory method pattern is...
by arunenigma
 

 
 
Gospers_glider_gun

Conway’s Game of Life Implemetation in Python with cool patterns

he Game of Life (or simply Life) is not a game in the conventional sense. There are no players, and no winning or losing. Once the “pieces” are placed in the starting position, the rules determine everything that ha...
by arunenigma
 

 
 
bin-tree

Python AVL Tree Implementation with ASCII visualization

n computer science, an AVL tree is a self-balancing binary search tree. It was the first such data structure to be invented. In an AVL tree, the heights of the two child subtrees of any node differ by at most one; if at any tim...
by arunenigma
 

 

 
bst

Binary Search Tree in Python with ASCII art visualization

Binary search tree implementation in Python with: in, post and pre-order traversals. Also includes methods for insertion, deletion and search of nodes. Deletion is fairly complex and is made possible by keeping track of parents...
by arunenigma
 

 
 
fibonacci

Python, Memoization, Dynamic Programming, Fibonacci Series and some Fun!

ython can implement the recursive formulation directly, caching return values. Memoization is a method where if a call is made more than once with the same arguments, and the result is returned directly from the cache. For exam...
by arunenigma
 

 




2 Comments


  1. This is first time visit here
    A good informative post that you have shared and appreciate your work for sharing the information.Register domain name


  2. I don't have any words to appreciate this post. Really this articles is very useful to me Thanks
    Register a website



You must be logged in to post a comment.