Like Tree1Likes
  • 1 Post By c99tutorial

finding ip with C program

This is a discussion on finding ip with C program within the C Programming forums, part of the General Programming Boards category; Hi everybody, is it possible to find ip with C code. i am not looking for ip, which we find ...

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    1

    finding ip with C program

    Hi everybody,
    is it possible to find ip with C code.
    i am not looking for ip, which we find with ipconfig.
    i want to find ip, which is written on web site "whatismyip.com" or "myip.com" ....
    thanks

  2. #2
    Registered User
    Join Date
    Nov 2012
    Posts
    1,067
    It seems you want the public IP address. This may be difficult to get without contacting an external server. For a simple method, how about the following:

    1. contact the server "whatismyipaddress.com"
    2. request the http page /
    3. parse the downloaded page to find your ip address

    For more info and an alternative (STUN server) see this thread: networking - Obtaining Own External IP Address in POSIX C - Stack Overflow

  3. #3
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    I wonder if it is possible to to via the proxy serve back to your own computer and get the address that way somehow?

    Just a thought really.

    Bit beyond me to do I think, but I guess you could have one program on you PC connecting to another receiving program via the proxy.

    Also there are two types of proxy I think one where you put in a proxy address in on your PC connection instructions and another
    where you don't use a proxy address on your PC but connect to a proxy via HTML, II think these are called HTML proxies?
    Or perhaps should be lol.


    Not to sure what I am talking about but there are two ways, one where you type in an IP number and another were you connect to a
    site and type in the web address you want and it's all html (or whatever).

  4. #4
    Registered User ledow's Avatar
    Join Date
    Dec 2011
    Posts
    435
    Generally a PC does not know it's real external address (the one it gets told is external is likely modified silently by a NAT router like an ADSL router or similar before touching the Internet, and the ISP may even then - on top of that - change your external address at any time). Thus a computer cannot reliably detect its own external IP address without contacting an external server and that server saying "Okay, I see a connection from x.x.x.x, and that's got the magic number I gave you in it, so that must be you and therefore your external address".

    This means that you need an external server, with Internet connection, running a program 24/7 to reliably detect your external IP address. That's what those websites are, basically (and the "magic number" is a cookie sent with the HTTP request, normally). The "internal" part of the program is just something that talks to an external server and gets an answer back ("your IP is x.x.x.x"), no matter how that's done.

    How do those external sites find your IP? They literally just look it up in their connections list or in the Apache logs of their server (all your communications will appear to come from x.x.x.x, for example, and be logged as such). How do you discover your own external IP? You ask an external server that is running a program that accepts connections all the time. There's no way to do it just "in C" (i.e. locally on your own machine) without that external server. And the way to do it is to talk to an external server and parse the result it gives which, hopefully, will include your correct external IP (so there's no difference between, say, using libcurl to get a URL from whatismyip.com, or doing it all yourself by renting an external server all the time).

    The biggest question is really: Why do you need the external IP? Surely anything that needs to know the external IP could just accept connections from the Internet at a known, fixed address and work "forwards" instead of "backwards" (and likely anything that has an external IP won't accept incoming connections by default, so it wouldn't be useful to KNOW the external IP of your own machine anyway - as telling it to anyone is going to need you to play with your router settings and port-forwarding to do anything practical!).

    - Compiler warnings are like "Bridge Out Ahead" warnings. DON'T just ignore them.
    - A compiler error is something SO stupid that the compiler genuinely can't carry on with its job. A compiler warning is the compiler saying "Well, that's bloody stupid but if you WANT to ignore me..." and carrying on.
    - The best debugging tool in the world is a bunch of printf()'s for everything important around the bits you think might be wrong.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I want to add finding mode fuction in this program.. Plz Help
    By fredsilvester93 in forum C Programming
    Replies: 2
    Last Post: 11-26-2011, 06:23 AM
  2. Little help finding a bug in this program
    By Neotriz in forum C Programming
    Replies: 11
    Last Post: 09-30-2009, 03:31 PM
  3. finding a program in memory
    By ... in forum C++ Programming
    Replies: 7
    Last Post: 09-19-2003, 09:29 AM
  4. need help finding program
    By djpeterpan2003 in forum Windows Programming
    Replies: 1
    Last Post: 02-06-2003, 02:59 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21