| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 885 人关注过本帖
标题:[求助]书上的代码不能编译
只看楼主 加入收藏
ya07228
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2006-8-23
收藏
 问题点数:0 回复次数:2 
[求助]书上的代码不能编译
一个网络编程实例,该实例是一个客户端程序,它首先连接到一个标准时间服务器上,从服务器读取当前时间,然后显示时间。多数Unix主机有一个标准时间服务器,它随系统的启动而启动,并在TCP端口13等待。当它接受到客户连接请求后,则建立连接,然后将时间以字符串形式发送给客户,最后关闭连接。


#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下),按书上无改动的输入代码后,不能成功生成可执行文件。检查上面代码后一直找不出错误所在,麻烦各位帮帮忙啊。


搜索更多相关主题的帖子: 编译 代码 
2006-11-22 16:11
卧龙孔明
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:59
帖 子:3872
专家分:684
注 册:2006-10-13
收藏
得分:0 
注意:不同的编译系统对代码的要求是不同的,因此你写代码时最好明确所用编译器(如GCC与TC),还有,你的代码引用的头文件好象在C99中没有(难道是C++??,你可以试试用G++,哈哈,具体的在unix下的操作我就不说了,好象与GCC相似).还有,如果你的是VC代码,就不用指望在其他的非微软类的系统中编译的.

My Blog: www.aiexp.info
虽然我的路是从这里开始的,但是这里不再是乐土.感谢曾经影响过,引导过,帮助过我的董凯,飞燕,leeco,starwing,Rockcarry,soft_wind等等等等.别了,BCCN.
2006-11-23 19:41
hao0716
Rank: 4
等 级:业余侠客
威 望:1
帖 子:353
专家分:222
注 册:2006-4-11
收藏
得分:0 

[CODE]#include <vxWorks.h>
#include <loglib.h>
#include <stdio.h>
#include <sysLib.h>
#include <taskLib.h>
#include <errnoLib.h>
#include "sockLib.h"
#include "time.h"
#include "Sntp.h"
#include "ioLib.h"
#include "inetLib.h"
#include "hostlib.h"
#include "errnoLib.h"
#include "sntpcLib.h"
#define syslogPrintf printf
/* defines */
#define PORT_NUM 123
#define TIMEOUT 5

static struct sockaddr_in caddr;/*client address*/
static struct sockaddr_in saddr;/*server address*/
/* globals */
static SNTP_PACKET sntpRequest, sntpReply;
int sntpSocket;
u_short sntpPort;
static void CreateSntpRequest();
static STATUS SntpTask(char* ServerID, int interval);
LOCAL ULONG sntpcFractionToNsec
(
ULONG sntpFraction /* base 2 fractional part of the NTP timestamp */
)
{
ULONG factor = 0x8AC72305; /* Conversion factor from base 2 to base 10 */
ULONG divisor = 10; /* Initial exponent for mantissa. */
ULONG mask = 1000000000; /* Pulls digits of factor from left to right. */
int loop;
ULONG nsec = 0;
BOOL shift = FALSE; /* Shifted to avoid overflow? */

/*
* Adjust large values so that no intermediate calculation exceeds
* 32 bits. (This test is overkill, since the fourth MSB can be set
* sometimes, but it's fast).
*/
if (sntpFraction & 0xF0000000)
{
sntpFraction /= 10;
shift = TRUE;
}
/*
* In order to increase portability, the following conversion avoids
* floating point operations, so it is somewhat obscure.
*
* Incrementing the NTP fractional part increases the corresponding
* decimal value by 2^(-32). By interpreting the fractional part as an
* integer representing the number of increments, the equivalent decimal
* value is equal to the product of the fractional part and 0.2328306437.
* That value is the mantissa for 2^(-32). Multiplying by 2.328306437E-10
* would convert the NTP fractional part into the equivalent in seconds.
*
* The mask variable selects each digit from the factor sequentially, and
* the divisor shifts the digit the appropriate number of decimal places.
* The initial value of the divisor is 10 instead of 1E10 so that the
* conversion produces results in nanoseconds, as required by POSIX clocks.
*/
for (loop = 0; loop < 10; loop++) /* Ten digits in mantissa */
{
nsec += sntpFraction * (factor/mask)/divisor; /* Use current digit. */
factor %= mask; /* Remove most significant digit from the factor. */
mask /= 10; /* Reduce length of mask by one. */
divisor *= 10; /* Increase preceding zeroes by one. */
}
/* Scale result upwards if value was adjusted before processing. */
if (shift)
nsec *= 10;
return (nsec);
}

BOOL SntpcInit(char* ServerID, int interval)
{
STATUS Status;

Status = taskSpawn("sntp", 200, 0, 8192, (FUNCPTR)SntpTask, (int)ServerID, interval,3,4,5,6,7,8,9,10);
if(Status == ERROR)
return FALSE;
return TRUE;
}
void CreateSntpRequest(char* ServerID)
{
bzero ( (char *)&sntpRequest, sizeof (sntpRequest));
sntpRequest.leapVerMode = 0xE3;/*SNTP_CLIENT_REQUEST;*/
sntpRequest.poll=4;
sntpRequest.precision=-6;

sntpRequest.referenceIdentifier = hostGetByName (ServerID);
if (sntpRequest.referenceIdentifier == ERROR)
sntpRequest.referenceIdentifier = inet_addr (ServerID);
}
STATUS UpdateTime(char* ServerID,int interval)
{
int FromLen;
int i;
fd_set readFds;
struct timeval sockTimeout;
struct timespec Clock;
int result;
time_t lt;

i=0;
bzero((char*)&saddr, sizeof(saddr)); /* zero the rest of the struct */
saddr.sin_family = AF_INET;
saddr.sin_port = htons (PORT_NUM);
saddr.sin_addr.s_addr = hostGetByName (ServerID);
if (saddr.sin_addr.s_addr == ERROR)
saddr.sin_addr.s_addr = inet_addr (ServerID);

do{
if(sendto(sntpSocket, (caddr_t)&sntpRequest, sizeof(sntpRequest),
0, (struct sockaddr*)&saddr, sizeof(struct sockaddr)) == -1)
{
return (ERROR);
}

i+=1;
sockTimeout.tv_sec = TIMEOUT;
sockTimeout.tv_usec = 0;

/* Wait for reply at the ephemeral port selected by the sendto () call. */
FD_ZERO (&readFds);
FD_SET (sntpSocket, &readFds);
result = select (FD_SETSIZE, &readFds, NULL, NULL, &sockTimeout);
}while(!((result == 1)||(i == 3)));

if (result == -1)
{
return (ERROR);
}
if (result == 0) /* TIMEOUT interval expired. */
{
errnoSet (S_sntpcLib_TIMEOUT);
return (ERROR);
}
FromLen = sizeof(saddr);
result = recvfrom(sntpSocket, (caddr_t)&sntpReply, sizeof(sntpReply), 0,
(struct sockaddr*)&saddr, &FromLen);
if (result == -1)
{

return (ERROR);
}


/*update option from latest packet*/
sntpRequest.stratum = sntpReply.stratum+1;
sntpRequest.poll = sntpReply.poll;
sntpRequest.referenceTimestampSec = sntpReply.transmitTimestampSec;
sntpRequest.referenceTimestampFrac = sntpReply.transmitTimestampFrac;

Clock.tv_sec = ntohl(sntpReply.transmitTimestampSec) - SNTP_UNIX_OFFSET;
Clock.tv_nsec = ntohl(sntpcFractionToNsec(sntpReply.transmitTimestampFrac));
clock_settime(CLOCK_REALTIME, &Clock);

sntpRequest.transmitTimestampSec=htonl(time(&lt)+SNTP_UNIX_OFFSET+(interval));
sntpRequest.transmitTimestampFrac=htonl(sntpcFractionToNsec(time(&lt)));

syslogPrintf("The time is %s\n\n",asctime(localtime(&lt)));
return (OK);
}
STATUS SntpTask(char* ServerID, int interval)
{
STATUS Status;

sntpSocket = socket (AF_INET, SOCK_DGRAM, 0);
if(sntpSocket == -1)
{
syslogPrintf("Socket failed.\n");
return (ERROR);
}

bzero((char*)&caddr, sizeof(caddr));/* zero the rest of the struct */
caddr.sin_family = AF_INET; /* host byte order */
caddr.sin_port = htons (PORT_NUM);
caddr.sin_addr.s_addr = INADDR_ANY ;

if((bind (sntpSocket, (struct sockaddr *)&caddr, sizeof (caddr))) == -1)
{
syslogPrintf("Bind failed.\n");
return (ERROR);
}

CreateSntpRequest(ServerID);

for(;;)
{
Status = UpdateTime(ServerID, interval);
if(Status == ERROR)
{
printf("%s\n", strerror(errnoGet()));
}
taskDelay(interval*sysClkRateGet());
}
return OK;
}

int ShowTime()
{
time_t lt;
time(&lt);
printf("%s\n", asctime(localtime(&lt)));
return 0;
}[/CODE]

自己写的SNTP的代码 稍微改一下应该可以在UNIX下用的 我这个是vxworks下用的


2006-11-24 10:13
快速回复:[求助]书上的代码不能编译
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016642 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved