网络编程(6)

2019-02-15 18:13

} while(1) {

if((connectfd=accept(listenfd,(struct sockaddr *) &client,&sin_size))==-1) { perror(\ exit(-1);

}

arg=(struct ARG *)malloc(sizeof (struct ARG)); arg -> connfd = connectfd;

memcpy((void *)&arg -> client, &client, sizeof(client));

if(pthread_create(&tid, NULL, start_routine, (void *)arg)) {

perror(\ error.\\n\ exit(-1); } }

close(listenfd); }

static pthread_key_t key;

static pthread_once_t once = PTHREAD_ONCE_INIT; struct DATA_THR { int index; };

static void key_destroy(void *buf) { free(buf); }

static void buffer_key_alloc() {

pthread_key_create(&key, key_destroy); }

26

void savedata(char *recvbuf, int len, char *cli_data) {

struct DATA_THR *data;

pthread_once(&once, buffer_key_alloc);

if ((data = (struct DATA_THR *)pthread_getspecific ( key )) == NULL) { data = (struct DATA_THR *) calloc (1, sizeof(struct DATA_THR)); pthread_setspecific (key, data); data -> index = 0; } int i;

for (i=0; iindex++] = recvbuf[i]; cli_data[data->index] = '\\0'; }

void process_client(int connectfd,struct sockaddr_in client){ char recvbuf[MAXCHARSIZE]; char sendbuf[MAXCHARSIZE]; char client_name[MAXCHARSIZE]; char client_data[5000]; int recvlen,i;

printf(\ send(connectfd,\ recvlen=recv(connectfd,client_name,MAXCHARSIZE,0); if(recvlen==0){ close(connectfd);

printf(\ return; }

else if(recvlen<0){ close(connectfd);

printf(\

27

return; }

client_name[recvlen]='\\0';

printf(\ bzero(recvbuf,1000);

while(recvlen=recv(connectfd,recvbuf,MAXCHARSIZE,0)){ savedata(recvbuf,recvlen,client_data); recvbuf[recvlen]='\\0';

printf(\ message:%s\\n\ for(i=0;i

send(connectfd,sendbuf,strlen(sendbuf),0);

bzero(recvbuf,1000); } /*while*/

printf(\ \\n\ close(connectfd); }

void *start_routine(void *arg) { struct ARG *info; info = (struct ARG *)arg;

process_client(info -> connfd, info -> client); free(arg);

pthread_exit(NULL); }

28

实验六 实现NTP网络授时客户端

参考源代码: 程序源代码:

/*

* ntp_client_by_other_main.c *

* Created on: 2010-9-8 * Author: kerwin */

#include #include #include #include #include #include #include #include #include

#define NTP_SERVER \#define NTP_PORT 123 //

//rfc1305 defined from 1900 so also 2208988800 (1900 - 1970 ) seconds left //

//timeval.tv_sec + JAN_1970 = timestamp.coarse #define JAN_1970 0x83aa7e80

//timeval.tv_usec=>timestamp.fine

#define NTPFRAC(x) (4294 * (x) + ((1981 * (x))>>11)) //timeval.tv_usec<=timestamp.fine

#define USEC(x) (((x) >> 12) - 759 * ((((x) >> 10) + 32768) >> 16))

#define Data(i) ntohl(((unsigned int *)data)[i])

#define LI 0 #define VN 3 #define MODE 3 #define STRATUM 0 #define POLL 4 #define PREC -6

29

struct ntptime { unsigned int coarse; unsigned int fine; };

void send_packet(int fd) { unsigned int data[12]; struct timeval now; int ret; if (sizeof(data) != 48) { fprintf(stderr, \ return; } memset((char*) data, 0, sizeof(data)); data[0] = htonl((LI << 30) | (VN << 27) | (MODE << 24) | (STRATUM << 16) | (POLL << 8) | (PREC & 0xff)); data[1] = htonl(1 << 16); /* Root Delay (seconds) */ data[2] = htonl(1 << 16); /* Root Dispersion (seconds) */ gettimeofday(&now, NULL); data[10] = htonl(now.tv_sec + JAN_1970); /* Transmit Timestamp coarse */ data[11] = htonl(NTPFRAC(now.tv_usec)); /* Transmit Timestamp fine */ send(fd, data, 48, 0); }

void get_udp_arrival_local_timestamp( struct ntptime *udp_arrival_local_timestamp) { struct timeval udp_arrival_local_time; gettimeofday(&udp_arrival_local_time, NULL); //print just precise to second printf(\}

void get_new_time(unsigned int *data, struct timeval* new_time) { //int li, vn, mode, stratum, poll, prec; //int delay, disp, refid; //struct ntptime reftime, orgtime, rectime; struct ntptime trantime; /*li = Data(0) >> 30 & 0x03; vn = Data(0) >> 27 & 0x07; mode = Data(0) >> 24 & 0x07; stratum = Data(0) >> 16 & 0xff;

30


网络编程(6).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:2013版用于立项激光显示项目可行性研究报告(甲级资质)审查要求

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: