| | |
Host's Non-Local IP Address
Thread Solved
![]() |
•
•
Join Date: Dec 2005
Posts: 45
Reputation:
Solved Threads: 0
Hello, I was just wondering if it is possible in Java to get the host's IP address, as seen on the Internet, not network or by the computer itself. I mean, getting something like 87.253.145.35 instead of something like 192.168.1.100 or 127.0.0.1.
Or, if not, is there any popular/known server that has a static IP address with which I could open a Socket and have it tell me my IP address?
Or, if not, is there any popular/known server that has a static IP address with which I could open a Socket and have it tell me my IP address?
•
•
Join Date: Dec 2005
Posts: 45
Reputation:
Solved Threads: 0
Ah, well, that won't really work in Java by itself, but, for those who care, this works (by utilizing a website like the one above):
If I could figure out what the index URL for whatismyip.org is (which has only 1 thing on it: the IP address), that would be even better. I've tried a lot of extensions to "index" and some to "default"....
Java Syntax (Toggle Plain Text)
try { Socket sock = new Socket("www.edpsciences.org", 80); BufferedReader is = new BufferedReader(new InputStreamReader(sock.getInputStream())); PrintWriter os = new PrintWriter(sock.getOutputStream(), true); // the actual URL is www.edpsciences.org/htbin/ipaddress (a ruby page) os.println("GET /htbin/ipaddress HTTP/1.0"); os.println(); // two new lines are required to send a command // the page has x number of lines with unnecessary information before it says "Your IP address".... String line = ""; while(line.indexOf("Your IP address") == -1) { line = is.readLine(); } // do a little substringing, and we'll have our answer (the ip address is inbetween "<B> " and " </B>" String ipAddr = line.substring(line.indexOf("<B> ")+4, line.indexOf("</B>")-1); } catch (Exception e) { e.printStackTrace(); }
If I could figure out what the index URL for whatismyip.org is (which has only 1 thing on it: the IP address), that would be even better. I've tried a lot of extensions to "index" and some to "default"....
•
•
Join Date: Jun 2004
Posts: 609
Reputation:
Solved Threads: 7
Hi everyone,
Try looking at the java's InetAddress api, there maybe something useful there for you
http://java.sun.com/j2se/1.5.0/docs/...etAddress.html
Richard West
Try looking at the java's InetAddress api, there maybe something useful there for you
http://java.sun.com/j2se/1.5.0/docs/...etAddress.html
Richard West
Microsoft uses "One World, One Web, One Program" as a slogan.
Doesn’t that sound like "Ein Volk, Ein Reich, Ein Führer" to you, too?
— Eric S. Raymond
Tell me what type of software do you like and what would you pay for it
http://www.daniweb.com/techtalkforums/thread19660.html
Doesn’t that sound like "Ein Volk, Ein Reich, Ein Führer" to you, too?
— Eric S. Raymond
Tell me what type of software do you like and what would you pay for it
http://www.daniweb.com/techtalkforums/thread19660.html
•
•
Join Date: Jun 2004
Posts: 609
Reputation:
Solved Threads: 7
Hi everyone,
see the below threads
http://forum.java.sun.com/thread.jsp...62802&tstart=0
http://forum.java.sun.com/thread.jsp...hreadID=592585
http://forum.java.sun.com/thread.jsp...hreadID=520999
http://forum.java.sun.com/thread.jsp...hreadID=639572
http://sharkysoft.com/tutorials/jsa/content/040.html
http://forum.java.sun.com/thread.jsp...hreadID=143409
http://www.java-tips.org/java-se-tip...an-applet.html
http://forum.java.sun.com/thread.jsp...hreadID=218845
http://forum.java.sun.com/thread.jsp...hreadID=269203
Richard West
see the below threads
http://forum.java.sun.com/thread.jsp...62802&tstart=0
http://forum.java.sun.com/thread.jsp...hreadID=592585
http://forum.java.sun.com/thread.jsp...hreadID=520999
http://forum.java.sun.com/thread.jsp...hreadID=639572
http://sharkysoft.com/tutorials/jsa/content/040.html
http://forum.java.sun.com/thread.jsp...hreadID=143409
http://www.java-tips.org/java-se-tip...an-applet.html
http://forum.java.sun.com/thread.jsp...hreadID=218845
http://forum.java.sun.com/thread.jsp...hreadID=269203
Richard West
Microsoft uses "One World, One Web, One Program" as a slogan.
Doesn’t that sound like "Ein Volk, Ein Reich, Ein Führer" to you, too?
— Eric S. Raymond
Tell me what type of software do you like and what would you pay for it
http://www.daniweb.com/techtalkforums/thread19660.html
Doesn’t that sound like "Ein Volk, Ein Reich, Ein Führer" to you, too?
— Eric S. Raymond
Tell me what type of software do you like and what would you pay for it
http://www.daniweb.com/techtalkforums/thread19660.html
•
•
Join Date: Dec 2005
Posts: 45
Reputation:
Solved Threads: 0
All of those do not give the public IP address, except the fourth one (http://forum.java.sun.com/thread.jsp...hreadID=639572). However, the way suggested is just what I had thought of and implemented above.
•
•
Join Date: Nov 2009
Posts: 1
Reputation:
Solved Threads: 0
0
#8 2 Days Ago
•
•
•
•
Hello, I was just wondering if it is possible in Java to get the host's IP address, as seen on the Internet, not network or by the computer itself. I mean, getting something like 87.253.145.35 instead of something like 192.168.1.100 or 127.0.0.1.
Or, if not, is there any popular/known server that has a static IP address with which I could open a Socket and have it tell me my IP address?
Java Syntax (Toggle Plain Text)
public String getMyPublicIP(){ try { URL autoIP = new URL("http://www.whatismyip.com/automation/n09230945.asp"); BufferedReader in = new BufferedReader( new InputStreamReader(autoIP.openStream())); String ip_address = (in.readLine()).trim(); return ip_address; }catch (Exception e){ e.printStackTrace(); return "ERROR"; } }
Last edited by davido82; 2 Days Ago at 1:04 pm.
![]() |
Similar Threads
- help in C, Find local Ip address (C)
- Get local IP address (VB.NET)
- creating a host name (Networking Hardware Configuration)
Other Threads in the Java Forum
- Previous Thread: problem with compiling
- Next Thread: Selection Sort in java
| Thread Tools | Search this Thread |
-xlint 911 actionlistener addball addressbook android applet application array automation binary blackberry block bluetooth button character class client code compile component consumer css csv database desktop developmenthelp eclipse ee error fractal ftp game gameprogramming givemetehcodez graphics gui html image j2me j2seprojects japplet java javaarraylist javac javaee javaprojects jni jpanel julia jvm lego linked linux loan mac method mobile netbeans notdisplaying number objects online oriented phone printf problem program programming project projects recursion replaydirector reporting researchinmotion rotatetext rsa scanner se server service set singleton sms software sort sql string swing system test textfields threads time title tree tutorial-sample ubuntu update windows






