We help IT Professionals succeed at work.

Public IP detection.

Last Modified: 2013-12-07

I just breezed over from Java TA. Whoohoo.

Anyway, my question is about how to detect the public IP address of my machine. I have a piddly two machine wireless router network. The two machines are peers, and if one is switched off, or both, it doesnt affect the IP address that the router holds, since as long as the router is on, then it retains it.

I need to know that public IP address, so that someone can find me from the 'net concerning my app., and obviously giving them the local IP (which my Java programme picks up when it asks for the machine's IP -) is no good.

How can I get the public IP? Many thanks for what you can do.
Watch Question

What type of router is it?  On my linksys I can connect to it by typing in in internet explorer, then entereing a username + password.  Then, I click Status and viola, it lists my public IP (WAN IP).

Or, a more generic solution is to visit a website which will tell you your IP.  Try this one: http://scan.sygate.com/probe.html It's a site that scans your computer for vulnerabilities and at the top of this page it will list your IP address. (Don't worry, its quite safe).  You can also goto www.bandwidthplace.com and run a speed test: on the results page will be listed your public IP.

Good luck!
Once you have the public IP, you'll probably need to configure port forwarding on your router (since it likely has a NAT firewall) so that when people connect to it, their connections will be forwarded to one of your local machines.


YohanShminge :

Thanks. On your second posting, my app takes care of that itself in fact, so AFAICS, I dont consider that an issue, but thanks for thinking around the issue anyways.

On the main point, I really want to be able to get to the IP programmatically. From your URL suggestions it would indicate to me that I'd need to sort of parse the HTML from those sites, which I suppose I can do, but it seems a bit kludgy.

After all, in the headers that my email client sends with emails, you can see the public IP in there alright. If Outlook is obtaining it then it must be obtainable by other apps as well. Is the value not held in the registry somewhere?

If you have other ideas, they'd be greatly welcomed. ;)
Yes, to do it programmatically would be a bit difficult.  You would have to find some sort of resource on the internet that would do it.  Another idea I just had to was connect to some random SMTP server and say "helo".  For example, goto the DOS prompt and type "telnet mail.earthlink.net 25" Then, one connected, type "helo" w/o quotes and press enter.  The server will return your IP along with domain name.

FYI: When you receive mail in Outlook, it is NOT outlook that adds the IP, but rather the SMTP server that handles your request.
Here's a nice resource for you! :-)

Top Expert 2009

Your dynamically assigned IP address can be found in the following key in the registry:

HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\{network connection}

The IP address is under the DhcpIPAddress string.  You can simply search the registry on your IP address and it will take you to the correct key.
The IPs listed there would be local (if on a LAN with NAT) which, I belive, is not what you want.

Ii have searched the entire registry, and I could not find my public IP.
Top Expert 2009

You're right...I overlooked the whole router part :)

Only the private IP address would be found in the registry.


>> FYI: When you receive mail in Outlook, it is NOT outlook that adds the IP, but rather the SMTP server that handles your request.

Interesting. My SMTP server is actually on my machine - my mail doesnt go through an ISP.

Is it not possible to ask the router what IP it has - after all, that must be retained there, since the PCs can be switched off but it gets retained.

YohanShminge - I looked at the URLs you suggested and they do give the IP. Again, they are surely interrogating my machine for this via HTTP, so it must be possible to do the same thing locally I would have thought.

Hi Krakatoa,

You can find your public IP ( and any ports that are exposed to the outside world ) by using a nifty website called "Shields Up".  The address is:


Once you have found your address, if you want someone else to be able to get to your exposed program, you will have to expose a port through your router.  ( You currently have all your machines as local IP's... I assume 192.168.X.X. ).  The router should be able to "forward" any port requests to a certain machine or IP beneath it.

For example, if someone on the internet tries to access [ ROUTER IP ] port 42... the router must know to forward that "call" to a certain machine beneath it.  Most ( if not all ) routers have an easy utility to provide such capability...

Coincidentally, you can use the Shields Up website to find out if the port is open, closed, or stealthed.  

hi there

is this needed only for this solution ? or for others as well..

here is my thoughts:

do a traceroute to a known website like www.cisco.com your first hop is = Global IP address
if by any means you are running NAT the 2nd hop will be the Global IP address....

then by some programming lines you can pick up these two lines..


The IP websites are finding your IP address because you are attempting to connect to THEM.  You are always connecting to the outside world through your router's IP address, not your local area network addresses.  Whenever you connect to a website, the HTTP header information your computer sends that website... contains your IP address.

So the chain of events is... your local machine connects to your router using it's local IP... usually 192.168.x.x.  The router "forwards" the information on to the destination IP... but changes the source IP to something like or whatever your public IP is.

Your public IP must be constant if you are going to give it out to someone else to connect to YOU.

There is no way for your program code to detect the public IP from the router... you CAN however, detect the router's LOCAL ip.  But that is only marginally useful.

This is a major reason why you would register your IP with a DNS server... to get something like www.yahoo.com.  that way your IP can change, and people can still get to your site.



I appreciate your comments, however, I am not trying to work with web sites or giving outsiders a line to my personal web space or server or suchlike.

What I am trying to do in that sense is really not *so* important (if you really want to know I am running a Java server application which communicates through TCPIP to external clients).

The app works fine, but the server (by which I really mean the server software), obtains the IP of the machine it sits on, so that it can tell a client where to point to. That IP which it obtains, is, as you and the others have rightly said, the local NAT IP for this machine on its tiny, wireless, 2 PC network.

That's what's no good. I need to programmatically be able to get hold of the public IP - the IP of the router I suspect. If a web site like the ones mentioned above can come into my machine and produce a web page that reports that public IP address, then as HTTP stream, it has managed to get hold of that IP, so it must be possible for me to do it as well - unless I am *totally* off the mark. ;)

In that sense it would be preferable if, that being the only way to do it, I put the same thing on my own website (which I can run on my own machine of course), and use it to report back to me the public IP of the machine it sits on! This sounds bizarre.
I don't think the web server idea would work.

When you connect out to an HTTP server, that server receives your IP address via TCP/IP (it would have to in order for it to return a web page to you).  The server software can access, through a variable, the IP of the remote machine that has connected to it.  The server, knowing your IP, can then execute another program through the Common Gateway Interface (CGI) which constructs an HTML page containing your IP address.  The CGI program returns the web page, and the server forwards that page back to your IP address.

So, as you can imagine, a server on your local machine, or even on the same network, would return a local address.  So in summary, I believe these are your options:

1) Connect to a web server that will return your IP address
2) Connect to an SMTP server that will return your IP address after receiving the "helo" command
3) Figure out some way to connect to your router and get the IP via HTTP or whatever it uses (WHAT TYPE IS IT?)
4) Plug one computer directly into the cable/dsl modem rather that the router then your local IP = remote IP (this is HIGHLY unsafe, and I woulnt try it)

I think tracert is out since it records the first hop AFTER leaving your router (at least, it didnt work for me!).  Also, if client are going to connect to this java server from the outside, then you will have to configure port forwarding on the router.

I'm not sure about your SMTP server works, but thats not important :-)


AHA!  I see what you want.  

You have implied by the last comment that you know the IP addresses of the clients.  So your server can attempt to connect to the clients to "tell" them about it's IP.

tada.  You did when you initially connected!  The IP connection protocol contains the address from the sender.... In this case, it will be the router's IP... because he plugged it in when it passed through him.... in the IP packet.

Now, unfortunately, you have violated the rule of clients and servers... being that clients always connect to the server first, not vice versa... but who cares.

Here is concrete documentation on the WIN32 API call ( sorry about it being c++...) ... WSAAccept... I put exclamation points around the interesting part.  Remember, SOMEWHERE in your client code... you have to accept a connection from the server.  Code similar to WSAAccept MUST be used somewhere.  Capture the IP from there.  

The WSAAccept function conditionally accepts a connection based on the return value of a condition function, provides quality of service flow specifications, and allows the transfer of connection data.

  SOCKET s,                    
  struct sockaddr FAR *addr,  
  LPINT addrlen,              
  LPCONDITIONPROC lpfnCondition,  
  DWORD dwCallbackData        
[in] Descriptor identifying a socket that is listening for connections after a call to the listen function.
[out] Optional pointer to a buffer that receives the address of the connecting entity, as known to the communications layer. The exact format of the addr parameter is determined by the address family established when the socket was created.
[in, out] Optional pointer to an integer that contains the length of the address addr.
[in] Procedure instance address of the optional, application-supplied condition function that will make an accept/reject decision based on the caller information passed in as parameters.
[in] Callback data passed back to the application as the value of the dwCallbackData parameter of the condition function. This parameter is not interpreted by Windows Sockets.


Hope that helps?  If not, clarify.  I've done networking like this quite a bit.

This one is on us!
(Get your first solution completely free - no credit card required)

Here is some code i wrote about a week ago.  

It is for a server that accepts connections from clients... and prints their IP's on a window.  I took out the complex stuff, and this won't compile, but it gives you the idea.  By the way, the Clients don't even BOTHER figuring out their IP...( even though the server WANTS it ) they just shoot out a connection request, and it's plugged in by the communications layer of the network interface.  The server then "magically" gets it on the other side.  Look at the code below.

WSAAccept (  server_socket,
                      0 ) ) )

WSAAddressToString ( &incoming_address,
                                     &size_ip_address_buffer ) ;

See there?  the Address is plugged into the incoming_address structure BY the Win32 API call WSAAccept .  Then the incoming_address structure is converted to a string by WSAAddressToString ... and presto... i now have the IP address ready to print on a window.  ( The string is called ip_address ).  Neither side went to great trouble to do it.

In YOUR case, it sounds as though the server will connect to the client instead...
Yes, mikeleebria is correct.  You are not going to get your public IP through any direct methods.  You'll have to have it reported to you by some outside source. I don't know how farmiliar you are with networking and TCP/IP, but whenever you send data over the internet, it is sent in packets, little segments of information.  Those packets have a header which indicates several things:

-Source IP (once its outside your network, this is your public IP)
-Destination IP (where the packet is going to)
-Length (# bytes)
-Number of packets in complete message
-Sequence of packets

Now, lets say you're going to connect to a web server.  You connect on port 80 and send this: "GET / HTTP/1.1" (plus some other info about browser and host and such).  That info is put into a packet with its own header.  When the HTTP server finally receives the packet, it will know your public IP from the header information.  This is how it knows where to return the requested page.  It creates its own packets and returns them to the source IP (your router).  Once the packets hit your router, it modifies the destination IP via NAT translation.  This is why you cannot programmatically discover your IP.  Now, if you connect to websites or servers that will return your IP within the packet data, then you can of course determine it that way.



Its a BT Home Network 1200 wireless router connected to 2 PCs, one laptop with wirless card, the other wireless via USB.

>> You ARE totally off the mark ...

and I realised it the moment I sent the comment. ;) I should be ashamed of myself for having said that, since my *own* application itself sends the IP out when connecting! HTTP does the same, so when I request a web site, my machine sends its IP in that TCPIP packet.

>> You have implied by the last comment that you know the IP addresses of the clients ...

I dont know the IP address of the clients at all. But the client needs to know the IP of the server, and that's what I'm after.
(It's a detail, and irrelevant to this conversaation, but I'd better tell you so you know : the server interrogates the machine for its IP; it then puts this information into an email and emails it to an address book list of clients. Those guys then literally read that email, and fire up their clients, pointing them at that IP in the email), But forget that, because its not relevant.

BTW YohanShminge, if I run a personal webserver, and attempt to connect to it with IE using the full URL, how would that URL be resolved in any other way than by being resolved on a DNS? Since I dont think I run such a service, it must be being done on the 'net's DNS machines, isnt it, it which case a reflection back to my own machine should work - no?



rubilacx  :

You've got to take a step back there for a moment: - I've already got fully operational code, in Java, that works fine, connecting clients to the server. Java handles all the socket connections, and everything's hunky-dorilly humming along over these TCPIP sockets. I have little problem with any of that, and understand the technology pretty well.

All I am looking for is how to get the public IP of the router, and I cant believe that it is not possible to do that from the local machine, without going on to the net, and going 'round the Sun to get to the Moon.
When you say you connect to your webserver with the full URL, do you mean its domain name?  In that case, then yes, it would have to go out to your ISP's DNS servers, which would report your public IP; then you would connect to yourself (which would show up in the server logs as a connection from your router's IP I'm afraid, not the public IP).

However, if you have a registered domain name, then the DNS servers would tell you your public IP.  However, if that is the case, you must have a static IP? No? And if its static, then why would you need to determine it? It would always be the same.
As I said, it is possible to connect to the router locally, and have it report the IP it has received from your ISP.  However, each different router uses a different configuration interface.  When you want to configure it, can you do so from IE? or is it command prompt based? (I'm looking on the net for it now)
OK, from what I see on the internet, it appears that you can configure your router by connecting to

Try that, and see if you can find a page listing your WAN IP (public IP)



Yes, this router is administered via a web page interface on the PC. I can indeed see the public IP address in it, when I open a certain part of the interface, which I do, as you say, via IE.

What's my best option right now then in light of all this? ;)
This one is on us!
(Get your first solution completely free - no credit card required)

Oh my goodness!  heheh.  You are EMAILING your public IP to the clients.  You're right. I DO need to take a step back.  This isn't the typical application, for sure.

Ok, the problem here is... you have to somehow send a packet through the router back to the router and back to yourself.

Ok... you are either going to LOVE this... or HATE this.

You are going to PING someone and trace the route the IP packet took.  This takes time, but since you are emailing the result, a couple seconds probably doesn't matter to you.

there is a system call... "tracert" that you can do from the command prompt... and you will be able to do from any java application.  I would put the output into a text file... possibly using the > operator from Dos... ( This puts the output into a file instead of to the screen )...

so... you would do something like void system ( "tracert www.yahoo.com > text.txt" ).

Now, the SECOND IP address SHOULD always be the public IP address of your router.  You'll have to test this on your machine to make sure it works.  Your Java application can then parse the text file... find the second IP... and...

Bingo, you have your public IP.

Here is the result from when I did this command:

C:\>tracert www.yahoo.com

Tracing route to www.yahoo.akadns.net []
over a maximum of 30 hops:

  1   <10 ms   <10 ms   <10 ms
  2    16 ms     *      <10 ms
  3   <10 ms    16 ms    15 ms
  4   <10 ms    16 ms    15 ms
  5    16 ms    15 ms    16 ms  axr00msy-7-0-0-1.bellsouth.net []
  6    16 ms    16 ms    15 ms  pxr00msy-2-0-0.bellsouth.net []
  7    32 ms    31 ms    31 ms  so-1-0-0-0.gar2.Dallas1.Level3.net []

  8    31 ms    31 ms    32 ms  unknown.Level3.net []
  9    47 ms    63 ms    62 ms  so-0-1-0.bbr2.Washington1.Level3.net [
 10    46 ms    63 ms    62 ms  ge-9-1.ipcolo1.Washington1.Level3.net [64.159.18
 11    47 ms    62 ms    63 ms  unknown.Level3.net []
 12    47 ms    62 ms    63 ms  vl30.bas1-m.dcn.yahoo.com []
 13    46 ms    63 ms    62 ms  p21.www.dcn.yahoo.com []

Trace complete.

THE SECOND ONE IS MY PUBLIC IP FOR MY ROUTER.  ( No one try to do nothin, it's firewalled... AND I changed the last digits to be safe. )

Does that work for you?
Yeah, im surprised that worked for you.  I suppose it depends on your ISP and your network config.  On my machine, the FIRST return is already beyond my router, some other Comcast router's IP...

Hope it works for you, tho ;-)


In a word - no.

I've tried all that kind of stuff, whether I thought I knew what I was doing or not. None of the utilities return the PIP. Here's my trace:

Tracing route to www.yahoo.akadns.net []

over a maximum of 30 hops:

  1     2 ms     3 ms     2 ms  homeportal.gateway.2wire.net []

  2    18 ms    15 ms    15 ms  rasf4nrp1.reading.broadband.bt.net []

  3    18 ms    16 ms    15 ms  rasf4nrp5.reading.broadband.bt.net []

  4    15 ms    14 ms    14 ms

  5    15 ms    14 ms    15 ms  core2-pos4-1.reading.ukcore.bt.net []

  6    16 ms    17 ms    17 ms  core2-pos15-0.ealing.ukcore.bt.net []

  7    15 ms    16 ms    14 ms  transit2-pos4-0.ealing.ukcore.bt.net []

  8    16 ms    17 ms    18 ms  t2c2-p1-0.uk-eal.eu.bt.net []

  9    91 ms    90 ms    89 ms  t2c2-p5-0.us-ash.concert.net []

 10    93 ms    92 ms    91 ms  exchange-cust1.ash.equinix.net []

 11    94 ms    96 ms    95 ms  ge-0-3-0-p34.msr1.dcn.yahoo.com []

 12    94 ms    94 ms    92 ms  vl31.bas2-m.dcn.yahoo.com []

 13    94 ms    94 ms    94 ms  p19.www.dcn.yahoo.com []

Trace complete.


>> Also, does your router require authentication? (i.e. username + password) If you ...

Well, it only requires, I believe, authentication when I want to change the firewall settings. BUt in the normal course of events, I just switch on the PC(s). log in in Windows as normal, and something in the startup group waves to the router, and off I go.


BTW - doesnt this router have a set of "AT" -like commands like a normal modem that I can kind of ping to get profile and params, including, hopefully this PIP?

The router acts just like an HTTP server.  Unfortunately, they didn't expose the externals in any other kind of way.  They expect the user to always go in through that interface.
I doubt it...its not a normal modem.  You could try telnetting into it, but its unlikely that it supports that (from DOS (YAY DOS) type "telnet" w/o quotes of course).  And to be more specific about the authentication, I mean when you connect to the page listing the public IP, does it require a login?  If so, that will add another level of difficulty if you decide to connect and get the IP via HTTP.  But not impossible!

>>something in the startup group waves to the router, and off I go.

Actually, what probably happens is your "gateway" is set to the router's IP local number in TCP/IP settings under network properties.  In this case it is  Whenever you try to get out, it goes out through the gateway.


I mean it's all a bit antediluvian, having to resort to mucking about with html text parsing. The worst aspect of it however is that you can never really be sure that one of the servers you rely on to bounce your PIP off, is ever going to be online when you need it, so any parsing code will ultimately fail sometime.

Guess its an industry smokescreen/obfuscation/hurdle to stop people having what amounts to a static, or at least pseudo-static IP, which is a valuable commercial commodity.
I suppose so, krakatoa.  HTML/SMTP response parsing looks like your only option at this point.  The most reliable (but most difficult) would be to parse your router's response.  But this also limits your program to your computer (dunno if thats a problem or not).


this is exactly why people use DNS servers with dedicated IP's.


This program MIGHT get you a little closer?
wyliecoyoteukIT director
This one is on us!
(Get your first solution completely free - no credit card required)


Yes, to both your comments. I think you have both done a lot on this one, and that's great.

I tried to telnet to the router, and had "some" success - the reponse varied according to the port I tried. I might write a small piece of code to iterate all the "ports" on the router - not that I know there are really any you understand. ;)

But I think that I am going to have to go with YohanShminge's prescription, and do a bit of parsing around. It wont be a problem to limit it to my router, as long as I can make a mould for replication on others. I may try one of the outside URLs as well.

While typing this, I tried to telnet the router again. It responded that no connection was available on port 23 (!!) so I tried 24 and then 25. I 25 I met my own SMTP server, responding in a telnet fashion, but any telnet commands I tried were given Syntax Errors. Is this progress? :)


>>  wyliecoyoteuk

No I'm not gonna shout ( at least not outside my usual TA!).

Reason I dont want to do that is that my Java app is meant to be like an instant messenger VPN, and I dont want any information or configuration details sitting on web servers out of my control.

Heheh.  ok, you want a nasty solution?!!

do a system call on...


and give it the http address of a local html file.

such as system ( iexplore.exe local.htm ) ;

the local.htm file uses PHP script or another way to go to www.whatismyip.com and returns the TITLE, parses the IP, and puts it into a file.



>. do a system call ...

when you say that rubilacx ( and you look like a C man from what I saw earlier), so you mean a system call from within Java (as perhaps would be made from in C) ?

In Java it would be something like : Runtime.getRuntime().exec("iexplore.exe local.htm");

I dont expect you to know the Java necessarily, but is that what you had in mind?

As for the telnet, it sounds fine, but not really progress...are you farmiliar with SMTP commands? Here are some good ones: mail from: xxxx@xxxx.com, rcpt to: xxxx@xxxx.com, data, help, and helo.  There are more, but its not important. ;-)

Good luck with your parsing!


that is precisely what i mean.

and the benefit of THAT is it allows you to run a third party program from inside your code.  Perhaps you need to broaden your possibilities and check into free programs that save IP's to text files.
For instance, http://www.keir.net/ip2.html is REALLY close to what you need.  It queries quite a few servers, so there is a low chance of it not working.


>> For instance, http://www.keir.net/ip2.html is REALLY close to ..

yes, except that I then have the problem of getting the WAN IP out of IP2, which is probably even more difficult than parsing a websie in the first place ! ;)

I think I need to close this present question, and consider this parsing lark, which I can do, but just dont want to because its "amateurish" in its approach. Jeez, hardware is disappointing stuff.

And I suppose that setting my machine up as its own proxy wont work either will it? (That comment was sheer desperation, if you couldnt tell) ! ;)
Haha, sheer desperation.  No, that wouldnt work either, it'd still be local.  Sorry we couldnt help more!  Perhaps there is a way, deep down in the depths of TCP/IP, something only accessible to the enlightened... ;-)

Best of luck,
This one is on us!
(Get your first solution completely free - no credit card required)


>> the enlightened ...

Jee - I thought that was where I *was* all this time until now!! ;)

Thanks Yohan, and all others. I'll be back. ! ;)


Read the FAQ on "How do I host a web site on my computer?"


I think this is the best you are going to get.


Pity  I never asked Yohan about where on the net he saw the admin details for my router. :((

Ciao; krak.
I found the info here: http://www.adslguide.org.uk/hardware/reviews/2003/q2/bt-1200.asp

And thanks for the points!


Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.