WORD wVersionRequested;
WSADATA wsaData;
char name[255];
char *ip;
PHOSTENT hostinfo;
wVersionRequested = MAKEWORD( 2, 0 );
ip = (char *)malloc(100);
if ( WSAStartup( wVersionRequested, &wsaData ) == 0 )
{
if( gethostname ( name, sizeof(name)) == 0)
{
if((hostinfo = gethostbyname(name)) != NULL)
{
ip = inet_ntoa (*(struct in_addr *)*hostinfo->h_addr_list);
}
}
WSACleanup( );
}
printf("Your IP is: %s\n", ip);
Getting users IP
Trying to get the IP off the computer that is running the program. The following code works but if the user is behind a router then it gives the IP of the router and not the computer itself. What do I have to do to just get the IP of the computer?
if u read the forum faq you will notice an NB which says never try to get the local IP!
when u want to get the Ip of a machine (especially needed while communicating), when the the client first connects to the server u can get its Ip from the accept(..) and save that IP this will be the IP u ll use to communicate with the client , never try to get the client IP locally then try to communicate with it, thiswon't work in all the cases!
when u want to get the Ip of a machine (especially needed while communicating), when the the client first connects to the server u can get its Ip from the accept(..) and save that IP this will be the IP u ll use to communicate with the client , never try to get the client IP locally then try to communicate with it, thiswon't work in all the cases!
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement