- 论坛徽章:
- 8
|
gethostbyname问题!
- char MyName[20];
- char *hostname, *ptr;
- struct hostent *HostIP;
- struct in_addr inip;
-
- /* 得到该运行程序所在主机的主机名称 */
- if( gethostname(MyName, sizeof(MyName))<0 )
- {
- printf("Gethostname Error\n");
- return;
- }
- printf("My Host Name is %s\n",MyName);
-
- if(!(HostIP = gethostbyname(MyName)))
- {
- printf("Get host name failed.\n");
- return;
- }
- /* 得到该运行程序所在主机的IP地址(形如 *.*.*.* ) */
- memset((void *)&inip, sizeof(inip), 0);
- hostname=HostIP->;h_name;
- inip.s_addr=*((unsigned long *)HostIP->;h_addr_list[0]);
- if(!(ptr=(char *)inet_ntoa(inip)))
- memcpy(&inip,HostIP->;h_addr,HostIP->;h_length);
- printf("My IP is %s\n",ptr);
复制代码 |
|