#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
int main(int argc,char *argv[])
{
int rc;
int sockfd;
char buf[BUFLEN+1];
char *pc;
struct sockaddr_in sa;
sturct hostent *hen;
if(argc<2)
{
fprintf(stderr,"Missing host name\n");
exit(1);
}
hen=gethostbyname(argv[1]);
if(!hen)
{
perror("couldn't resolve host name");
}
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("connet() failed");
}
pc=buf;
while(rc=read(sockfd,pc,BUFLEN-(pc-buf)))
{
pc+=rc;
}
close(sockfd);
*pc='\0';
printf("Time: %s",buf);
return 0;
}
$ g++ -g -o TimeService TimeService.c -lsocket -lnsl
TimeService.c: In function `int main(int, char **)':
TimeService.c:19: syntax error before `*'
TimeService.c:25: `hen' undeclared (first use thisfunction)
TimeService.c:25: (Each undeclared identifier isreported only once
TimeService.c:25: for each function it appears in.)
$ gdb TimeService
GNU gdb 4.18 (FreeBSD)
Copyright 1998 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General
Public License, and you arewelcome to change it and/or distribute copies of it
under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for details.
This GDB was configured as "i386-unknown-freebsd"...
TimeService: No such file or directory.
(gdb) q
刚开始学习用gcc编程(Unix下),按书上无改动的输入代码后,不能成功生成可执行文件。检查上面代码后一直找不出错误所在,麻烦各位帮帮忙啊。