GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing codescracker.com — Java Program to Get IP Address
Java Program to Get IP AddressThis article covers multiple programs in Java that find and prints IP address. Here are the list of programs covered by this article:
Find and Print Local IP Address in JavaThe question is, write a Java program to print the local IP address of a computer system. The program given below is its answer: import java.net.InetAddress; public class CodesCracker { public static void main(String[] args) { try { InetAddress ip = InetAddress.getLocalHost(); System.out.println("IP Address = " +ip.getHostAddress()); } catch(Exception e) { System.out.println("Exception: " +e); } } } The snapshot given below shows the sample output produced by above Java program: Code executed on local system, will print local IP. But no worry, other options are also available to find the public IP. Let's see how, in the section given below. Find and Print Public IP Address in JavaTo check your public IP address, you need to visit any website that shows your system's public IP address. For example, amazonaws shows the IP address of your computer system. And of course, that will be your public IP address. To see, open your browser, and type checkip.amazonaws.com. You'll get your public IPv4 address. There are a lot of websites, from where you can find your public IP, but since Amazon is trusted one, therefore I've used their URL. Now, I'm going to use this web address in my Java program to get the value generated by this URL for my system. And that value will get printed on the output, to show the public IPv4 address: import java.net.*; import java.io.*; public class CodesCracker { public static void main(String[] args) { try { URL ip = new URL("https://checkip.amazonaws.com"); BufferedReader br = new BufferedReader(new InputStreamReader(ip.openStream())); String pip = br.readLine(); System.out.println("Public/External IP Address = " +pip); } catch(Exception e) { System.out.println("Exception: " +e); } } } Here is its sample output: In your case, the output will be different. Find Local and Public IP Address using JavaThis is the combined version of previous program. This program prints both local and public IP address of a computer system. import java.net.*; import java.io.*; public class CodesCracker { public static void main(String[] args) { try { InetAddress localhost = InetAddress.getLocalHost(); System.out.println("Local IP Address = " +localhost.getHostAddress()); } catch(Exception e) { System.out.println("Exception: " +e); } try { URL ipfinder = new URL("https://checkip.amazonaws.com"); BufferedReader br = new BufferedReader(new InputStreamReader(ipfinder.openStream())); String publicIP = br.readLine(); System.out.println("Public IP Address = " +publicIP); } catch(Exception e) { System.out.println("Exception: " +e); } } } The sample output produced by above program is shown in the snapshot given below: Same Program in Other Languages« Previous Program Next Program » Practice Quizzes
Test your programming and computer science knowledge with interactive MCQs and topic-based quizzes. Popular Topics
Explore the most popular programming and computer science tutorials on CodesCracker. |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |