关于gethostbyaddr的问题
我想通过gethostbyaddr返回机器的主机名字,但就是按照msdn的例子也不行,我用watch看取回的值是0x00000000,msdn的程序如下:
//----------------------
// Declare and initialize variables
hostent* remoteHost;
char* host_name;
unsigned int addr;
//----------------------
// User inputs name of host
printf("Input name of host: ");
host_name = (char*) malloc(sizeof(char*)*16);
fgets(host_name, 16, stdin);
// If the user input is an alpha name for the host, use gethostbyname()
// If not, get host by addr (assume IPv4)
if (isalpha(host_name[0])) { /* host address is a name */
host_name[strlen(host_name)-1] = '\0'; /* NULL TERMINATED */
remoteHost = gethostbyname(host_name);
}
else {
addr = inet_addr(host_name);
remoteHost = gethostbyaddr((char *) &addr, 4, AF_INET);
}
if (WSAGetLastError() != 0) {
if (WSAGetLastError() == 11001)
printf("Host not found...\nExiting.\n");
}
else
printf("error#:%ld\n", WSAGetLastError());
// The remoteHost structure can now be used to
// access information about the host
想请教一下