Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.85 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <stdlib.h>
  4. #include <errno.h>
  5. #include <string.h>
  6. #include <time.h>
  7. #include <signal.h>
  8. #include <sys/wait.h>
  9. #include <sys/types.h>
  10. #include <sys/socket.h>
  11. #include <netinet/in.h>
  12. #include <arpa/inet.h>
  13. #include <netdb.h>
  14.  
  15. static void sigchld_handler() {
  16. pid_t PID;
  17. int status;
  18.  
  19. while (PID = waitpid(-1, &status, WNOHANG) > 0)
  20. printf("子程序 %d 結束.\n", PID);
  21.  
  22. /* Re-install handler */
  23. signal(SIGCHLD, sigchld_handler);
  24. }
  25.  
  26. int main(int argc, char **argv) {
  27. int z, len_inet;
  28. struct sockaddr_in adr_srvr, adr_clnt;
  29. int sockfd, connfd;
  30. pid_t PID;
  31. time_t clock;
  32. struct tm *tm;
  33. int len_time, i;
  34. int buf[256];
  35. char score[5][256]={"","","","",""};
  36. int tcpclient[5];
  37. int mesgpipe[2],countpipe[2];
  38. int count = 0;
  39. int fd1[2],fd2[2];
  40. int smaller = 100;
  41.  
  42. signal(SIGCHLD, sigchld_handler);
  43.  
  44. memset(&adr_srvr, 0, sizeof(adr_srvr));
  45.  
  46. if (argc ==2)
  47. adr_srvr.sin_addr.s_addr = inet_addr(argv[1]);
  48.  
  49. adr_srvr.sin_family = AF_INET;
  50. adr_srvr.sin_port = htons(9090);
  51.  
  52. sockfd = socket(AF_INET, SOCK_STREAM, 0);
  53. if (sockfd == -1) {
  54. perror("socket error");
  55. exit(1);
  56. }
  57.  
  58. z = bind(sockfd, (struct sockaddr *)&adr_srvr, sizeof(adr_srvr));
  59. if (z == -1) {
  60. perror("bind error");
  61. exit(1);
  62. }
  63.  
  64. z = listen(sockfd, 10);
  65. if (z == -1) {
  66. perror("listen error");
  67. exit(1);
  68. }
  69. /*
  70. if (pipe(fd1)==-1)
  71. {
  72. fprintf(stderr, "Pipe Failed" );
  73. return 1;
  74. }
  75. if (pipe(fd2)==-1)
  76. {
  77. fprintf(stderr, "Pipe Failed" );
  78. return 1;
  79. }
  80. */
  81. while(1) {
  82. printf("a new TCP cilent in , server has deal %d clients now\n",count++);
  83. len_inet = sizeof(adr_clnt);
  84. connfd = accept(sockfd, (struct sockaddr *)&adr_clnt, &len_inet);
  85. tcpclient[count] = connfd;
  86. if (connfd == -1) {
  87. perror("connect error");
  88. exit(1);
  89. }
  90. //創建PIPE管道(READ)
  91. if (pipe(fd1)==-1)
  92. {
  93. fprintf(stderr, "Pipe Failed" );
  94. return 1;
  95. }
  96. //創建PIPE管道(WRITE)
  97. if (pipe(fd2)==-1)
  98. {
  99. fprintf(stderr, "Pipe Failed" );
  100. return 1;
  101. }
  102. PID = fork();
  103.  
  104. if (PID > 0) {
  105. close(connfd);
  106. close(fd1[1]);
  107. char times[256];
  108. char char_smaller[256];
  109. read(fd1[0], times, 256);
  110. int int_times = atoi(times);
  111. //進行各個子程序的答案最小比較
  112. if(smaller > int_times)
  113. smaller = int_times;
  114. close(fd2[0]);
  115. sprintf(char_smaller,"%d",smaller);
  116. //寫給各個子程序
  117. write(fd2[1], char_smaller, 256);
  118. }
  119. else{
  120. printf("子程序處理...\n");
  121. close(sockfd);
  122. //創建PIPE讓子程序根富程序溝通
  123. close(fd1[0]);
  124. close(fd2[1]);
  125. for(int t=0;t<1;t++){
  126. write(connfd, "I'm Lin Pig\n", 256);
  127. write(connfd, "Enter integer 0-100\n", 256);
  128. int a;
  129. //計算玩家猜測次數
  130. int player_times;
  131. char times[256];
  132. char char_smaller[256];
  133. //玩家次數初始化
  134. if(t == 0){
  135. player_times = 0;
  136. }
  137. srand(time(NULL));
  138. a=(rand()%100)+1;
  139. for(int i = 0;;i++){
  140. //讀取CLIENT傳來的答案
  141. read(connfd,buf,256);
  142. //進行確認
  143. if(buf[0] == a) {
  144. write(connfd, "Bingo\n", 256);
  145. sprintf(times,"%d",++i);
  146. //寫給父程序答案
  147. write(fd1[1], times, 256);
  148. //從父程序接收最小的值
  149. read(fd2[0], char_smaller, 256);
  150. strcat(char_smaller," times is the smaller(Winner)!!");
  151. write(connfd, char_smaller, 256);
  152. exit(0);
  153. break;
  154. }
  155. //答案太大
  156. else if(buf[0] > a){
  157. write(connfd, "Too Big,Enter again\n", 256);
  158. }
  159. //答案太小
  160. else if(buf[0] < a){
  161. write(connfd, "Too small,Enter again\n", 256);
  162. }
  163. }
  164. }
  165. exit(0);
  166. }
  167. }
  168. return 0;
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement