Getting the IP Address of a workstation by using Java

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 class allows Java programs to do this.Given a host name, one can use the getByName method to find out itsIP address:

InetAddress address = InetAddress.getByName(“”);	byte[] IPnumber = address.getAddress();
Note that getByName is a static method and therefore can be called withouthaving an InetAddress object to begin with. This is similar to how theSystem.out.println() method works.

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

You should also know that applets are not allowed to perform generalIP address look-ups. They can only look up the IP address of the user’slocal system and the server system they came from. This is a security measurethat prevents malicious applets from performing IP address look-upsinside a protected intranet and shipping that information back out tothe Internet.

The following NameLookup program can be run as either an applet or astandalone Java application. As an applet it con only look up thelocal 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*;import*;public class NameLookup extends Applet {	TextField hostname;	Label addressLabel;	public void init() {		setLayout(new BorderLayout());		setBackground(Color.white);		Panel namePanel = new Panel();		Label nameLabel = new Label(“Host name: “);		namePanel.add(nameLabel);		hostname = new TextField(20);		namePanel.add(hostname);		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 ( == hostname && == Event.ACTION_EVENT) {			try {				InetAddress address =  					InetAddress.getByName(hostname.getText());				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();		a.init();		a.start();		f.setLayout(new BorderLayout());		f.add(“Center”, a);		f.pack();;	}}

Share the Post:
data observability

Data Observability Explained

Data is the lifeblood of any successful business, as it is the driving force behind critical decision-making, insight generation, and strategic development. However, due to its intricate nature, ensuring the

Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular