• Linux——网络编程二


    一、多路复用:

    基本常识:


       linux中每个进程默认情况下,最多可以打开1024个文件,最多有1024个文件描述符
       文件描述符的特点:  
         1.非负整数
      2.从最小可用的数字来分配
     3.每个进程启动时默认打开0,1,2三个文件描述符

    多路复用针对不止套接字fd,也针对普通的文件描述fd

    I\O模型

     阻塞IO模型

    sendto不阻塞 

    读阻塞

     写阻塞

     非阻塞模式IO

     

     轮询不需要阻塞,一般写成这样很欠揍👀👀👀👀👀

    非阻塞模型实现

     多路复用IO思想

    多路复用服务器模型:

     select()/poll()实现多路复用

     select参数

     函数select

     select宏

     TCP多路复用

     

     其它函数

     demo

     伪代码

    1. int main (void)
    2. {
    3. fd_set rset;
    4. int maxfd = -1;
    5. struct timeval tout;
    6. fd = socket ( ...);
    7. bind (fd, ...);
    8. listen (fd, ...);
    9. while (1) {
    10. maxfd = fd;
    11. FD_ZERO (&rset);
    12. FD_SET (fd, &rset);
    13. //依次把已经建立好连接fd加入到集合中,记录下来最大的文件描述符maxfd
    14. //...FIXME!!
    15. #if 0
    16. select (maxfd + 1, &rset, NULL, NULL, NULL);
    17. #else
    18. tout.tv_sec = 5;
    19. tout.tv_usec = 0;
    20. select (maxfd + 1, &rset, NULL, NULL, &tout);
    21. #endif
    22. if (FD_ISSET (fd, &rset)) {
    23. newfd = accept (fd, ....);
    24. }
    25. //依次判断已建立连接的客户端是否有数据
    26. //...FIXME!
    27. }
    28. return 0;
    29. }

    测试代码

    1. /*./client serv_ip serv_port */
    2. #include "net.h"
    3. void usage (char *s)
    4. {
    5. printf ("\n%s serv_ip serv_port", s);
    6. printf ("\n\t serv_ip: server ip address");
    7. printf ("\n\t serv_port: server port(>5000)\n\n");
    8. }
    9. int main (int argc, char **argv)
    10. {
    11. int fd = -1;
    12. int port = -1;
    13. struct sockaddr_in sin;
    14. if (argc != 3) {
    15. usage (argv[0]);
    16. exit (1);
    17. }
    18. /* 1. 创建socket fd */
    19. if ((fd = socket (AF_INET, SOCK_STREAM, 0)) < 0) {
    20. perror ("socket");
    21. exit (1);
    22. }
    23. port = atoi (argv[2]);
    24. if (port < 5000) {
    25. usage (argv[0]);
    26. exit (1);
    27. }
    28. /*2.连接服务器 */
    29. /*2.1 填充struct sockaddr_in结构体变量 */
    30. bzero (&sin, sizeof (sin));
    31. sin.sin_family = AF_INET;
    32. sin.sin_port = htons (port); //网络字节序的端口号
    33. #if 0
    34. sin.sin_addr.s_addr = inet_addr (SERV_IP_ADDR);
    35. #else
    36. if (inet_pton (AF_INET, argv[1], (void *) &sin.sin_addr) != 1) {
    37. perror ("inet_pton");
    38. exit (1);
    39. }
    40. #endif
    41. if (connect (fd, (struct sockaddr *) &sin, sizeof (sin)) < 0) {
    42. perror ("connect");
    43. exit (1);
    44. }
    45. printf ("Client staring...OK!\n");
    46. int ret = -1;
    47. fd_set rset;
    48. int maxfd = -1;
    49. struct timeval tout;
    50. char buf[BUFSIZ];
    51. while (1) {
    52. FD_ZERO (&rset);
    53. FD_SET (0, &rset);
    54. FD_SET (fd, &rset);
    55. maxfd = fd;
    56. tout.tv_sec = 5;
    57. tout.tv_usec = 0;
    58. select (maxfd + 1, &rset, NULL, NULL, &tout);
    59. if (FD_ISSET (0, &rset)) { //标准键盘上有输入
    60. //读取键盘输入,发送到网络套接字fd
    61. bzero (buf, BUFSIZ);
    62. do {
    63. ret = read (0, buf, BUFSIZ - 1);
    64. } while (ret < 0 && EINTR == errno);
    65. if (ret < 0) {
    66. perror ("read");
    67. continue;
    68. }
    69. if (!ret)
    70. continue;
    71. if (write (fd, buf, strlen (buf)) < 0) {
    72. perror ("write() to socket");
    73. continue;
    74. }
    75. if (!strncasecmp (buf, QUIT_STR, strlen (QUIT_STR))) { //用户输入了quit字符
    76. printf ("Client is exiting!\n");
    77. break;
    78. }
    79. }
    80. if (FD_ISSET (fd, &rset)) { //服务器给发送过来了数据
    81. //读取套接字数据,处理
    82. bzero (buf, BUFSIZ);
    83. do {
    84. ret = read (fd, buf, BUFSIZ - 1);
    85. } while (ret < 0 && EINTR == errno);
    86. if (ret < 0) {
    87. perror ("read from socket");
    88. continue;
    89. }
    90. if (!ret)
    91. break; /* 服务器关闭 */
    92. //There is a BUG,FIXME!!
    93. printf ("server said: %s\n", buf);
    94. if (!strncasecmp (buf, QUIT_STR, strlen (QUIT_STR))) { //用户输入了quit字符
    95. printf ("Sender Client is exiting!\n");
    96. break;
    97. }
    98. }
    99. }
    100. /*4.关闭套接字 */
    101. close (fd);
    102. }

    1. #ifndef __MAKEU_NET_H__
    2. #define __MAKEU_NET_H__
    3. #include
    4. #include
    5. #include
    6. #include
    7. #include
    8. #include
    9. #include /* See NOTES */
    10. #include
    11. #include
    12. #include /* superset of previous */
    13. #include
    14. #include
    15. //#include
    16. #include
    17. #define SERV_PORT 5002
    18. #define SERV_IP_ADDR "192.168.7.246"
    19. #define BACKLOG 5
    20. #define QUIT_STR "quit"
    21. #endif

    1. #include
    2. #include
    3. #include "net.h"
    4. void cli_data_handle (void *arg);
    5. void sig_child_handle(int signo)
    6. {
    7. if(SIGCHLD == signo) {
    8. waitpid(-1, NULL, WNOHANG);
    9. }
    10. }
    11. int main (void)
    12. {
    13. int fd = -1;
    14. struct sockaddr_in sin;
    15. signal(SIGCHLD, sig_child_handle);
    16. /* 1. 创建socket fd */
    17. if ((fd = socket (AF_INET, SOCK_STREAM, 0)) < 0) {
    18. perror ("socket");
    19. exit (1);
    20. }
    21. /*优化4: 允许绑定地址快速重用 */
    22. int b_reuse = 1;
    23. setsockopt (fd, SOL_SOCKET, SO_REUSEADDR, &b_reuse, sizeof (int));
    24. /*2. 绑定 */
    25. /*2.1 填充struct sockaddr_in结构体变量 */
    26. bzero (&sin, sizeof (sin));
    27. sin.sin_family = AF_INET;
    28. sin.sin_port = htons (SERV_PORT); //网络字节序的端口号
    29. /*优化1: 让服务器程序能绑定在任意的IP上 */
    30. #if 1
    31. sin.sin_addr.s_addr = htonl (INADDR_ANY);
    32. #else
    33. if (inet_pton (AF_INET, SERV_IP_ADDR, (void *) &sin.sin_addr) != 1) {
    34. perror ("inet_pton");
    35. exit (1);
    36. }
    37. #endif
    38. /*2.2 绑定 */
    39. if (bind (fd, (struct sockaddr *) &sin, sizeof (sin)) < 0) {
    40. perror ("bind");
    41. exit (1);
    42. }
    43. /*3. 调用listen()把主动套接字变成被动套接字 */
    44. if (listen (fd, BACKLOG) < 0) {
    45. perror ("listen");
    46. exit (1);
    47. }
    48. printf ("Server starting....OK!\n");
    49. int newfd = -1;
    50. /*4. 阻塞等待客户端连接请求 */
    51. struct sockaddr_in cin;
    52. socklen_t addrlen = sizeof (cin);
    53. while(1) {
    54. pid_t pid = -1;
    55. if ((newfd = accept (fd, (struct sockaddr *) &cin, &addrlen)) < 0) {
    56. perror ("accept");
    57. break;
    58. }
    59. /*创建一个子进程用于处理已建立连接的客户的交互数据*/
    60. if((pid = fork()) < 0) {
    61. perror("fork");
    62. break;
    63. }
    64. if(0 == pid) { //子进程中
    65. close(fd);
    66. char ipv4_addr[16];
    67. if (!inet_ntop (AF_INET, (void *) &cin.sin_addr, ipv4_addr, sizeof (cin))) {
    68. perror ("inet_ntop");
    69. exit (1);
    70. }
    71. printf ("Clinet(%s:%d) is connected!\n", ipv4_addr, ntohs(cin.sin_port));
    72. cli_data_handle(&newfd);
    73. return 0;
    74. } else { //实际上此处 pid >0, 父进程中
    75. close(newfd);
    76. }
    77. }
    78. close (fd);
    79. return 0;
    80. }
    81. void cli_data_handle (void *arg)
    82. {
    83. int newfd = *(int *) arg;
    84. printf ("Child handling process: newfd =%d\n", newfd);
    85. //..和newfd进行数据读写
    86. int ret = -1;
    87. char buf[BUFSIZ];
    88. char resp_buf[BUFSIZ+10];
    89. while (1) {
    90. bzero (buf, BUFSIZ);
    91. do {
    92. ret = read (newfd, buf, BUFSIZ - 1);
    93. } while (ret < 0 && EINTR == errno);
    94. if (ret < 0) {
    95. perror ("read");
    96. exit (1);
    97. }
    98. if (!ret) { //对方已经关闭
    99. break;
    100. }
    101. printf ("Receive data: %s\n", buf);
    102. if (!strncasecmp (buf, QUIT_STR, strlen (QUIT_STR))) { //用户输入了quit字符
    103. printf ("Client(fd=%d) is exiting!\n", newfd);
    104. break;
    105. }
    106. bzero(resp_buf, BUFSIZ+10);
    107. strncpy(resp_buf, SERV_RESP_STR, strlen(SERV_RESP_STR));
    108. strcat(resp_buf, buf);
    109. do {
    110. ret = write(newfd, resp_buf, strlen(resp_buf));
    111. }while(ret < 0 && EINTR == errno);
    112. }
    113. close (newfd);
    114. }
    1. 1 #Makefile
    2. 2 #CROSS_COMPILE = arm-linux-gnu-
    3. 3 #CC = $(CROSS_COMPILE)gcc
    4. 4 CC = gcc
    5. 5 DEBUG = -g -o0 -Wall
    6. 6 CFLAGS += $(DEBUG)
    7. 7 PROGS = ${patsubst %.c, %, ${wildcard *.c}}
    8. 8 % : %.c
    9. 9 $(CC) $(CFLAGS) $< -o $@ -lpthread
    10. 10 all : $(PROGS)
    11. 11

    这里出现了点问题老师的ubuntu和我的版本不同所以我还需要加两个头文件

    具体可看报错合集2

    然后有个错误有个宏不认识,man了半天,最后发现是自己定义的

    #define SERV_RESP_STR "SERVER:"

    1. #include
    2. #include
    3. #include
    4. #include
    5. #include
    6. #include
    7. #include
    8. #include
    9. #include
    10. typedef struct sockaddr Addr;
    11. typedef struct sockaddr_in Addr_in;
    12. typedef struct Node{
    13. int fd;
    14. struct Node *next;
    15. }Node;
    16. #define BACKLOG 5
    17. void Argment(int argc, char *argv[]);
    18. void NodeCreate(Node **p);
    19. void AcceptHandle(int sfd, Node **H);
    20. int ClientHandle(int fd);
    21. int main(int argc, char *argv[])
    22. {
    23. int ret, sfd, nfd = 0;
    24. Addr_in saddr;
    25. fd_set rfds;
    26. Node *H, *p = NULL;
    27. Argment(argc, argv);
    28. NodeCreate(&H);
    29. sfd = socket(AF_INET, SOCK_STREAM, 0);
    30. if(sfd < 0)
    31. {
    32. perror("socket");
    33. exit(0);
    34. }
    35. saddr.sin_family = AF_INET;
    36. saddr.sin_port = htons(atoi(argv[2]));
    37. saddr.sin_addr.s_addr = inet_addr(argv[1]);
    38. if(bind(sfd, (Addr *)&saddr, sizeof(Addr_in)))
    39. {
    40. perror("bind");
    41. exit(0);
    42. }
    43. if(listen(sfd, BACKLOG))
    44. {
    45. perror("listen");
    46. exit(0);
    47. }
    48. H->fd =sfd;
    49. while(1)
    50. {
    51. FD_ZERO(&rfds);
    52. p = H;
    53. nfd = 0;
    54. while(p != NULL)
    55. {
    56. if(p->fd > nfd)
    57. nfd = p->fd;
    58. FD_SET(p->fd, &rfds);
    59. p = p->next;
    60. }
    61. printf("nfd = %d run select...\n", nfd);
    62. ret =select(nfd+1, &rfds, NULL, NULL, NULL);
    63. if(!ret)
    64. continue;
    65. if(ret < 0)
    66. {
    67. perror("select");
    68. exit(0);
    69. }
    70. p = H;
    71. while(p->next != NULL)
    72. {
    73. if(FD_ISSET(p->fd, &rfds))
    74. {
    75. if(ClientHandle(p->fd) <= 0)
    76. {
    77. close(p->fd);
    78. Node *q = p->next;
    79. p->fd = q->fd;
    80. p->next = q->next;
    81. free(q);
    82. continue;
    83. }
    84. }
    85. p = p->next;
    86. }
    87. if(FD_ISSET(p->fd, &rfds))
    88. AcceptHandle(p->fd, &H);
    89. #if 1
    90. p = H;
    91. puts("");
    92. printf("Node:");
    93. while(p != NULL)
    94. {
    95. printf("%d ", p->fd);
    96. p = p->next;
    97. }
    98. #endif
    99. if(H->next == NULL)
    100. break;
    101. }
    102. close(sfd);
    103. free(H);
    104. return 0;
    105. }
    106. void Argment(int argc, char *argv[])//错误输出
    107. {
    108. if(argc != 3)
    109. {
    110. fprintf(stderr, "%s [addr] [port]\n", argv[0]);
    111. exit(0);
    112. }
    113. }
    114. void NodeCreate(Node **p)//创建节点
    115. {
    116. *p = malloc(sizeof(Node));
    117. if(p == NULL)
    118. {
    119. perror("malloc");
    120. exit(0);
    121. }
    122. bzero(*p, sizeof(Node));
    123. }
    124. void AcceptHandle(int sfd, Node **H)//Accept句柄
    125. {
    126. Node *p = NULL;
    127. Addr_in caddr;
    128. socklen_t caddr_len = sizeof(Addr_in);
    129. int cfd = accept(sfd, (Addr *)&caddr, &caddr_len);
    130. if(cfd < 0)
    131. {
    132. perror("accept");
    133. exit(0);
    134. }
    135. fprintf(stderr, "client %s:%d connect success.\n",
    136. inet_ntoa(caddr.sin_addr), ntohs(caddr.sin_port));
    137. NodeCreate(&p);
    138. p->fd = cfd;
    139. p->next = *H;
    140. *H = p;
    141. }
    142. int ClientHandle(int fd)//客户端句柄
    143. {
    144. int ret;
    145. char buf[1024] = {};
    146. ret = recv(fd, buf, 1024, 0);
    147. if(ret <= 0)
    148. return 0;
    149. printf("fd=%d buf = %s\n",fd, buf);
    150. if(buf[0] == '#')
    151. return 0;
    152. return ret;
    153. }

  • 相关阅读:
    NEON优化2:ARM优化高频指令总结
    企业财务数字化转型怎么才能落地?_光点科技
    Selenium+Python系列 - 开发环境搭建
    uni-app初步认识
    Redis 入门和数据类型讲解
    c语言tips-函数指针和指针函数
    升级node到指定版本
    [深入研究4G/5G/6G专题-54]: L3信令控制-3-软件功能与流程的切分-CU-UP网元的信令
    【Java】将Base64格式的图片等比伸缩至目标尺寸代码实现
    lvs+Keepalived高可用集群
  • 原文地址:https://blog.csdn.net/qq_52479948/article/details/127948144