- 论坛徽章:
- 0
|
struct hostent *phe = NULL; /* host entry */
struct servent *pse = NULL; /* port number */
struct protoent *ppe = NULL; /* protocol number */
struct sockaddr_in sin;
int s, type;
char name[20];
memset(&sin, 0, sizeof(sin));
sin.sin_family = AF_INET; /* Address Family: Internet IP Protocol */
gethostname(name, 20);
/* map hostname to ip address */
if( phe = gethostbyname(name) )
memset(&sin.sin_addr, phe->h_addr, phe->h_length);
printf("ip address is %s\n", inet_ntoa(sin.sin_addr));
主机名name是队的,phe也不是NULL,为什么打印出来的ip地址就不对呢?
机器只有一个网卡 |
|