当前位置: 技术问答>linux和unix
一个关于linux网络编程的问题
来源: 互联网 发布时间:2015-02-15
本文导语: 我写了一个获得主机姓名和多个ip地址的程序如下 但在red hat 6.2 下会报错 testsocket.c: In function `main': testsocket.c:10: warning: assignment makes pointer from integer without a cast testsocket.c:15: dereferencing pointer to incomplete ty...
我写了一个获得主机姓名和多个ip地址的程序如下
但在red hat 6.2 下会报错
testsocket.c: In function `main':
testsocket.c:10: warning: assignment makes pointer from integer without a cast
testsocket.c:15: dereferencing pointer to incomplete type
testsocket.c:16: dereferencing pointer to incomplete type
testsocket.c:18: dereferencing pointer to incomplete type
请给予解答
#include
int main()
{
struct hostent *h;
char host[100];
int i;
gethostname(host,100);
printf("host name is %sn",host);
if((h = gethostbyname(host))==NULL)
{
printf("error");
exit(1);
}
printf("Default IP:%s!n",inet_ntoa(((struct in_addr*)h->h_addr)));
for( i=0;ih_length/sizeof(char);i++)
{
printf("IP is %d:%sn",i+1,inet_ntoa(((struct in_addr*)h->h_addr_list[i])));
}
}
但在red hat 6.2 下会报错
testsocket.c: In function `main':
testsocket.c:10: warning: assignment makes pointer from integer without a cast
testsocket.c:15: dereferencing pointer to incomplete type
testsocket.c:16: dereferencing pointer to incomplete type
testsocket.c:18: dereferencing pointer to incomplete type
请给予解答
#include
int main()
{
struct hostent *h;
char host[100];
int i;
gethostname(host,100);
printf("host name is %sn",host);
if((h = gethostbyname(host))==NULL)
{
printf("error");
exit(1);
}
printf("Default IP:%s!n",inet_ntoa(((struct in_addr*)h->h_addr)));
for( i=0;ih_length/sizeof(char);i++)
{
printf("IP is %d:%sn",i+1,inet_ntoa(((struct in_addr*)h->h_addr_list[i])));
}
}
|
加上幾個頭文件,然後把.c改為.cpp即可