Getting the IP Address of a workstation by using Java

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

Answer:
The java.net.InetAddress 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(“www.inquiry.com”);	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 java.net.*;import java.io.*;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 (e.target == hostname &&			e.id == 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();		f.show();	}}

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

iOS app development

The Future of iOS App Development: Trends to Watch

When it launched in 2008, the Apple App Store only had 500 apps available. By the first quarter of 2022, the store had about 2.18 million iOS-exclusive apps. Average monthly app releases for the platform reached 34,000 in the first half of 2022, indicating rapid growth in iOS app development.

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a