标签:解决方法 min 调用 网络编程 tar sig get http targe
网络编程中需要考虑的一些情况
1 /**************服务器*******************************/ 2 int main() 3 { 4 int listenfd, connfd; 5 pid_t childpid; 6 socklen_t chilen; 7 struct sockaddr_in cliaddr, servaddr; 8 9 listenfd = Socket(AF_INET, SOCK_STREAM, 0); 10 bzero(&servaddr, sizeof(servaddr)); 11 servaddr.sin_family = AF_INET; 12 servaddr.sin_addr.s_addr = htol(INADDR_ANY); 13 Bind(listenfd, (SA *)&servaddr, sizeof(servaddr)); 14 Listen(listenfd, LISTENQ); 15 16 for(;;) 17 { 18 clilen = sizeof(cliaddr); 19 connfd = Accept(listenfd, (SA *)&cliaddr, &clilen); 20 if((clild=Fork()) == 0) 21 { 22 Close(listenfd); 23 str_echo(connfd); 24 exit(0); 25 } 26 Close(connfd); 27 } 28 } 29 30 void str_echo(int sockfd) 31 { 32 ssize_t n; 33 char buf[MAXLINE]; 34 again: 35 while((n = read(sockfd, buf, MAXLINE))>0) 36 { 37 Writen(sockfd, buf, n) 38 } 39 if(n<0 && errno == EINTR) 40 { 41 goto again; 42 } 43 else if 44 { 45 err_sys("str_echo:read error"); 46 } 47 } 48 /*********************客户端***********************/ 49 int main(int argc, char **argv) 50 { 51 int sockfd; 52 strcut sockaddr_in servaddr; 53 if(argc != 2) 54 { 55 err_quit("usage:tcpcli"); 56 } 57 sockfd = Socket(AF_INET, SOCK_STREAM, 0); 58 bzero(&servaddr, sizeof(servaddr)); 59 servaddr.sin_family = AF_INET; 60 servaddr.sin_port = htons(SERV_PORT); 61 Inet_pton(AF_INET, argv[1], &servaddr.sin_addr); 62 Connect(sockfd, (SA *)&servaddr, sizeof(servaddr)); 63 str_cli(stdin, sockfd); 64 exit(0); 65 } 66 67 void str_cli(FILE *fp, int sockfd) 68 { 69 char sendline[MAXLINE], recvline[MAXLINE]; 70 71 while(Fgets(sendline, MAXLINE, fp) != NULL) 72 { 73 Writen(sockfd, sendline, strlen(sendline)); 74 if(Readline(sockfd, recvline, MAXLINE) == 0) 75 { 76 err_quit("str_cli: server terminated prematurely"); 77 } 78 Fputs(recvline, stdout); 79 } 80 } 81 /**********************************************/
1 Signal(SIGCHLD, sig_chld); 2 3 Void sig_chld(int signo) 4 { 5 Pid_t pid; 6 Int stat; 7 8 Pid = wait(&stat); 9 Printf(”child %d terminated\n”, pid); 10 Return; 11 } 12 SIGCHLID在服务器父进程阻塞于慢系统调用accept时有父进程捕获,内核于是致使accept返回ENINTR错误,而父进程不处理该错误,所以会被中止,需要修改上面的服务器代码。 13 for(;;) 14 { 15 clilen = sizeof(cliaddr); 16 if((connfd=accept(listenfd, (SA *)&cliaddr, &clilen))<0) 17 { 18 if(errno == EINTR) 19 { 20 continue; 21 } 22 else 23 { 24 err_sys("accept error"); 25 } 26 } 27 }
1 void sig_chld(int signo) 2 { 3 pid_t pid; 4 int stat; 5 6 while((pid = waitpid(-1, &stat, WNOHANG))>0) 7 { 8 printf("child %d terminated\n", pid); 9 } 10 return; 11 }
1 void str_cli(FILE *fp, int sockfd) 2 { 3 int maxfdp1; 4 fd_set rset; 5 char sendline[MAXLINE], recvline[MAXLINE]; 6 7 FD_ZERO(&rset); 8 for(;;) 9 { 10 FD_SET(fileno(fp), &rset); 11 FD_SET(sockfd, &rset); 12 maxfdp1 = max(fileno(fp), sockfd) +1; 13 Select(maxfdp1, &rset, NULL, NULL, NULL); 14 if(FD_ISSET(sockfd, &rset)) 15 { 16 if(Readline(sockfd, recvline, MAXLINE) == 0) 17 { 18 err_quit("str_cli:server terminated prematurely"); 19 } 20 Fputs(recvline, stdout); 21 } 22 if(FD_ISSET(fileno(fp), &rset)) 23 { 24 if(Fgets(sendline, MAXLINE, fp) == NULL) 25 { 26 return; 27 } 28 Writen(sockfd, sendline, strlen(sednline)); 29 } 30 } 31 }
1 void str_cli(FILE *fp, int sockfd) 2 { 3 char sendline[MAXLINE], recvline[MAXLINE]; 4 5 while(Fgets(sendline, MAXLINE, fp) != NULL) 6 { 7 Writen(sockfd, sendline, 1); 8 Sleep(1); 9 Writen(sockfd, sendline, strlen(sendline)-1); 10 if(Readline(sockfd, recvline, MAXLINE) == 0) 11 { 12 err_quit("str_cli: server terminated prematurely"); 13 } 14 Fputs(recvline, stdout); 15 } 16 }
标签:解决方法 min 调用 网络编程 tar sig get http targe
原文地址:http://www.cnblogs.com/lenglingshuang123/p/6708124.html