Finding my "real" ip
Sorry I'm unfamiliar with the technical terms for this, but I am trying to find my "real" IP: the one websites see when I send a request. I've tried many ways of doing this so far but all of them give me either 127.0.0.1 or my network address 192.168.1.100. I'm trying to find a way in Java to fetch the IP address others could use to connect to me.
[359 byte] By [
muleasdfa] at [2007-10-3 0:27:23]

192.168 is a private ip address range so when your requests go out the people outside will only see the IP of your gateway/proxyIf you want to people outside your lan to connect you you either need to get a public ip or map a port of your gate way in to your computer using a
LRMKa at 2007-7-14 17:20:01 >

Well I want the program to be compatable with no configuration on the user's part. Are the any widely known servers out there which I can connect to and have my IP sent back to me.
Whatismyip.com
This will tell you your public ip address. Routers, etc. have a system where systems that are connected to it are "firewalled." Which means they have a private ip address. (ie. 192.168.1.*) This ip can only be accessed by other computers within that network given available permissions. However, if you wish for a public, non-networked computer to connect, you must do something called port-forwarding. Enter the ip address of your default gateway, and you should enter the configuration settings. (Be careful changing these...and if you need a password, contact the manufacturer). Now find the port forwarding. Now enter in the port number on which incoming traffic will be forwarded, enter the name of the program, and the network ip to forward to.
To find the default gateway ip and your ip:
Start>Run> "cmd" (ENTER)
now in the command window, type "ipconfig" and press enter
You will see your ip address and the default gateway ip address.
if for some reason it does not show or does not appear, type "ipconfig /all" instead.
Whatismyip.com is for humans. I want a program to somehow determine it's public IP. This problem has nothing to do with port forwarding!
> Whatismyip.com is for humans. I want a program to
> somehow determine it's public IP.
Your computer doesn't have a public IP address.
> This problem has
> nothing to do with port forwarding!
It does if you want a host outside your network to open a connection to your box and you don't have a public IP address.
jverda at 2007-7-14 17:20:01 >

>Your computer doesn't have a public IP address....but isn't it assigned by my ISP? or can I only find my IP by making a connectoin with another IP?
> >Your computer doesn't have a public IP address.
>
> ...but isn't it assigned by my ISP?
No.
The key word though is "computer". Your computer does not have a public IP address. Your router/modem does. We know this because as you already stated your computer had an internal network address (192.168)More or less your router forwards packets from you to the internet and from the internet to you.
Why do you need to get this information anyway?
Well I'll just leave that feature out then. I wanted this information so people could run the software with little configuration or work.
> >Your computer doesn't have a public IP address.
>
> ...but isn't it assigned by my ISP?
Possibly. But that doesn't mean it's public. Or else maybe your router has a public IP, in which case, as already mentioned, you could set up port forwarding (assuming your ISP allows incoming connecitons to your router on those ports).
> or can I only
> find my IP by making a connectoin with another IP?
That still will not give an IP address that another computer (outside your network) can use to connect to you.
jverda at 2007-7-14 17:20:01 >

> Well I'll just leave that feature out then. I wanted
> this information so people could run the software
> with little configuration or work.
If you think someone is going to connect to this app via the internet then you had better figure out what a public IP is.
Either you "have" one or you don't. And you need to understand what that means in terms of what the app can do.
I think I know what you are asking.
I have a multi-user app that can run as a server, so that a dedicated server is not required. As a result, it requires that the users know the IP they are connecting to. Rather than assume the user will know this, I want to display it to the user who starts the server, so that they can inform the other users (players in this case as it is a multi user game) where to connect.
I have managed to write the following code to guess the assigned IP from your ISP. It does this by enumerating through all your network adapters, looking at the IPs and discarding localhost. It will then find your network IP and your internet IP. I assume that for your purposes you will want to ignore the networkIP, unless your users will be connecting through a Local Area Net.
Here is the code anyway.....
String networkIP = null;
String internetIP = null;
try {
Enumeration interfaces = NetworkInterface.getNetworkInterfaces();
while (interfaces.hasMoreElements()) {
NetworkInterface networkInterface = (NetworkInterface) interfaces.nextElement();
Enumeration addresses = networkInterface.getInetAddresses();
while (addresses.hasMoreElements()) {
InetAddress inetAddress = (InetAddress) addresses.nextElement();
// ignore the localhost loopback IP
if (!inetAddress.getHostAddress().equals("127.0.0.1")) {
// LAN addresses usually start 192.168, or 10.
if (inetAddress.getHostAddress().startsWith("192.168") ||
inetAddress.getHostAddress().startsWith("10.")) {
networkIP = inetAddress.getHostAddress();
}
// assume all other IP's are Internet IP's
else {
internetIP = inetAddress.getHostAddress();
}
}
}
}
} catch (SocketException e) {
e.printStackTrace();
}
hope this helps
codemwnci
You can always use the following technique (even though it is not so efficient):
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec("ipconfig"); //if windows OS
InputStream stderr = proc.getInputStream();
InputStreamReader isr = new InputStreamReader(stderr);
BufferedReader br = new BufferedReader(isr);
String line = null;
while ( (line = br.readLine()) != null)
....and parse the output to determine the line of your interest.
I hope that helps
/m
The problem is that the OP does not have a public IP address, so these techniques will not work.
jverda at 2007-7-14 17:20:01 >

> I have managed to write the following code to guess
> the assigned IP from your ISP. It does this by
> enumerating through all your network adapters,
> looking at the IPs and discarding localhost. It will
> then find your network IP and your internet IP. I
> assume that for your purposes you will want to ignore
> the networkIP, unless your users will be connecting
> through a Local Area Net.
That will not work unless the user does indeed have a public IP.
You can't connect to a public IP that your ISP is using and expect that it is going to be routed to a single private IP that the ISP services.
ISPs do not have a public IP for every user that the service. It is quite possible that they only have one. How do you think that a packet to that single public IP is going to find a single users private IP out of the thousands that the ISP serves?
The only way that will work is if the user already has a public IP. Just one for the user.And if that is the case then they could just ask their ISP what it is.
you can try this it works on my computer but its not very eloquent and it is dependent upon getip.com not changing its format
import java.net.*;
import java.io.*;
public static void main(String[] args)
{
try{
URL ipurl = new URL("http://www.getip.com");
InputStream is = ipurl.openStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String startTag = "<title>";
String endTag = "</title>";
String sr;
do
{
sr = br.readLine();
if(sr.contains(startTag))
{
String[] srs = sr.split("::");
for(int i = 0; i<srs.length;i++)
if(srs.contains(endTag))
{
String[] srsIp = srs.split("<");
System.out.println(srsIp[0].trim());
sr = null;
}
}
}
while(sr != null);
br.close();
}
catch(Exception e)
{
System.out.println(e.toString());
}
}>
mruna at 2007-7-21 9:06:45 >

copy paste error it should readif(srs[ i ].contains(endTag))String[] srsIp = srs[ i ].split("<");
mruna at 2007-7-21 9:06:45 >

> you can try this it works on my computer but its not
> very eloquent and it is dependent upon getip.com not
> changing its format
This doesn't change the fundamental issue: If there's no public IP, there's no public IP, and you can't open a connection from the outside, unless the router does NAT. But then, the public IP is that of the router, not your computer.
I agree, but it does return the public ip that is used when the computer makes an internet request. Seems like maybe putting the computer in a DMZ might work but that still requires router/firewall configuration.
mruna at 2007-7-21 9:06:45 >

> I agree, but it does return the public ip that is
> used when the computer makes an internet request.
Which is meaningless.
Me having the public IP of my ISP isn't going to allow me to connect to my computer.
> Seems like maybe putting the computer in a DMZ might
> work but that still requires router/firewall
> configuration.
That has absolutely nothing to do with this discussion.