Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / December 2007

Tip: Looking for answers? Try searching our database.

Getting A Computers IP Address

Thread view: 
Hal Vaughan - 08 Dec 2007 20:46 GMT
I know there are different ways to get a computers IP address.  I'd like to
have a Java program find out a computer's IP address for the Internet, not
for any LAN it's on.  Is there a way I can do this easily or by finding the
gateway's or proxy's eternal IP address?

Thanks!

Hal
Mark Thornton - 08 Dec 2007 21:41 GMT
> I know there are different ways to get a computers IP address.  I'd like to
> have a Java program find out a computer's IP address for the Internet, not
> for any LAN it's on.  Is there a way I can do this easily or by finding the
> gateway's or proxy's eternal IP address?

As far as I know there is no general way of doing this. It is one of the
issues addressed by UPnP. If your gateway supports UPnP that would
provide a mechanism, and a Java implementation is possible. Some people
think UPnP is a security risk and disable it, so unless you have
management authority over the LAN you may not be able to use this
approach even if the hardware supports it.

Mark Thornton
Arne Vajhøj - 08 Dec 2007 22:04 GMT
> I know there are different ways to get a computers IP address.  I'd like to
> have a Java program find out a computer's IP address for the Internet, not
> for any LAN it's on.  Is there a way I can do this easily or by finding the
> gateway's or proxy's eternal IP address?

There are not even a 100% safe way.

You will need to ask an external server what IP it see.

Below are a small example using a danish service.

Arne

============================

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ExternIP {
    public static String getIP() throws Exception {
        URLConnection uc = (new
URL("http://www.myip.dk/")).openConnection();
        BufferedReader br = new BufferedReader(new
InputStreamReader(uc.getInputStream()));
        StringBuilder sb = new StringBuilder("");
        String line;
        while((line = br.readLine()) != null) {
            sb.append(line);
        }
        br.close();
        Pattern p = Pattern.compile("(?:<title>Your IP:
)(\\d+\\.\\d+\\.\\d+\\.\\d+)(?:</title>)");
        Matcher m = p.matcher(sb.toString());
        m.find();
        return m.group(1);
    }
    public static void main(String[] args) throws Exception {
        System.out.println(ExternIP.getIP());
    }
}
Mark Thornton - 08 Dec 2007 23:46 GMT
>> I know there are different ways to get a computers IP address.  I'd
>> like to
[quoted text clipped - 9 lines]
>
> Below are a small example using a danish service.

So called "transparent" proxies often mean such attempts return the
address of the proxy instead of your networks external address.

Mark Thornton
Arne Vajhøj - 08 Dec 2007 23:51 GMT
>>> I know there are different ways to get a computers IP address.  I'd
>>> like to
[quoted text clipped - 12 lines]
> So called "transparent" proxies often mean such attempts return the
> address of the proxy instead of your networks external address.

Assuming that it will be the same proxy for such a site and the
real site then it does not matter.

Arne
Mark Thornton - 09 Dec 2007 08:57 GMT
>>>> I know there are different ways to get a computers IP address.  I'd
>>>> like to
[quoted text clipped - 17 lines]
>
> Arne

The proxy depends on the port, so you have to do the test using the same
port as your later intended use of the IP address. My ISP has
transparent proxies on port 80.

Mark Thornton
Andrew Thompson - 09 Dec 2007 07:18 GMT
Re: Getting A Computers IP Address

Why?  What does that do for the end user?*

Had you considered ..

<sscce>
import javax.swing.*;

class GetIPAddress {

 public static void main(String[] args) {
   JOptionPane.showMessageDialog(
     null,
     "The appl. needs you IP address so it can AAA\n" +
     "To find the IP address on your system, do BBB");
   String ipAddress = JOptionPane.showInputDialog(
     null,
     "IP address");
   System.out.println("IP Address: " + ipAddress);
 }
}
</sscce>

Where AAA is a text that is sufficiently convincing that
it would *motivate me, as an end user, to supply an IP
address, and BBB is OS specific instructions.

Easy done!

Signature

Andrew Thompson
http://www.physci.org/

Mark Rafn - 09 Dec 2007 21:33 GMT
>I know there are different ways to get a computers IP address.  I'd like to
>have a Java program find out a computer's IP address for the Internet, not
>for any LAN it's on.  Is there a way I can do this easily or by finding the
>gateway's or proxy's eternal IP address?

Have the server report the address of the endpoint of the connection.  This
will give you the NAT or proxy server address.
--
Mark Rafn    dagon@dagon.net    <http://www.dagon.net/>
Roedy Green - 10 Dec 2007 11:44 GMT
On Sat, 08 Dec 2007 15:46:18 -0500, Hal Vaughan
<hal@thresholddigital.com> wrote, quoted or indirectly quoted someone
who said :

>I know there are different ways to get a computers IP address.  I'd like to
>have a Java program find out a computer's IP address for the Internet, not
>for any LAN it's on.  Is there a way I can do this easily or by finding the
>gateway's or proxy's eternal IP address?

Usually internal IPs are masked by the firewall or proxy server.  For
the various IPs you can discover, see
http://mindprod.com/jgloss/ip.html
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.