Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Beginning Java
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Jeanne Boyarsky
Ron McLeod
Paul Clapham
Liutauras Vilda
Sheriffs:
paul wheaton
Rob Spoor
Devaka Cooray
Saloon Keepers:
Stephan van Hulst
Tim Holloway
Carey Brown
Frits Walraven
Tim Moores
Bartenders:
Mikalai Zaikin
Forum:
Beginning Java
My IP address
Dave Jones
Ranch Hand
Posts: 77
posted 19 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Hello
I'm writing a program and I need to get my IP address (i.e 12.34.123.123 )
I tryed using this code,
try { InetAddress addr = InetAddress.getLocalHost(); String ipAddr = addr.getHostAddress(); String hostname = addr.getHostName(); System.out.println(ipAddr); System.out.println(hostname); } catch (Exception e) { e.printStackTrace(); }
but I get:
127.0.0.1 as my address.....not very helpful eh ?...
does someone have a short code sample that will give me what I need ??
Thanks
Jeroen Wenting
Ranch Hand
Posts: 5093
posted 19 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
You're getting exactly what you're asking for, the local host which is always 127.0.0.1
42
Dave Jones
Ranch Hand
Posts: 77
posted 19 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
I know, but I need the real ip address not 127.0.0.1 !!
never mind, I got it already.
thanks anyway :roll:
Grishma Dube
Ranch Hand
Posts: 275
posted 19 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
u got it???
tell me how??? even i need to know it
Abhishek Roy
Greenhorn
Posts: 4
posted 19 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
This may helpful
Run as (for IPv4)$java lookupAndReturn abhiroy.com
OR as (for IPv6)$java -6 lookupAndReturn abhiroy.com
import java.net.*; public class lookupAndReturn{ public static void main (String[] args) { try{ String cacheAddress=""; String cacheHostname=""; InetAddress cacheInetAddress; String usageMsg = "Usage: displayHostnames [options]\n" + "Where [options] are:\n" + "\t -h\t\t\t display this message\n" + "\t --help\t\t\t display this message - same with -h\n"+ "\t -4\t\t\t lookup IPv4 address\n" + "\t -6\t\t\t lookup into IPv6 address\n" + "\t by default IPv4 address of the given hostname will be lookup\n"; for (int i=0; i<args.length;i++) { if(args[i].equals("-h")) { System.err.println(usageMsg); return; } else if(args[i].equalsIgnoreCase("-help")) { System.err.println(usageMsg); return; } else if(args[i].equals("-4")) { // System.setProperty("java.net.preferIPv4Stack", "true"); } else if(args[i].equals("-6")) { System.setProperty("java.net.preferIPv6Addresses", "true"); } else { cacheHostname=args[i]; } } if (cacheHostname==""){ System.err.println("Error: address argument required"); return; }else{ System.out.println("\tThe initial hostname is:\t"+cacheHostname); } cacheInetAddress = InetAddress.getByName(cacheHostname); cacheAddress = cacheInetAddress.getHostAddress().toString(); System.out.println("\tThe IP address is:\t\t"+cacheAddress); cacheInetAddress = InetAddress.getByName(cacheAddress); System.out.println("\tThe reverse lookup hostname is:\t"+cacheInetAddress.getHostName()); }catch (Exception e){ e.printStackTrace(System.err); System.out.println("error:\t"+e); } } }
[ March 17, 2005: Message edited by: Abhishek Roy ]
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
Lists
finding the character after a specific word in a String
[help] Access UserName/Password restrict WebPages!
can we use ServerSocket and Socket for Wide area network?
How to get own ip Address:
More...