您的位置:首页 > 其它

I/O多路转接之select

2017-06-30 16:39 183 查看
在I/O中,I/O需注意(1)是否有读事件发生

                                  (2)是否有写事件发生

                                (3)是否有异常事件 

进而写出如下代码

select_server.c

  1  #include<sys/socket.h>

  2 #include<stdio.h>

  3 #include<sys/types.h>

  4 #include<sys/socket.h>

  5 #include <netinet/in.h>

  6 #include <arpa/inet.h>

  7 #include<stdlib.h>

  8 #include<string.h>

  9 

 10 

 11 int fds_array[sizeof(fd_set)*8];

 12 static void* usage(const char* proc)

 13 {

 14     printf("usage:%s[local_ip] [local_port]\n",proc);

 15 }

 16 int starup(const char* ip,int port)

 17 {

 18     int  sock = socket(AF_INET,SOCK_STREAM,0);

 19     if(sock < 0)

 20     {

 21         perror("socket");

 22         return 1;

 23     }

 24 

 25     struct sockaddr_in local;

 26     local.sin_family = AF_INET;

 27     local.sin_port = htons(port);

 28     local.sin_addr.s_addr = inet_addr(ip);

  2 #include<stdio.h>

  3 #include<sys/types.h>

  4 #include<sys/socket.h>

  5 #include <netinet/in.h>

  6 #include <arpa/inet.h>

  7 #include<stdlib.h>

  8 #include<string.h>

  9 

 10 

 11 int fds_array[sizeof(fd_set)*8];

 12 static void* usage(const char* proc)

 13 {

 14     printf("usage:%s[local_ip] [local_port]\n",proc);

 15 }

 16 int starup(const char* ip,int port)

 17 {

 18     int  sock = socket(AF_INET,SOCK_STREAM,0);

 19     if(sock < 0)

 20     {

 21         perror("socket");

 22         return 1;

 23     }

 24 

 25     struct sockaddr_in local;

 26     local.sin_family = AF_INET;

 27     local.sin_port = htons(port);

 28     local.sin_addr.s_addr = inet_addr(ip);

 29 int b=bind(sock,(struct sockaddr*)&local,sizeof(local));

 if(b<0)

 31     {

 32         perror("bind");

 33         return 2;

 34     }

 35 

 36 

 37     if(listen(sock,10)<0)

 38     {

 39         perror("listen");

 40         return 3;

 41     }

 42     return sock;

 43 }

 44 

 45 int main(int argc, const char* argv[])

 46 {

 47     if(argc != 3)

 48     {

 49         usage(argv[0]);

 50         return 4;

 51     }

 52  int listen_sock = starup(argv[1],atoi(argv[2]));

 53     fd_set rfds;

 54     fd_set wfds;

 FD_ZERO(&rfds);

 56     FD_ZERO(&wfds);

 57     int i=0;

 58     int num = sizeof(fds_array)/sizeof(fds_array[0]);

 59     for(; i<num; ++i)//给数组的所有标志位均置为-1

 60     {

 61         fds_array[i] = -1;

 62     }

 63     fds_array[0] = listen_sock;

 64 while(1)

 65     {

 66         int maxfd = -1;

 67         for(i=0; i<num; i++)

 68         {

 69             if(fds_array[i]==-1)

 70             {

 71                 continue;

 72             }

 73             FD_SET(fds_array[i],&rfds);

 74             FD_SET(fds_array[i],&wfds);

 75             if(maxfd<fds_array[i])

 76             {

 77                 maxfd = fds_array[i];

 78             }

 79         }

 80  switch(select(maxfd+1,&rfds,&wfds,NULL,NULL))

 81         {

 82             case 0:

 83                 printf("timeout...\n");

 84                 break;

 85             case -1:

 86                 perror("select");

 87                 break;

 88             default:

 89                 {   ///at least one read event ready

 90                     for(i=0; i<num; i++)

 91                     {

 92                         struct sockaddr_in client;

 93                         socklen_t len = sizeof(client);

 94                         if(fds_array[i]<0)

 95                         {

 96                             continue;

 97                         }

 98  if(i==0 && FD_ISSET(listen_sock,&rfds))

 99                         {

100                             int new_sock = accept(listen_sock,\

101                                     (struct sockaddr*)&client,&len);

102                             if(new_sock<0)

103                             {

104                                 perror("accept");

105                                 continue;

106                             }

107                             printf("get a client [%s:%d]\n",inet_ntoa(client.sin_addr),ntohs(client.sin_port));

108                             int j=0;

109                             for(;j<num; j++)

110                             {

111                                 if(fds_array[j]<0)

112                                 {

113                                     break;

114                                 }

115                             }

116  if(j==num)

117                             {

118                                 printf("fd_set full\n");

119                                 close(new_sock);

120                             }

121                             else

122                             {

123                                 fds_array[j] = new_sock;

124                             }

125                         }

126                         else if(i!=0 &&( FD_ISSET(fds_array[i],&rfds)||(FD_ISSET(fds_array[i],&wfds))))

127                         {

128                             char buf[1024];

129                             if(FD_ISSET(fds_array[i],&rfds))

130                              {

131                                     ssize_t s= read(fds_array[i],buf,sizeof(buf)-1);

132                                 if(s>0)

133                                 {

134                                     buf[s]=0;

135                                     printf("client#%s\n",buf);

136 

137                                     if(FD_ISSET(fds_array[i],&wfds))

138                                     {

139                                         printf("please enter:");

140                                         fflush(stdout);

141                                         ssize_t r = read(0,buf,sizeof(buf)-1);

                                                                                 if(r>0)

143                                         {

144                                             buf[r] = 0;

145                                             ssize_t w = write(fds_array[i],buf,strlen(buf));

146                                             if(w>0)

147                                             {

148                                                 buf[w] = 0;

149                                                 printf("server echo:%s\n",buf);

150                                             }

151                                            else if(w==0)

152                                             {

153                                                 printf("no data enter\n");

154                                                 close(fds_array[i]);

155                                                 fds_array[i] = -1;

156                                             }

157                                             else

158                                             {

159                                                 printf("server error\n");

160                                                 close(fds_array[i]);

161                                                 fds_array[i] = -1;

162                                             }

163                                         }

164                                     }

165                                 }

166                                else if(s==0)

                                 {

168                                     printf("client is quit\n");

169                                     close(fds_array[i]);

170 

171                                     fds_array[i] = -1;

172                                 }

173                                 else

174                                 {

175                                     printf("client error\n");

176                                     close(fds_array[i]);

177 

178                                     fds_array[i] = -1;

179                                 }

180                             }

181 

182                         }

183                    else{

184 

185                         }

186                     }

187                 }

188             break;

189         }

190 

191     }

192 

193 }

 select_client.c

     1 #include<stdio.h>

  2 #include<stdlib.h>

  3 #include<unistd.h>

  4 #include<sys/types.h>

  5 #include<sys/socket.h>

  6 #include<arpa/inet.h>

  7 #include<netinet/in.h>

  8 #include<string.h>

  9 #include<sys/stat.h>

 10 #include<fcntl.h>

 11 

 12 

 13 static void usage(const char* proc)

 14 {

 15     printf("usage:%s [local_ip] [local_proc]\n",proc);

 16 }

 17 

 18 int main(int argc,char* argv[])

 19 {

 20     if(argc!=3){

 21         usage(argv[0]);

 22         exit(1);

 23     }

 24 

 25     int sock = socket(AF_INET,SOCK_STREAM,0);

 26 

 27     struct sockaddr_in server;

 28     server.sin_family = AF_INET;

 29     server.sin_port = htons(atoi(argv[2]));

 30     server.sin_addr.s_addr = inet_addr(argv[1]); 

  31 

 32     if(connect(sock,(struct sockaddr*)&server,sizeof(server))<0){

 33         perror("connect");

 34         exit(2);

 35     }

 36 

 37     char buf[1024];

 38     while(1){

 39         printf("please enter#");

 40         fflush(stdout);

 41 

 42         int sfd = dup(1);

 43         close(1);

 44         dup2(sock,1);

 45         ssize_t s = read(0,buf,sizeof(buf)-1);

 46         printf("%s",buf);

 47         dup2(sfd,STDOUT_FILENO);

 48 

 49         s = read(sock,buf,sizeof(buf)-1);

 50         buf[s] = 0;

 51         printf("server# %s",buf);

 52 }

 53 return 0;

 54 }   

运行解果:




                        
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: