Detecting a PCs IP address in C++
Posted on 1998-08-11
I've been trying to write a small command line utility which will detect the IP address of a PC. This is going to be used in a login script to work out what geographical region the PC is located in.
I've tried to implement this using the winsock function as shown below, but I keep on hitting the brick wall.
The WSAAddressToString & getsockname functiosn will give the address, but I first need to bind the address tothe socket. catch - 22.
Does anyone know how to query the IP address ?
sockaddr FAR *name;
bytesRequired = sizeof(buffer);
err = EnumProtocols(NULL/*IPPROTO_TCP*/, buffer, &bytesRequired );
if ( err <= 0 ) return;
protocolCount = err;
protocolInfo = (PPROTOCOL_INFO)buffer;
for ( i = 0, protocolIndex = 0;
i < protocolCount && protocolIndex < 10;
i++, protocolInfo++ )
if (WSAStartup(MAKEWORD(2,0),&wsaData )) return;
s = socket(protocolInfo->iAddressFamily,SOCK_STREAM,protocolInfo->iProtocol);
if (getsockname(s,name,&sizename)) err=WSAGetLastError();
l_string = (char *) calloc(1,256);