Chinaunix首页 | 论坛 | 博客
  • 博客访问: 543304
  • 博文数量: 56
  • 博客积分: 10010
  • 博客等级: 上将
  • 技术积分: 2255
  • 用 户 组: 普通用户
  • 注册时间: 2006-10-18 20:14
文章分类

全部博文(56)

文章存档

2011年(1)

2009年(6)

2008年(49)

我的朋友

分类: 项目管理

2008-05-30 10:56:38

/*timeserv.c a socket-based time of day server*/
#include
#include
#include
#include
#include
#include
#include
#include

#define PORTNUM 13000 /*our time service phone number*/
#define HOSTLEN 256
#define oops(msg) {perror(msg);exit(1);}

int main(int ac,char * av[])
{
struct sockaddr_in saddr; /*build our address here*/
struct hostent *hp; /*this is part of our*/
char hostname[HOSTLEN]; /*address*/
int sock_id,sock_fd; /*line id,file desc*/
FILE *sock_fp; /*use socket as stream*/
char *ctime(); /*convert secs to string*/
time_t thetime; /*the time we report*/


/*
*step 1:ask kernel for a socket
*/

sock_id=socket(PF_INET,SOCK_STREAM,0); /*GET a socket*/
if(sock_id==-1)
oops("socket");

/*
*step 2:bind address to socket,address is host,port
*/

bzero((void *)&saddr,sizeof(saddr)); /*clear out struct*/

gethostname(hostname,HOSTLEN); /*WHERE am i?*/
hp=gethostbyname(hostname); /*get info about host*/

bcopy((void *)hp->h_addr,(void *)&saddr.sin_addr,hp->h_length);
saddr.sin_port=htons(PORTNUM); /*fill in socket port*/
saddr.sin_family=AF_INET; /*fill in address family*/

if(bind(sock_id,(struct sockaddr *)&saddr,sizeof(saddr))!=0)
oops("bind");

/*
*step 3:allow incoming calls with qsize=1 on socket
*/
if(listen(sock_id,1)!=0)
oops("listen");

/*
*main loop :accept(),write(),close()
*/

while(1){
sock_fd=accept(sock_id,NULL,NULL); /*WAIT FOR CALL */
printf("Wow! get a call!\n");
if(sock_fd==-1)
oops("accept"); /*error getting calls*/
sock_fp=fdopen(sock_fd,"w"); /*we'll write to the*/
if(sock_fp==NULL) /*socket as a stream*/
oops("fdopen");

thetime=time(NULL); /*get time*/
/*and convert to string*/
fprintf(sock_fp,"The time here is..");
fprintf(sock_fp,"%s",ctime(&thetime));
fclose(sock_fp); /*release connection*/
}
}






阅读(748) | 评论(0) | 转发(0) |
0

上一篇:timeclnt

下一篇:Reliable DNS Forgery in 2008[转]

给主人留下些什么吧!~~