TCP并發服務器構建:
單循環服務器:服務端同一時刻只能處理單個客戶端的任務
并發服務器:服務端同一時刻能夠處理多個客戶端的任務
產生多個套接字可建立多個連接:
TCP服務端并發模型:
1:使用多進程
頭文件:
#ifndef __HEAD_H__
#define __HEAD_H__#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h> /* superset of previous */
#include <arpa/inet.h>
#include <pthread.h>#endif
代碼
#include "head.h"#define SER_PORT 50000
#define SER_IP "192.168.0.179"int init_tcp_ser()
{int sockfd = socket(AF_INET, SOCK_STREAM, 0);if (sockfd < 0){perror("socket error");return -1;}struct sockaddr_in seraddr;seraddr.sin_family = AF_INET;seraddr.sin_port = htons(SER_PORT);seraddr.sin_addr.s_addr = inet_addr(SER_IP);int ret = bind(sockfd, (struct sockaddr *)&seraddr, sizeof(seraddr));if (ret < 0){perror("bind error");return -1;}ret = listen(sockfd, 100);if (ret < 0){perror("listen error");return -1;}return sockfd;
}void wait_handler(int signo)
{wait(NULL);
}int main(int argc, const char *argv[])
{signal(SIGCHLD, wait_handler);struct sockaddr_in cliaddr;socklen_t clilen = sizeof(cliaddr);int sockfd = init_tcp_ser();if (sockfd < 0){return -1;}while (1){int connfd = accept(sockfd, (struct sockaddr *)&cliaddr, &clilen);if (connfd < 0){perror("accept error");return -1;}pid_t pid = fork();if (pid > 0){}else if (0 == pid){char buff[1024] = {0};while (1){memset(buff, 0, sizeof(buff));size_t cnt = recv(connfd, buff, sizeof(buff), 0);if (cnt < 0){perror("recv error");break;}else if (0 == cnt){printf("[%s : %d] : offline\n", inet_ntoa(cliaddr.sin_addr), ntohs(cliaddr.sin_port));break;}printf("[%s : %d] : %s\n", inet_ntoa(cliaddr.sin_addr), ntohs(cliaddr.sin_port), buff);strcat(buff, "---->ok");cnt = send(connfd, buff, strlen(buff), 0);if (cnt < 0){perror("send error");break;}}close(connfd);}}close(sockfd);return 0;
}
2:使用多線程
#include "head.h"#define SER_PORT 50000
#define SER_IP "192.168.0.179"int init_tcp_ser()
{int sockfd = socket(AF_INET, SOCK_STREAM, 0);if (sockfd < 0){perror("socket error");return -1;}//允許綁定處于TIME_WAIT狀態的地址,避免端口占用問題:int optval = 1;setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval));struct sockaddr_in seraddr;seraddr.sin_family = AF_INET;seraddr.sin_port = htons(SER_PORT);seraddr.sin_addr.s_addr = inet_addr(SER_IP);int ret = bind(sockfd, (struct sockaddr *)&seraddr, sizeof(seraddr));if (ret < 0){perror("bind error");return -1;}ret = listen(sockfd, 100);if (ret < 0){perror("listen error");return -1;}return sockfd;
}void *task(void *arg)
{char buff[1024] = {0};int connfd = *((int *)arg);while (1){memset(buff, 0, sizeof(buff));size_t cnt = recv(connfd, buff, sizeof(buff), 0);if (cnt < 0){perror("recv error");break;}else if (0 == cnt){printf("offline\n");break;}printf("%s\n", buff);strcat(buff, "---->ok");cnt = send(connfd, buff, strlen(buff), 0);if (cnt < 0){perror("send error");break;}}close(connfd);
}int main(int argc, const char *argv[])
{pthread_t tid;struct sockaddr_in cliaddr;socklen_t clilen = sizeof(cliaddr);int sockfd = init_tcp_ser();if (sockfd < 0){return -1;}while (1){int connfd = accept(sockfd, (struct sockaddr *)&cliaddr, &clilen);if (connfd < 0){perror("accept error");return -1;}pthread_create(&tid, NULL, task, &connfd);pthread_detach(tid);}close(sockfd);return 0;
}
線程池:
為了解決多線程或者多進程模型,在服務器運行過程,頻繁創建和銷毀線程(進程)帶來的時間消耗問題,基于生產者和消費者編程模型,以及任務隊列等,實現的一套多線程框架。
IO多路復用:
對多個文件描述符的讀寫操作可以復用一個進程。在不創建新的進程和線程的前提下,使用一個進程實現多個文件的讀寫的同時監測。三種方式:
linux內核監測事件:
1:select實現IO多路復用:
(1)創建文件描述符集合
(2)添加關注的文件描述符到集合
(3)使用select傳遞集合表給內核,內核檢測事件
(4) 當內核監測到時間時,應用層select將解除阻塞,
(5)并將獲得相關的事件結果做不同的任務處理

{mkfifo("./myfifo", 0664);int fd = open("./myfifo", O_WRONLY);if (fd < 0){perror("open fifo error");return -1;}while (1){write(fd, "hello world", 11);sleep(1);}close(fd);return 0;
}
#include "head.h"int main(int argc, const char *argv[])
{char buff[1024] = {0};mkfifo("./myfifo", 0664);int fifofd = open("./myfifo", O_RDONLY);if (fifofd < 0){perror("open fifo error");return -1;}//1 創建文件描述符集合表fd_set rdfds;fd_set rdfdstmp;//2. 清空文件描述符集合表FD_ZERO(&rdfds);//3. 添加關注的文件描述符到集合中FD_SET(0, &rdfds);int maxfd = 0;FD_SET(fifofd, &rdfds);maxfd = maxfd > fifofd ? maxfd : fifofd;while (1){rdfdstmp = rdfds;//4. 傳遞集合表給內核并等待返回到達事件的結果int cnt = select(maxfd+1, &rdfdstmp, NULL, NULL, NULL);if (cnt < 0){perror("select error");return -1;}if (FD_ISSET(0, &rdfdstmp)){fgets(buff, sizeof(buff), stdin); //0printf("STDIN : %s\n", buff);}if (FD_ISSET(fifofd, &rdfdstmp)){memset(buff, 0, sizeof(buff));read(fifofd, buff, sizeof(buff));printf("FIFO : %s\n", buff);}}close(fifofd);return 0;
}
IO多路復用
#include "head.h"#define SER_PORT 50000
#define SER_IP "192.168.0.179"int init_tcp_ser()
{int sockfd = socket(AF_INET, SOCK_STREAM, 0);if (sockfd < 0){perror("socket error");return -1;}struct sockaddr_in seraddr;seraddr.sin_family = AF_INET;seraddr.sin_port = htons(SER_PORT);seraddr.sin_addr.s_addr = inet_addr(SER_IP);int ret = bind(sockfd, (struct sockaddr *)&seraddr, sizeof(seraddr));if (ret < 0){perror("bind error");return -1;}ret = listen(sockfd, 100);if (ret < 0){perror("listen error");return -1;}return sockfd;
}int main(int argc, const char *argv[])
{char buff[1024] = {0};struct sockaddr_in cliaddr;socklen_t clilen = sizeof(cliaddr);int sockfd = init_tcp_ser();if (sockfd < 0){return -1;}//1. 創建文件描述符集合fd_set rdfds;fd_set rdfdstmp;FD_ZERO(&rdfds);//2. 添加關注的文件描述符到集合FD_SET(sockfd, &rdfds);int maxfd = sockfd;while (1){rdfdstmp = rdfds;//3. 傳遞集合到內核,并等待返回監測結果int cnt = select(maxfd+1, &rdfdstmp, NULL, NULL, NULL);if (cnt < 0){perror("select error");return -1;}//4. 是否有監聽套接字事件到達 ----》三次握手已完成,可以acceptif (FD_ISSET(sockfd, &rdfdstmp)){int connfd = accept(sockfd, (struct sockaddr *)&cliaddr, &clilen);if (connfd < 0){perror("accept error");return -1;}FD_SET(connfd, &rdfds);maxfd = maxfd > connfd ? maxfd : connfd;}//5. 是否有通訊套接字事件到達for (int i = sockfd+1; i <= maxfd; i++){if (FD_ISSET(i, &rdfdstmp)){memset(buff, 0, sizeof(buff));ssize_t cnt = recv(i, buff, sizeof(buff), 0);if (cnt < 0){perror("recv error");FD_CLR(i, &rdfds);close(i);continue;}else if (0 == cnt){FD_CLR(i, &rdfds);close(i);continue;}printf("%s\n", buff);strcat(buff, "--->ok");cnt = send(i, buff, strlen(buff), 0);if (cnt < 0){perror("send error");FD_CLR(i, &rdfds);close(i);continue;}}}}close(sockfd);return 0;
}
2:poll
3:epoll
epoll:
1:創建文件描述符結合
2:添加關注的文件描述符
3:epoll通知內核開始進行事件監測
4:epoll返回時,獲取到達事件的結果
5:
函數1:
函數2:
函數3:
selsect和epoll區別:EPOLL沒有文件描述符上限限制,selsect上限1024個,