Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: Java Language
Expertise: Beginner
Apr 8, 1997



Building the Right Environment to Support AI, Machine Learning and Deep Learning

Getting the IP Address of a workstation by using Java

How can I simply get the IP address of a workstation by using Java so that I can display it and run another application ?

The java.net.InetAddress class allows Java programs to do this. Given a host name, one can use the getByName method to find out its IP address:

InetAddress address = InetAddress.getByName("www.inquiry.com");
	byte[] IPnumber = address.getAddress();
Note that getByName is a static method and therefore can be called without having an InetAddress object to begin with. This is similar to how the System.out.println() method works.

Also note that getByName() returns an InetAddress object that can then be used to find out the individual pieces of the IP address by getting an array of bytes.

You should also know that applets are not allowed to perform general IP address look-ups. They can only look up the IP address of the user's local system and the server system they came from. This is a security measure that prevents malicious applets from performing IP address look-ups inside a protected intranet and shipping that information back out to the Internet.

The following NameLookup program can be run as either an applet or a standalone Java application. As an applet it con only look up the local system's IP address and that of the server. As an application, it can look up arbitrary IP addresses.

To invoke it as an application, do:

java NameLookup

import java.applet.*;
import java.awt.*;
import java.net.*;
import java.io.*;

public class NameLookup extends Applet {
	TextField hostname;
	Label addressLabel;

	public void init() {
		setLayout(new BorderLayout());

		Panel namePanel = new Panel();
		Label nameLabel = new Label("Host name: ");
		hostname = new TextField(20);
		add("North", namePanel);

		addressLabel = new Label("IP address: ");
		add("Center", addressLabel);

		Label selfLabel = new Label();
		add("South", selfLabel);
		try {
			InetAddress address = InetAddress.getLocalHost();
			byte[] IPnumber = address.getAddress();

			// The reason for doing the bitwise AND is to
			// make sure the bytes are read as unsigned integers
			selfLabel.setText("Local IP address: " + 
					(0xFF & IPnumber[0]) + "." +
					(0xFF & IPnumber[1]) + "." +
					(0xFF & IPnumber[2]) + "." +
					(0xFF & IPnumber[3]));
		} catch (Exception e) { 
			selfLabel.setText("Unable to determine " +
						"local IP address");


	public synchronized boolean action(Event e, Object what) {
		if (e.target == hostname &&
			e.id == Event.ACTION_EVENT) {
			try {
				InetAddress address =  
				byte[] IPnumber = address.getAddress();
				addressLabel.setText("IP address: " + 
					(0xFF & IPnumber[0]) + "." +
					(0xFF & IPnumber[1]) + "." +
					(0xFF & IPnumber[2]) + "." +
					(0xFF & IPnumber[3]));
			} catch (Exception err) {
				addressLabel.setText("Unable to" +
					"determine IP address");
			return true;
		return false;

	public static void main(String argv[]) {
		Frame f = new Frame();
		NameLookup a = new NameLookup();
		f.setLayout(new BorderLayout());
		f.add("Center", a);

DevX Pro
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date