700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > 【多进程和多线程实现并发服务器】

【多进程和多线程实现并发服务器】

时间:2018-08-13 04:57:39

相关推荐

【多进程和多线程实现并发服务器】

多进程和多线程实现并发服务器

多进程实现多线程实现

多进程实现

#include <stdio.h>#include <sys/types.h>#include <sys/socket.h>#include <stdlib.h>#include <netinet/in.h>#include <netinet/ip.h>#include <arpa/inet.h>#include <unistd.h>#include <string.h>#include <signal.h>#include <sys/wait.h>#define PORT 6666#define IP "192.168.31.90"typedef void (*sighandler_t)(int);void hander(int sig);int deal_cli_msg(int acceptfd, struct sockaddr_in clientaddr);int main(int argc, const char *argv[]){sighandler_t s = signal(SIGCHLD, hander);if (SIG_ERR == s){perror("signal");return -1;}int sockfd = socket(AF_INET, SOCK_STREAM, 0);if (-1 == sockfd){perror("socket error");exit(-1);}//允许端口被重用int reuse = 1;if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0){perror("setsockopt");return -1;}//填充地址信息结构体struct sockaddr_in serveraddr;serveraddr.sin_family = AF_INET;serveraddr.sin_port = htons(PORT); //网络字节序1024-49151serveraddr.sin_addr.s_addr = inet_addr(IP);socklen_t serveraddr_len = sizeof(serveraddr);if (-1 == bind(sockfd, (struct sockaddr *)&serveraddr, serveraddr_len)){perror("bind error");exit(-1);}//监听状态if (-1 == listen(sockfd, 10)){perror("listen error");exit(-1);}//从队列函数头获取客户端信息struct sockaddr_in clientaddr;socklen_t clientaddr_len = sizeof(clientaddr);int acceptfd;pid_t pid;while (1){acceptfd = accept(sockfd, (struct sockaddr *)&clientaddr, &clientaddr_len);if (-1 == acceptfd){perror("accept error");exit(-1);}printf("[%s:%d]连接到服务器..\n", inet_ntoa(clientaddr.sin_addr), ntohs(clientaddr.sin_port));pid = fork();if (pid > 0){close(acceptfd);}else if (pid == 0){close(sockfd);deal_cli_msg(acceptfd, clientaddr);close(acceptfd);exit(0);}else{perror("fork");return -1;}}close(sockfd);return 0;}int deal_cli_msg(int acceptfd, struct sockaddr_in clientaddr){ssize_t res = 0;char buff[128] = "";while (1){bzero(buff, sizeof(buff));res = recv(acceptfd, buff, sizeof(buff), 0);if (res < 0){perror("recv");exit(-1);}else if (0 == res){printf("客户端[%s:%d]关闭\n", inet_ntoa(clientaddr.sin_addr), ntohs(clientaddr.sin_port));exit(-1);}printf("客户端[%s:%d]发来数据: %s\n", inet_ntoa(clientaddr.sin_addr), ntohs(clientaddr.sin_port), buff);strcpy(buff, "yes");if (send(acceptfd, buff, sizeof(buff), 0) < 0){perror("send error");return -1;}}return 0;}void hander(int sig){while (waitpid(-1, NULL, WNOHANG) > 0);}

多线程实现

#include <stdio.h>#include <sys/types.h>#include <sys/socket.h>#include <stdlib.h>#include <netinet/in.h>#include <netinet/ip.h>#include <arpa/inet.h>#include <unistd.h>#include <string.h>#include <sys/wait.h>#include <pthread.h>#define PORT 6666#define IP "192.168.31.90"struct msg{int acceptfd;struct sockaddr_in clientaddr;};int acceptfd;void *deal_cli_msg(void *arg){int sockfd=((struct msg*)arg)->acceptfd;struct sockaddr_in clientaddr=((struct msg*)arg)->clientaddr;ssize_t res = 0;char buff[128] = "";while (1){bzero(buff, sizeof(buff));res = recv(acceptfd, buff, sizeof(buff), 0);if (res < 0){perror("recv");exit(-1);}else if (0 == res){printf("客户端[%s:%d]关闭\n", inet_ntoa(clientaddr.sin_addr), ntohs(clientaddr.sin_port));break;;}printf("客户端[%s:%d]发来数据: %s\n", inet_ntoa(clientaddr.sin_addr), ntohs(clientaddr.sin_port), buff);strcpy(buff, "yes");if (send(acceptfd, buff, sizeof(buff), 0) < 0){perror("send error");return NULL;}}close(acceptfd);pthread_exit(NULL);}int main(int argc, const char *argv[]){int sockfd = socket(AF_INET, SOCK_STREAM, 0);if (-1 == sockfd){perror("socket error");exit(-1);}//允许端口被重用int reuse = 1;if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0){perror("setsockopt");return -1;}//填充地址信息结构体struct sockaddr_in serveraddr;serveraddr.sin_family = AF_INET;serveraddr.sin_port = htons(PORT); //网络字节序1024-49151serveraddr.sin_addr.s_addr = inet_addr(IP);socklen_t serveraddr_len = sizeof(serveraddr);if (-1 == bind(sockfd, (struct sockaddr *)&serveraddr, serveraddr_len)){perror("bind error");exit(-1);}//监听状态if (-1 == listen(sockfd, 10)){perror("listen error");exit(-1);}//从队列函数头获取客户端信息pthread_t tid;struct sockaddr_in clientaddr;socklen_t clientaddr_len=sizeof(clientaddr);struct msg info;while (1){acceptfd = accept(sockfd, (struct sockaddr *)&clientaddr, &clientaddr_len);if (-1 == acceptfd){perror("accept error");exit(-1);}printf("[%s:%d]连接到服务器..\n", inet_ntoa(clientaddr.sin_addr), ntohs(clientaddr.sin_port));info.acceptfd=acceptfd;info.clientaddr=clientaddr;if(pthread_create(&tid, NULL, deal_cli_msg, (void*)&info)<0){perror("create");return -1;}pthread_detach(tid);}close(sockfd);return 0;}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。