免费注册 查看新帖 |

Chinaunix

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

笨笨地问个问题! [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-07-03 16:47 |只看该作者 |倒序浏览
源码为:

#include <stdio.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <netdb.h>
#include <unistd.h>
#include <string.h>

#define HOSTNAMELEN 40
#define BUFLEN 1024
#define PORT 13 /* port of daytime server */

int main(int argc,char *argv[])
{
        int rc;
        int sockfd; /* socket descriptor */
        char buf[BUFLEN+1];
        char * pc;
        struct sockaddr_in sa;
        struct hostent * hen;

        if (argc<2)
        {
                fprintf(stderr,"Missing host name\n");
                exit(1);
        }

        /* Address resolution */
        hen=gethostbyname(argv[1]);
        if (!hen)
        {
                perror("couldn't resolve host name");
        }

        memset(&sa,0,sizeof(sa));
        sa.sin_family=AF_INET;
        sa.sin_port=htons(PORT);
        memcpy(&sa.sin_addr.s_addr,hen->h_addr_list[0],hen->h_length);

        sockfd=socket(AF_INET,SOCK_STREAM,0);
        if (sockfd<0)
        {
                perror("socket() failed");
        }

        rc=connect(sockfd,(struct sockaddr *)&sa,sizeof(sa));
        if (rc)
        {
                perror("connect() failed");
        }

        /* reading the socket */
        pc=buf;
        while (rc=read(sockfd,pc,BUFLEN-(pc-buf)))
        {
                pc+=rc;
        }

        /* close the socket */
        close(sockfd);
        /* pad null character to the end of the result */
        *pc='\0';
        /* print the result */
        printf("Time: %s",buf);
        /* and terminate */
        return 0;
}

编译报错:
$ make
        cc -c 1_1.c
        cc -g -o 1_1 1_1.o
undefined                       first referenced
symbol                             in file
gethostbyname                       1_1.o
htons                               1_1.o
socket                              1_1.o
connect                             1_1.o
i386ld fatal: Symbol referencing errors. No output written to 1_1
*** Error code 13 (bu21)


经检查后,.h文件都存在的.请教大致出了什么问题?

论坛徽章:
0
2 [报告]
发表于 2008-07-03 16:56 |只看该作者
makefile 出了问题
少了 -lsocket -lnsl
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP