Linux网络编程——tcp并发服务器(I/O复用之select)

https://blog.csdn.net/lianghe_work/article/details/46519633

多线程、多进程相比,I/O复用最大的优势是系统开销小,系统不需要建立新的进程或者线程,也不必维护这些线程和进程。


代码示例:

  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <stdlib.h>
  4. #include <errno.h>
  5. #include <string.h>
  6. #include <sys/socket.h>
  7. #include <sys/types.h>
  8. #include <netinet/in.h>
  9. #include <arpa/inet.h>
  10. #include <sys/select.h>
  11. #define SERV_PORT 8080
  12. #define LIST 20 //服务器最大接受连接
  13. #define MAX_FD 10 //FD_SET支持描述符数量
  14. int main(int argc, char *argv[])
  15. {
  16. int sockfd;
  17. int err;
  18. int i;
  19. int connfd;
  20. int fd_all[MAX_FD]; //保存所有描述符,用于select调用后,判断哪个可读
  21. //下面两个备份原因是select调用后,会发生变化,再次调用select前,需要重新赋值
  22. fd_set fd_read; //FD_SET数据备份
  23. fd_set fd_select; //用于select
  24. struct timeval timeout; //超时时间备份
  25. struct timeval timeout_select; //用于select
  26. struct sockaddr_in serv_addr; //服务器地址
  27. struct sockaddr_in cli_addr; //客户端地址
  28. socklen_t serv_len;
  29. socklen_t cli_len;
  30. //超时时间设置
  31. timeout.tv_sec = 10;
  32. timeout.tv_usec = 0;
  33. //创建TCP套接字
  34. sockfd = socket(AF_INET, SOCK_STREAM, 0);
  35. if(sockfd < 0)
  36. {
  37. perror( "fail to socket");
  38. exit( 1);
  39. }
  40. // 配置本地地址
  41. memset(&serv_addr, 0, sizeof(serv_addr));
  42. serv_addr.sin_family = AF_INET; // ipv4
  43. serv_addr.sin_port = htons(SERV_PORT); // 端口, 8080
  44. serv_addr.sin_addr.s_addr = htonl(INADDR_ANY); // ip
  45. serv_len = sizeof(serv_addr);
  46. // 绑定
  47. err = bind(sockfd, ( struct sockaddr *)&serv_addr, serv_len);
  48. if(err < 0)
  49. {
  50. perror( "fail to bind");
  51. exit( 1);
  52. }
  53. // 监听
  54. err = listen(sockfd, LIST);
  55. if(err < 0)
  56. {
  57. perror( "fail to listen");
  58. exit( 1);
  59. }
  60. //初始化fd_all数组
  61. memset(fd_all, -1, sizeof(fd_all));
  62. fd_all[ 0] = sockfd; //第一个为监听套接字
  63. FD_ZERO(&fd_read); // 清空
  64. FD_SET(sockfd, &fd_read); //将监听套接字加入fd_read
  65. int maxfd = fd_all[ 0]; //监听的最大套接字
  66. while( 1){
  67. // 每次都需要重新赋值,fd_select,timeout_select每次都会变
  68. fd_select = fd_read;
  69. timeout_select = timeout;
  70. // 检测监听套接字是否可读,没有可读,此函数会阻塞
  71. // 只要有客户连接,或断开连接,select()都会往下执行
  72. err = select(maxfd+ 1, &fd_select, NULL, NULL, NULL);
  73. //err = select(maxfd+1, &fd_select, NULL, NULL, (struct timeval *)&timeout_select);
  74. if(err < 0)
  75. {
  76. perror( "fail to select");
  77. exit( 1);
  78. }
  79. if(err == 0){
  80. printf( "timeout\n");
  81. }
  82. // 检测监听套接字是否可读
  83. if( FD_ISSET(sockfd, &fd_select) ){ //可读,证明有新客户端连接服务器
  84. cli_len = sizeof(cli_addr);
  85. // 取出已经完成的连接
  86. connfd = accept(sockfd, ( struct sockaddr *)&cli_addr, &cli_len);
  87. if(connfd < 0)
  88. {
  89. perror( "fail to accept");
  90. exit( 1);
  91. }
  92. // 打印客户端的 ip 和端口
  93. char cli_ip[INET_ADDRSTRLEN] = { 0};
  94. inet_ntop(AF_INET, &cli_addr.sin_addr, cli_ip, INET_ADDRSTRLEN);
  95. printf( "----------------------------------------------\n");
  96. printf( "client ip=%s,port=%d\n", cli_ip,ntohs(cli_addr.sin_port));
  97. // 将新连接套接字加入 fd_all 及 fd_read
  98. for(i= 0; i < MAX_FD; i++){
  99. if(fd_all[i] != -1){
  100. continue;
  101. } else{
  102. fd_all[i] = connfd;
  103. printf( "client fd_all[%d] join\n", i);
  104. break;
  105. }
  106. }
  107. FD_SET(connfd, &fd_read);
  108. if(maxfd < connfd)
  109. {
  110. maxfd = connfd; //更新maxfd
  111. }
  112. }
  113. //从1开始查看连接套接字是否可读,因为上面已经处理过0(sockfd)
  114. for(i= 1; i < maxfd; i++){
  115. if(FD_ISSET(fd_all[i], &fd_select)){
  116. printf( "fd_all[%d] is ok\n", i);
  117. char buf[ 1024]={ 0}; //读写缓冲区
  118. int num = read(fd_all[i], buf, 1024);
  119. if(num > 0){
  120. //收到 客户端数据并打印
  121. printf( "receive buf from client fd_all[%d] is: %s\n", i, buf);
  122. //回复客户端
  123. num = write(fd_all[i], buf, num);
  124. if(num < 0){
  125. perror( "fail to write ");
  126. exit( 1);
  127. } else{
  128. //printf("send reply\n");
  129. }
  130. }
  131. else if( 0 == num){ // 客户端断开时
  132. //客户端退出,关闭套接字,并从监听集合清除
  133. printf( "client:fd_all[%d] exit\n", i);
  134. FD_CLR(fd_all[i], &fd_read);
  135. close(fd_all[i]);
  136. fd_all[i] = -1;
  137. continue;
  138. }
  139. } else {
  140. //printf("no data\n");
  141. }
  142. }
  143. }
  144. return 0;
  145. }

运行结果:




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值