读书人

hostinfo = gethostbyname(host);赋值

发布时间: 2012-03-09 21:42:54 作者: rapoo

hostinfo = gethostbyname(host);赋值错误,不知道为什么
//getnameServer.cpp -- main
#include <stdlib.h>
#include <stdio.h>
#include <winsock2.h>

int main()
{
struct hostent* FAR hostinfo;
char FAR * host;
char FAR FAR** addrs;

printf( "Input name of host: ");

//----------------------
// Allocate 64 byte char string for host name
host = (char*) malloc(sizeof(char)*64);
fgets(host, 64, 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[0])) { /* host address is a name */

if (host[strlen(host)-1] == '\n ')
host[strlen(host)-1] = '\0 ';
hostinfo = gethostbyname(host);
}

printf( "results for host %s:\n ",host);
printf( "Name: %s\n ",hostinfo-> h_name);

addrs = hostinfo-> h_addr_list;
while(*addrs)
{
printf( " %s ",inet_ntoa(*(struct in_addr *)*addrs));
addrs++;
}
printf( "\n ");
return 0;
}

hostinfo = gethostbyname(host);这句赋值总是不成功,照着msdn上的例子写的啊,
host已经等于 "localhost "的了,但是hostinfo就还是不能得到正确的值,

[解决办法]
接分ing

读书人网 >C++

热点推荐