IO多路转接

来源:互联网 发布:网络推广课程 编辑:程序博客网 时间:2024/05/01 04:03
[objc] view plain copy
 print?
  1. #include <stdlib.h>  
  2. #include <stdio.h>  
  3. #include <errno.h>  
  4. #include <string.h>  
  5. #include <netdb.h>  
  6. #include <sys/types.h>  
  7. #include <netinet/in.h>  
  8. #include <sys/socket.h>  
  9. #include<unistd.h>  
  10. #include<arpa/inet.h>  
  11. #include<ctype.h>  
  12.   
  13. /* 宏定义端口号 */  
  14. #define portnumber 8000  
  15.   
  16. #define MAX_LINE 80  
  17.   
  18.   
  19.   
  20. int main(void)  
  21. {  
  22.     int  lfd;  
  23.     int cfd;  
  24.     int sfd;  
  25.     int rdy;  
  26.   
  27.     struct sockaddr_in sin;  
  28.     struct sockaddr_in cin;  
  29.   
  30.     int client[FD_SETSIZE];  /* 客户端连接的套接字描述符数组 */  
  31.   
  32.     int maxi;  
  33.     int maxfd;                        /* 最大连接数 */  
  34.   
  35.     fd_set rset;  
  36.     fd_set allset;  
  37.   
  38.     socklen_t addr_len;         /* 地址结构长度 */  
  39.   
  40.     char buffer[MAX_LINE];  
  41.   
  42.     int i;  
  43.     int n;  
  44.     int len;  
  45.     int opt = 1;   /* 套接字选项 */  
  46.   
  47.     char addr_p[20];  
  48.   
  49.   
  50.   
  51.     /* 对server_addr_in  结构进行赋值  */  
  52.     bzero(&sin,sizeof(struct sockaddr_in));   /* 先清零 */  
  53.     sin.sin_family=AF_INET;                 //  
  54.     sin.sin_addr.s_addr=htonl(INADDR_ANY);  //表示接受任何ip地址   将ip地址转换成网络字节序  
  55.     sin.sin_port=htons(portnumber);         //将端口号转换成网络字节序  
  56.   
  57.     /* 调用socket函数创建一个TCP协议套接口 */  
  58.     if((lfd=socket(AF_INET,SOCK_STREAM,0))==-1// AF_INET:IPV4;SOCK_STREAM:TCP  
  59.     {  
  60.         fprintf(stderr,"Socket error:%s\n\a",strerror(errno));  
  61.         exit(1);  
  62.     }  
  63.     printf("socket!\n");  
  64.   
  65.     /*设置套接字选项 使用默认选项*/  
  66.     setsockopt(lfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));  
  67.   
  68.     /* 调用bind函数 将serer_addr结构绑定到sockfd上  */  
  69.     if(bind(lfd,(struct sockaddr *)(&sin),sizeof(struct sockaddr))==-1)  
  70.     {  
  71.         fprintf(stderr,"Bind error:%s\n\a",strerror(errno));  
  72.         exit(1);  
  73.     }  
  74.     printf("bind!\n");  
  75.   
  76.     /* 开始监听端口   等待客户的请求 */  
  77.     if(listen(lfd,20)==-1)  
  78.     {  
  79.         fprintf(stderr,"Listen error:%s\n\a",strerror(errno));  
  80.         exit(1);  
  81.     }  
  82.     printf("listen!\n");  
  83.     printf("Accepting connections .......\n");  
  84.   
  85.     maxfd = lfd;                                /*对最大文件描述符进行初始化*/  
  86.     maxi = -1;  
  87.   
  88.     /*初始化客户端连接描述符集合*/  
  89.     for(i = 0;i < FD_SETSIZE;i++)  
  90.     {  
  91.         client[i] = -1;  
  92.     }  
  93.   
  94.     FD_ZERO(&allset);                     /* 清空文件描述符集合 */  
  95.     FD_SET(lfd,&allset);                    /* 将监听字设置在集合内 */  
  96.   
  97.   
  98.     /* 开始服务程序的死循环 */  
  99.     while(1)  
  100.     {  
  101.         rset = allset;  
  102.         printf("selecting!\n");  
  103.         /*得到当前可以读的文件描述符数*/  
  104.         rdy = select(maxfd + 1, &rset, NULLNULLNULL);  
  105.   
  106.         printf("selected!\n");  
  107.         if(FD_ISSET(lfd, &rset))  
  108.         {  
  109.   
  110.             addr_len = sizeof(sin);  
  111.             printf("accepting!\n");  
  112.             /* 接受客户端的请求 */  
  113.             if((cfd=accept(lfd,(struct sockaddr *)(&cin),&addr_len))==-1)  
  114.             {  
  115.                 fprintf(stderr,"Accept error:%s\n\a",strerror(errno));  
  116.                 exit(1);  
  117.             }  
  118.             printf("accepted!\n");  
  119.   
  120.   
  121.                      /*查找一个空闲位置*/  
  122.             for(i = 0; i<FD_SETSIZE; i++)  
  123.             {       //printf("%d\t",client[i]);  
  124.                 if(client[i] <= 0)  
  125.                 {  
  126.                     client[i] = cfd;   /* 将处理该客户端的连接套接字设置到该位置 */  
  127.                     break;  
  128.                 }  
  129.             }  
  130.   
  131.         /* 太多的客户端连接   服务器拒绝俄请求  跳出循环 */  
  132.             if(i == FD_SETSIZE)  
  133.             {  
  134.                 printf("too many clients");  
  135.                 exit(1);  
  136.             }  
  137.   
  138.             FD_SET(cfd, &allset);     /* 设置连接集合 */  
  139.   
  140.             if(cfd > maxfd)                  /* 新的连接描述符 */  
  141.             {  
  142.                 maxfd = cfd;  
  143.             }  
  144.   
  145.             if(i > maxi)  
  146.             {  
  147.                 maxi = i;  
  148.             }  
  149.   
  150.             if(--rdy <= 0)                /* 减少一个连接描述符 */  
  151.             {  
  152.                 continue;  
  153.             }  
  154.   
  155.         }  
  156.   
  157.         /* 对每一个连接描述符做处理 */  
  158.         for(i = 0;i< FD_SETSIZE;i++)  
  159.         {  
  160.             if((sfd = client[i]) < 0)  
  161.             {  
  162.                 continue;  
  163.             }  
  164.   
  165.             if(FD_ISSET(sfd, &rset))  
  166.             {  
  167.                 printf("reading!\n");  
  168.                 n = read(sfd,buffer,MAX_LINE);  
  169.                 printf("%s\n",buffer);  
  170.                 printf("read!\n");  
  171.                 if(n == 0)  
  172.                 {  
  173.                     printf("the other side has been closed. \n");  
  174.                     fflush(stdout);                                    /* 刷新 输出终端 */  
  175.                     close(sfd);  
  176.   
  177.                     FD_CLR(sfd, &allset);                        /*清空连接描述符数组*/  
  178.                     client[i] = -1;  
  179.                 }  
  180.   
  181.                 else  
  182.                 {  
  183.                     /* 将客户端地址转换成字符串 */  
  184.                     inet_ntop(AF_INET, &cin.sin_addr, addr_p, sizeof(addr_p));  
  185.                     addr_p[strlen(addr_p)] = '\0';  
  186.   
  187.                     /*打印客户端地址 和 端口号*/  
  188.                     printf("Client Ip is %s, port is %d\n",addr_p,ntohs(cin.sin_port));  
  189.   
  190.   
  191.                     /* 谐函数出错 */  
  192.                     if(n == 1)  
  193.                     {  
  194.                         exit(1);  
  195.                     }  
  196.                 }  
  197.   
  198.                 /*如果没有可以读的套接字   退出循环*/  
  199.                 if(--rdy <= 0)  
  200.                 {  
  201.                     break;  
  202.   
  203.   
  204.             }  
  205.         }  
  206.   
  207.     }  
  208.   
  209.     close(lfd);       /* 关闭链接套接字 */  
  210.     return 0;  
  211. }  

客户端

[objc] view plain copy
 print?
  1. #include <stdlib.h>   
  2. #include <stdio.h>   
  3. #include <errno.h>   
  4. #include <string.h>   
  5. #include <netdb.h>   
  6. #include <sys/types.h>   
  7. #include <netinet/in.h>   
  8. #include <sys/socket.h>   
  9.   
  10. #define portnumber 8000  
  11.   
  12. int main(int argc, charchar *argv[])   
  13. {   
  14.     int nbytes;  
  15.     int sockfd;   
  16.     char buffer[80];   
  17.     char buffer_2[80];  
  18.     struct sockaddr_in server_addr;   
  19.     struct hostent *host;   
  20.   
  21.     if(argc!=2)   
  22.     {   
  23.         fprintf(stderr,"Usage:%s hostname \a\n",argv[0]);   
  24.         exit(1);   
  25.     }   
  26.   
  27.     if((host=gethostbyname(argv[1]))==NULL)   
  28.     {   
  29.         fprintf(stderr,"Gethostname error\n");   
  30.         exit(1);   
  31.     }   
  32.   
  33.      /* 调用socket函数创建一个TCP协议套接口 */   
  34.     if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1// AF_INET:Internet;SOCK_STREAM:TCP  
  35.     {   
  36.         fprintf(stderr,"Socket Error:%s\a\n",strerror(errno));   
  37.         exit(1);   
  38.     }   
  39.   
  40.   
  41.     bzero(&server_addr,sizeof(server_addr)); //   
  42.     server_addr.sin_family=AF_INET;  
  43.     server_addr.sin_port=htons(portnumber);  
  44.     server_addr.sin_addr = *((struct in_addr *)host->h_addr);//?  
  45.   
  46.   
  47.     if(connect(sockfd,(struct sockaddr *)(&server_addr),sizeof(struct sockaddr))==-1)   
  48.     {   
  49.         fprintf(stderr,"Connect Error:%s\a\n",strerror(errno));   
  50.         exit(1);   
  51.     }   
  52.   
  53.     while(1){  
  54.   
  55.     printf("Please input char:\n");  
  56.   
  57.   
  58.     fgets(buffer,1024,stdin);   
  59.     write(sockfd,buffer,strlen(buffer));  
  60.        #if 1  
  61.     if((nbytes=read(sockfd,buffer_2,81))==-1)   
  62.         {   
  63.             fprintf(stderr,"Read Error:%s\n",strerror(errno));  
  64.             exit(1);   
  65.         }  
  66.   
  67.         buffer_2[nbytes]='\0';  
  68.         printf("Client received from Server %s\n",buffer_2);  
  69.         #endif  
  70.   
  71.   
  72.     }     
  73.     close(sockfd);  
  74.     exit(0);   
  75. }   
0 0
原创粉丝点击