How can I get my own ip address that represents me in the internet in my c/c++ program.
How can I get my own ip address that represents me in the internet in my c/c++ program.
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
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
simple,How can I get my own ip address that represents me in the internet in my c/c++ program.
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
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 11: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
And on Windows 9x systems, it's winipcfg, not ipconfig.Originally posted by correlcj
3. At the DOS prompt type ipconfig/all. abracadabra!! You IP address appears.
"Optimal decisions, once made, do not need to be changed." - Robert Sedgewick, Algorithms in C
thanx but ipconfig/all only shows the ip for the LANOriginally posted by johnnie2
And on Windows 9x systems, it's winipcfg, not ipconfig.
I already checked this functions but i never get the right address.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
thanx
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; }
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:
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().Code:SOCKADDR_IN addr; getsockname(aConnectedSocket, (LPSOCKADDR)&addr, sizeof(struct SOCKADDR_IN));
"Optimal decisions, once made, do not need to be changed." - Robert Sedgewick, Algorithms in C
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
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 .
Exactly. Closed.
Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.