免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2281 | 回复: 2
打印 上一主题 下一主题

网络编程,说我的in_addr未定义 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2004-04-08 15:52 |只看该作者 |倒序浏览
程序:        #include <stdio.h>;
        #include <unistd.h>;
        #include <strings.h>;
        #include <sys/types.h>;
        #include <sys/socket.h>;
        #include <netinet/in.h>;
        #include <netdb.h>;        /* netbd.h is needed for struct hostent  */

        #define PORT 1234   /* Open Port on Remote Host */
        #define MAXDATASIZE 100   /* Max number of bytes of data */

        int main(int argc, char *argv[])
        {
        int fd, numbytes;   /* files descriptors */
        char buf[MAXDATASIZE];  /* buf will store received text */
        struct hostent *he;         /* structure that will get information about remote host */
        struct sockaddr_in server;  /* server's address information */

        if (argc !=2) {       /* this is used because our program will need one argument (IP) */
        printf("Usage: %s <IP Address>;\n",argv[0]);
        exit(1);
        }

        if ((he=gethostbyname(argv[1]))==NULL){ /* calls gethostbyname() */
        printf("gethostbyname() error\n";
        exit(1);
        }

        if ((fd=socket(AF_INET, SOCK_STREAM, 0))==-1){  /* calls socket() */
        printf("socket() error\n";
        exit(1);
        }

        bzero(&server,sizeof(server));
        server.sin_family = AF_INET;
        server.sin_port = htons(PORT); /* htons() is needed again */
        server.sin_addr = *((in_addr *)he->;h_addr);  /*he->;h_addr passes "*he"'s info to "h_addr" */ 就是这一行有问题,同时显示parse error before ')' token
        if(connect(fd, (struct sockaddr *)&server,sizeof(struct sockaddr))==-1){ /* calls connect() */
        printf("connect() error\n";
        exit(1);
        }

        if ((numbytes=recv(fd,buf,MAXDATASIZE,0)) == -1){  /* calls recv() */
        printf("recv() error\n";
        exit(1);
        }

        buf[numbytes]='\0';
        printf("Server Message: %s\n",buf); /* it prints server's welcome message  */

        close(fd);   /* close fd */
}
系统是vm的redhat8.0 gcc
~~~~~~~~~~~~~~~~~~~``
谢谢各位大侠

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
2 [报告]
发表于 2004-04-08 16:04 |只看该作者

网络编程,说我的in_addr未定义

注意,in_addr 应该写做 struct in_addr!!!!!!!!

你那种写法是 C++ 才支持的。

论坛徽章:
0
3 [报告]
发表于 2004-04-08 16:15 |只看该作者

网络编程,说我的in_addr未定义

谢谢谢!!!!
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP