Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Getting A Computers IP Address

Reply
Thread Tools

Getting A Computers IP Address

 
 
Hal Vaughan
Guest
Posts: n/a
 
      12-08-2007
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
 
Reply With Quote
 
 
 
 
Mark Thornton
Guest
Posts: n/a
 
      12-08-2007
Hal Vaughan wrote:
> 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
 
Reply With Quote
 
 
 
 
Arne Vajh°j
Guest
Posts: n/a
 
      12-08-2007
Hal Vaughan wrote:
> 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());
}
}
 
Reply With Quote
 
Mark Thornton
Guest
Posts: n/a
 
      12-08-2007
Arne Vajh°j wrote:
> Hal Vaughan wrote:
>> 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.
>


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

Mark Thornton
 
Reply With Quote
 
Arne Vajh°j
Guest
Posts: n/a
 
      12-08-2007
Mark Thornton wrote:
> Arne Vajh°j wrote:
>> Hal Vaughan wrote:
>>> 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.
>>

>
> 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
 
Reply With Quote
 
Andrew Thompson
Guest
Posts: n/a
 
      12-09-2007
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!

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

Message posted via http://www.javakb.com

 
Reply With Quote
 
Mark Thornton
Guest
Posts: n/a
 
      12-09-2007
Arne Vajh°j wrote:
> Mark Thornton wrote:
>> Arne Vajh°j wrote:
>>> Hal Vaughan wrote:
>>>> 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.
>>>

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


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
 
Reply With Quote
 
Mark Rafn
Guest
Posts: n/a
 
      12-09-2007
Hal Vaughan <(E-Mail Removed)> wrote:
>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 http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.dagon.net/>
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      12-10-2007
On Sat, 08 Dec 2007 15:46:18 -0500, Hal Vaughan
<(E-Mail Removed)> 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
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can two different computers have the same IP address? Clueless in Seattle Computer Support 14 08-24-2007 02:23 PM
Cannot see workgroup computers by ''view workgroup computers'' - important! kimiraikkonen Computer Support 5 02-07-2007 12:06 PM
Computers acquire IP address from wrong WLAN. =?Utf-8?B?dG9ycGVzY28=?= Wireless Networking 1 07-05-2006 08:27 AM
Computers Enabled Inc - Cheap Notebook Computers KUTEATLBOI@gmail.com Computer Information 0 03-09-2006 04:42 AM
Client computers IP-address? Alexander Duerloo ASP .Net 3 02-08-2004 08:47 PM



Advertisments