Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include <errno.h>
- #include <string.h>
- #include <time.h>
- #include <signal.h>
- #include <sys/wait.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <arpa/inet.h>
- #include <netdb.h>
- static void sigchld_handler() {
- pid_t PID;
- int status;
- while (PID = waitpid(-1, &status, WNOHANG) > 0)
- printf("子程序 %d 結束.\n", PID);
- /* Re-install handler */
- signal(SIGCHLD, sigchld_handler);
- }
- int main(int argc, char **argv) {
- int z, len_inet;
- struct sockaddr_in adr_srvr, adr_clnt;
- int sockfd, connfd;
- pid_t PID;
- time_t clock;
- struct tm *tm;
- int len_time, i;
- int buf[256];
- char score[5][256]={"","","","",""};
- int tcpclient[5];
- int mesgpipe[2],countpipe[2];
- int count = 0;
- int fd1[2],fd2[2];
- int smaller = 100;
- signal(SIGCHLD, sigchld_handler);
- memset(&adr_srvr, 0, sizeof(adr_srvr));
- if (argc ==2)
- adr_srvr.sin_addr.s_addr = inet_addr(argv[1]);
- adr_srvr.sin_family = AF_INET;
- adr_srvr.sin_port = htons(9090);
- sockfd = socket(AF_INET, SOCK_STREAM, 0);
- if (sockfd == -1) {
- perror("socket error");
- exit(1);
- }
- z = bind(sockfd, (struct sockaddr *)&adr_srvr, sizeof(adr_srvr));
- if (z == -1) {
- perror("bind error");
- exit(1);
- }
- z = listen(sockfd, 10);
- if (z == -1) {
- perror("listen error");
- exit(1);
- }
- /*
- if (pipe(fd1)==-1)
- {
- fprintf(stderr, "Pipe Failed" );
- return 1;
- }
- if (pipe(fd2)==-1)
- {
- fprintf(stderr, "Pipe Failed" );
- return 1;
- }
- */
- while(1) {
- printf("a new TCP cilent in , server has deal %d clients now\n",count++);
- len_inet = sizeof(adr_clnt);
- connfd = accept(sockfd, (struct sockaddr *)&adr_clnt, &len_inet);
- tcpclient[count] = connfd;
- if (connfd == -1) {
- perror("connect error");
- exit(1);
- }
- //創建PIPE管道(READ)
- if (pipe(fd1)==-1)
- {
- fprintf(stderr, "Pipe Failed" );
- return 1;
- }
- //創建PIPE管道(WRITE)
- if (pipe(fd2)==-1)
- {
- fprintf(stderr, "Pipe Failed" );
- return 1;
- }
- PID = fork();
- if (PID > 0) {
- close(connfd);
- close(fd1[1]);
- char times[256];
- char char_smaller[256];
- read(fd1[0], times, 256);
- int int_times = atoi(times);
- //進行各個子程序的答案最小比較
- if(smaller > int_times)
- smaller = int_times;
- close(fd2[0]);
- sprintf(char_smaller,"%d",smaller);
- //寫給各個子程序
- write(fd2[1], char_smaller, 256);
- }
- else{
- printf("子程序處理...\n");
- close(sockfd);
- //創建PIPE讓子程序根富程序溝通
- close(fd1[0]);
- close(fd2[1]);
- for(int t=0;t<1;t++){
- write(connfd, "I'm Lin Pig\n", 256);
- write(connfd, "Enter integer 0-100\n", 256);
- int a;
- //計算玩家猜測次數
- int player_times;
- char times[256];
- char char_smaller[256];
- //玩家次數初始化
- if(t == 0){
- player_times = 0;
- }
- srand(time(NULL));
- a=(rand()%100)+1;
- for(int i = 0;;i++){
- //讀取CLIENT傳來的答案
- read(connfd,buf,256);
- //進行確認
- if(buf[0] == a) {
- write(connfd, "Bingo\n", 256);
- sprintf(times,"%d",++i);
- //寫給父程序答案
- write(fd1[1], times, 256);
- //從父程序接收最小的值
- read(fd2[0], char_smaller, 256);
- strcat(char_smaller," times is the smaller(Winner)!!");
- write(connfd, char_smaller, 256);
- exit(0);
- break;
- }
- //答案太大
- else if(buf[0] > a){
- write(connfd, "Too Big,Enter again\n", 256);
- }
- //答案太小
- else if(buf[0] < a){
- write(connfd, "Too small,Enter again\n", 256);
- }
- }
- }
- exit(0);
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement