首页 > 代码库 > select 函数 实现三个客户端异步通信

select 函数 实现三个客户端异步通信

1 //建立管道2 mkfifo 12 13 21 23 31 32

 

open 顺序:

 

cl1 读 , cl2 cl3 向 cl1写

 

cl2 读 , cl1 cl3 向 cl2写

 

cl3 读 , cl1 cl2 向 cl3写

 

 

 

cl1 代码:

 1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<string.h> 4 #include<unistd.h> 5 #include<sys/stat.h> 6 #include<sys/types.h> 7 #include<fcntl.h> 8  #include <sys/time.h> 9 #include<sys/select.h>10  #include <sys/select.h>11 12        /* According to earlier standards */13        #include <sys/time.h>14        #include <sys/types.h>15        #include <unistd.h>16 17 int main(int argc, char* argv[])//2118 {19 20     int fd21, fd31,fd12,fd13 ;21     fd21 = open("21", O_RDONLY);22     fd31 = open("31", O_RDONLY);23 24     fd12 = open("12",O_WRONLY);25 26     fd13 = open("13",O_WRONLY);27     printf("OK!\n");28 29 30     printf("OK!\n");31     fd_set read_sets ;32     fd_set write_sets ;33     int iret,iwrt ;34     char buf[1024] ;35     struct timeval tm ;36     while(1)37     {38 39         tm.tv_sec = 1 ;40         tm.tv_usec = 0 ;41         FD_ZERO(&read_sets);42         FD_ZERO(&write_sets);43 44         FD_SET(fd21, &read_sets);45         FD_SET(fd31, &read_sets);46         FD_SET( 0, &write_sets);47         //FD_SET(fd12, &write_sets);48         //FD_SET(fd13, &write_sets);49 50         iret = select(10, &read_sets, NULL, NULL, &tm);51         iwrt = select(10,&write_sets,NULL,NULL,&tm);52 53         //54         if(iret != 0)55         {56             printf("active: %d\n", iret);57 58             if(FD_ISSET(fd21, &read_sets))59             {60                 memset(buf, 0, 1024);61                 read(fd21, buf, 1023);62                 printf("from 2: %s\n", buf);63             }64             if(FD_ISSET(fd31, &read_sets))65             {66                 memset(buf, 0, 1024);67                 read(fd31, buf, 1023);68                 printf("from 3: %s\n", buf);69             }70         }71 72 73         // write74         if(iwrt != 0)75         {76             printf("active: %d\n", iwrt);77             if(FD_ISSET( 0 /*fd12*/, &write_sets))78             {79                 memset(buf, 0, 128);80                 read(0, buf, 127) ;81                 write(fd12, buf, strlen(buf));82                 write(fd13, buf, strlen(buf));83             }84             /*if(FD_ISSET(fd13, &write_sets))85             {86                 memset(buf, 0, 128);87                 read(0, buf, 127) ;88                 write(fd13, buf, strlen(buf));89             }*/90         }91 92     }93     return 0 ;94 }

 

 

 

 

cl2 代码:

 1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<string.h> 4 #include<unistd.h> 5 #include<sys/stat.h> 6 #include<sys/types.h> 7 #include<fcntl.h> 8 #include<sys/select.h> 9 int main(int argc, char* argv[])//2110 {11     int fd12, fd32,fd21,fd23 ;12     fd21 = open("21",O_WRONLY);13 14     fd12 = open("12", O_RDONLY);15     fd32 = open("32", O_RDONLY);16 17     fd23 = open("23",O_WRONLY);18 19 20     fd_set read_sets ,write_sets ;21     int iret ,iwrt;22     char buf[1024] ;23     struct timeval tm ;24     while(1)25     {26 27         tm.tv_sec = 1 ;28         tm.tv_usec = 0 ;29         FD_ZERO(&read_sets);30         FD_ZERO(&write_sets);31         FD_SET(fd12, &read_sets);32         FD_SET(fd32, &read_sets);33         FD_SET( 0, &write_sets);34         //FD_SET(fd21,&write_sets);35         //FD_SET(fd23,&write_sets);36 37         iret = select(10, &read_sets, NULL, NULL, &tm);38             iwrt = select(10,&write_sets,NULL,NULL,&tm);39 40         if(iret != 0)41         {42             printf("active: %d\n", iret);43 44             if(FD_ISSET(fd12, &read_sets))45             {46                 memset(buf, 0, 1024);47                 read(fd12, buf, 1023);48                 printf("from 1: %s\n", buf);49             }50             if(FD_ISSET(fd32, &read_sets))51             {52                 memset(buf, 0, 1024);53                 read(fd32, buf, 1023);54                 printf("from 3: %s\n", buf);55             }56         }57 58 59         // write60         if(iwrt != 0)61         {62             printf("active: %d\n", iwrt);63             if(FD_ISSET( 0 , &write_sets))64             {65                 memset(buf, 0, 128);66                 read(0, buf, 127) ;67                 write(fd21, buf, strlen(buf));68                 write(fd23, buf, strlen(buf));69             }70         /*    if(FD_ISSET(fd23, &write_sets))71             {72                 memset(buf, 0, 128);73                 read(0, buf, 127) ;74                 write(fd23, buf, strlen(buf));75             }*/76         }77 78     }79     return 0 ;80 }

 

 

 

cl3 代码:

 1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<string.h> 4 #include<unistd.h> 5 #include<sys/stat.h> 6 #include<sys/types.h> 7 #include<fcntl.h> 8 #include<sys/select.h> 9 int main(int argc, char* argv[])//3110 {11     int fd13, fd23,fd31,fd32 ;12     fd31 = open("31",O_WRONLY);13 14     fd32 = open("32",O_WRONLY);15 16     fd13 = open("13", O_RDONLY);17     fd23 = open("23", O_RDONLY);18 19     printf("OK!\n");20     fd_set read_sets ,write_sets ;21     int iret,iwrt ;22     char buf[1024] ;23     struct timeval tm ;24     while(1)25     {26 27         tm.tv_sec = 1 ;28         tm.tv_usec = 0 ;29         FD_ZERO(&read_sets);30         FD_ZERO(&write_sets);31         FD_SET(fd13, &read_sets);32         FD_SET(fd23, &read_sets);33         //FD_SET(fd31,&write_sets);34         //FD_SET(fd32,&write_sets);35         FD_SET( 0, &write_sets);36 37         iret = select(10, &read_sets, NULL, NULL, &tm);38         iwrt = select(10,&write_sets,NULL,NULL,&tm);39 40         //41     if(iret != 0)42         {43             printf("active: %d\n", iret);44 45             if(FD_ISSET(fd13, &read_sets))46             {47                 memset(buf, 0, 1024);48                 read(fd13, buf, 1023);49                 printf("from 1: %s\n", buf);50             }51             if(FD_ISSET(fd23, &read_sets))52             {53                 memset(buf, 0, 1024);54                 read(fd23, buf, 1023);55                 printf("from 2: %s\n", buf);56             }57         }58 59 60         // write61         if(iwrt != 0)62         {63             printf("active: %d\n", iwrt);64             if(FD_ISSET( 0 , &write_sets))65             {66                 memset(buf, 0, 128);67                 read(0, buf, 127) ;68                 write(fd31, buf, strlen(buf));69                  write(fd32, buf, strlen(buf));70             }71             /*if(FD_ISSET(fd32, &write_sets))72             {73                 memset(buf, 0, 128);74                 read(0, buf, 127) ;75                 write(fd32, buf, strlen(buf));76             }*/77         }78     }79 80     return 0 ;81 }

 

select 函数 实现三个客户端异步通信