static void *time_serv(void *arg) { printf("This is the new window for time service\n"); Pthread_detach(pthread_self()); int sockfd=*((int*)arg); free(arg); time_t ticks; char buff[MAXLINE]; while(1) { if(Readable_timeo(sockfd, 5)==0) { ticks=time(NULL); snprintf(buff, sizeof(buff), "%.24s\r\n", ctime(&ticks)); Write(sockfd, buff, strlen(buff)); printf("Current time is sent to client\n"); } else break; } printf("This time service is terminated\n"); Close(sockfd); return NULL; }
void dg_cli(FILE *fp, int sockfd, const SA *pservaddr, socklen_t servlen) { int n; char sendline[MAXLINE], recvline[MAXLINE + 1]; while (Fgets(sendline, MAXLINE, fp) != NULL) { Sendto(sockfd, sendline, strlen(sendline), 0, pservaddr, servlen); if (Readable_timeo(sockfd, 5) == 0) { fprintf(stderr, "socket timeout\n"); } else { n = Recvfrom(sockfd, recvline, MAXLINE, 0, NULL, NULL); recvline[n] = 0; /* null terminate */ Fputs(recvline, stdout); } } }
static void timeRequest(int sockfd) { ssize_t n; static char buf[MAXLINE]; time_t ticks; static char buff[MAXLINE]; // Write(connfd, buff, strlen(buff)); while (1) { int tmp = Readable_timeo(sockfd, 5); if (tmp == 0) { //n = Recvfrom(sockfd, buf, MAXLINE, 0, NULL, NULL); ticks = time(NULL); snprintf(buff, sizeof(buff), "%.24s\r\n", ctime(&ticks)); printf("current time %s\n", buff); Writen(sockfd, buff, strlen(buff)); // fprintf(stderr, "socket timeout\n"); continue; } else { // n = Recvfrom(sockfd, buf, MAXLINE, 0, NULL, NULL); n = recvfrom(sockfd, buf, MAXLINE, 0, NULL, NULL); if (n == 0) { printf( "time client child process termination : socket read returned with value 0\n\n"); pthread_t tid; pthread_exit(&tid); } else if (n < 0) { if (errno == EWOULDBLOCK) fprintf(stderr, "socket timeout\n"); } } } }
void dg_cli1(int sockfd) { int n; char sendline[MAXLINE], recvline[MAXLINE + 1]; // Writen(sockfd, sendline, strlen(sendline)); int tmp1 = Readable_timeo(sockfd, 5); if (tmp1 == 0) { fprintf(stderr, "socket timeout\n"); //continue; } else { n = Recvfrom(sockfd, recvline, MAXLINE, 0, NULL, NULL); recvline[n] = 0; /* null terminate */ Fputs(recvline, stdout); } }