Getting A Computers IP Address

H

Hal Vaughan

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
 
M

Mark Thornton

Hal 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?

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
 
A

Arne Vajhøj

Hal 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?

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());
}
}
 
M

Mark Thornton

Arne said:
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.

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

Mark Thornton
 
A

Arne Vajhøj

Mark said:
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
 
A

Andrew Thompson

Hal Vaughan wrote:

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!
 
M

Mark Thornton

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

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
 
M

Mark Rafn

Hal Vaughan 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?

Have the server report the address of the endpoint of the connection. This
will give you the NAT or proxy server address.
 
R

Roedy Green

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top