internet ip

This is a discussion on internet ip within the Windows Programming forums, part of the Platform Specific Boards category; How can I get my own ip address that represents me in the internet in my c/c++ program....

  1. #1
    lvk
    lvk is offline
    Registered User
    Join Date
    Oct 2002
    Posts
    8

    Question internet ip

    How can I get my own ip address that represents me in the internet in my c/c++ program.

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    You can determine your internet IP easily via the same solution as you would if you were resolving a host IP.

    On the other hand, if you are behind a router, then the simplest solution is to read your IP as it reflects off of a website. For example, let say your IP is 192.168.x.x over a network and the internet ip. whatever.isp.net. To get the real internet IP from isp.net, you need to visit a website that outputs your IP address. There are many websites that will outputs the visitors' internet IP including bandwidth testing websites and network security websites.

    Kuphryn

  3. #3
    Banned Cgawd's Avatar
    Join Date
    Oct 2002
    Posts
    405
    I *think* you ment what C/C++ code you would use to determine the users IP, i dont know, but ill try to help http://www.catalyst.com/support/help...s/methods.html
    http://edocs.bea.com/wle/wle42in/inhtml/datashts.htm
    and some of this www.google.com

  4. #4
    TransparentMember correlcj's Avatar
    Join Date
    Jun 2002
    Posts
    378
    How can I get my own ip address that represents me in the internet in my c/c++ program.
    simple,
    1. click START -> RUN.
    2. in the open text box, type comman, then click ok. The command prompt window opens
    3. At the DOS prompt type ipconfig/all. abracadabra!! You IP address appears.

    I hope this helps, my friend.
    cj
    "Be formless, shapeless, like water... You put water into a cup, it becomes the cup, you put water into a bottle, it becomes the bottle, you put it in a teapot, it becomes the teapot... Now water can flow, or it can crash, be water my friend."
    -Bruce Lee

  5. #5
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,853
    Try

    GetAdaptersAddresses()

    or

    GetAdaptersInfo()

    from

    Iphlpapi header and lib

    depending on your targets OS.

    >>2. in the open text box, type comman, then click ok. The command prompt window opens

    this is OS dependant
    Last edited by novacain; 10-28-2002 at 10:45 PM.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  6. #6
    Registered User johnnie2's Avatar
    Join Date
    Aug 2001
    Posts
    186
    Originally posted by correlcj
    3. At the DOS prompt type ipconfig/all. abracadabra!! You IP address appears.
    And on Windows 9x systems, it's winipcfg, not ipconfig.
    "Optimal decisions, once made, do not need to be changed." - Robert Sedgewick, Algorithms in C

  7. #7
    lvk
    lvk is offline
    Registered User
    Join Date
    Oct 2002
    Posts
    8
    Originally posted by johnnie2
    And on Windows 9x systems, it's winipcfg, not ipconfig.
    thanx but ipconfig/all only shows the ip for the LAN

  8. #8
    lvk
    lvk is offline
    Registered User
    Join Date
    Oct 2002
    Posts
    8
    Originally posted by novacain
    Try

    GetAdaptersAddresses()

    or

    GetAdaptersInfo()

    from

    Iphlpapi header and lib

    depending on your targets OS.

    >>2. in the open text box, type comman, then click ok. The command prompt window opens

    this is OS dependant
    I already checked this functions but i never get the right address.
    thanx

  9. #9
    Agafaed
    Guest
    Code:
    #include <stdio.h> 
    #include <winsock.h> 
    
    int main() 
    { 	char hostname[256]; 
    	struct hostent *h; 
    	int i;
    	WSADATA WSAData; 
    	WSAStartup(MAKEWORD(1,1),&WSAData); 
    
    	if (gethostname(hostname,256) < 0) 
    		return printf ("Erreur gethostname() !"); 
    
    	if ( (h=gethostbyname (hostname)) == NULL) 
    		return printf ("Erreur gethostbyname() !"); 
    
    	printf("\nhostname %s\n", h->h_name);
    
    	for (i=0; h->h_addr_list[i]!=NULL; i++) 
    		printf("ip%d %s\n", i+1, inet_ntoa(*(struct in_addr *)h->h_addr_list[i] ));
    
    	WSACleanup(); 
    	return 0; }

  10. #10
    Registered User johnnie2's Avatar
    Join Date
    Aug 2001
    Posts
    186
    A note about the preceding post: the code outlined above will return a list of all network addresses, meaning you'd have to iterate through the list and throw out addresses known not to be the correct IP address. Basically, you'd see an address beginning with 192.168. or the loopback address and automatically disregard that entry. The method utilized in the preceding post is explained in a Winsock Programmer's FAQ article detailing the capture of all network addresses associated with a machine.

    A significantly easier way is to harvest the address from a connected socket:

    Code:
       SOCKADDR_IN addr;
       
       getsockname(aConnectedSocket, (LPSOCKADDR)&addr, sizeof(struct SOCKADDR_IN));
    The IP address of interest is now stored in addr.sin_addr as an in_addr struct, which may be conveniently transformed into string form with inet_ntoa(). Conversely, if you wanted to retrieve the address of the foreign machine to which you are connected, use getpeername().
    "Optimal decisions, once made, do not need to be changed." - Robert Sedgewick, Algorithms in C

  11. #11
    Banned Cgawd's Avatar
    Join Date
    Oct 2002
    Posts
    405
    I say a good practice would be to enter the name of the compiler you are using whenever you post code, either that or keep it in your signature like many do

  12. #12
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    You could always use a socket library (any you want) and use HTTP to GET this file from the internet:

    http://www.ostrosoft.com/OIT/GetExternalIP.asp

    Take a look at the source of the file returned. It just contains your external IP of your computer. This is what I use to get my external IP in programs. If the link changes you can always place the link string in a file/registry entry/resource file.

    Making a little server program that does this so you can host it on your own server is real easy. So you might want to do that instead if you don't want to use that link (which I see no reason why not).

    Hope I helped .

  13. #13
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    Exactly. Closed.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Converting 32 bit binary IP to decimal IP (vice-versa)
    By Mankthetank19 in forum C Programming
    Replies: 15
    Last Post: 12-28-2009, 06:17 PM
  2. Replies: 4
    Last Post: 03-29-2006, 03:36 PM
  3. getting local ip (not internet ip)
    By ipe in forum C Programming
    Replies: 12
    Last Post: 03-17-2003, 02:10 PM

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