1、多进程并发服务器根据流程图重新编写
#include <myhead.h>#define ERR_MSG(msg) do{\fprintf(stderr, "__%d__:", __LINE__); \perror(msg);\
}while(0)#define PORT 8888 //端口号,范围1024~49151
#define IP "192.168.114.85" //本机IP,ifconfigint deal_cli_msg(int newfd,struct sockaddr_in cin);//回收僵尸进程
void handler(int sig)
{while(waitpid(-1,NULL,WNOHANG) > 0);
}int main(int argc, const char *argv[])
{if(signal(17,handler) == SIG_ERR){ERR_MSG("signal");return -1;}
//创建流式套接字 socketint sfd = socket(AF_INET,SOCK_STREAM,0);if(sfd < 0){ERR_MSG("socket");return -1;}printf("socket create success sfd=%d\n",sfd);//允许端口快速的被复用int reuse = 1;if(setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0){ERR_MSG("setsockopt");return -1;}printf("允许端口快速的被复用成功\n");int newfd = -1;struct sockaddr_in cin;socklen_t addrlen = sizeof(cin);pid_t cpid = -1;while(1){//父进程只负责连接newfd = accept(sfd,(struct sockaddr*)&cin,&addrlen);if(newfd < 0){ERR_MSG("newfd");return -1;}printf("[%s;%d]客户端连接成功 newfd=%d\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),newfd);//能运行到当前位置,则代表有客户端连接成功;子进程只负责交互cpid = fork();if(0 == cpid){close(sfd);//子进程只负责与客户端交互deal_cli_msg(newfd,cin);exit(0);}close(newfd);}//关闭文件名描述符if(close(sfd) < 0){ERR_MSG("close");return -1;}return 0;
}int deal_cli_msg(int newfd, struct sockaddr_in cin)
{char buf[128] = "";ssize_t res = 0;while(1){//清空字符串bzero(buf,sizeof(buf));//memset//接收res = recv(newfd,buf,sizeof(buf),0);if(res < 0){ERR_MSG("recv");return -1;}else if(0 == res){printf("[%s;%d]客户端下载 newfd=%d\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),newfd);break;}printf("[%s;%d] newfd=%d ; %s\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),newfd,buf);//发送strcat(buf,"*_*");if(send(newfd,buf,sizeof(buf),0) < 0) {ERR_MSG("send");return -1;}printf("send success\n");}close(newfd);return 0;}
2、多线程并发服务器重新写一遍
#include<myhead.h>#define ERR_MSG(msg) do{\fprintf(stderr, "__%d__:", __LINE__); \perror(msg);\
}while(0)#define PORT 8888 //端口号,范围1024~49151
#define IP "192.168.114.85" //本机IP,ifconfig//需要传入到分支线程的参数
struct Climsg{int newfd;struct sockaddr_in cin;
};void* deal_cli_msg(void* arg); //void* arg = (void*)&infoint main(int argc, const char *argv[])
{//创建流式套接字 socketint sfd = socket(AF_INET, SOCK_STREAM, 0);if(sfd < 0){ERR_MSG("socket");return -1;}printf("socket create success sfd=%d\n", sfd);//允许端口快速的被复用int reuse = 1;if(setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0){ERR_MSG("setsockopt");return -1;}printf("允许端口快速的被复用成功\n");//填充地址信息结构体给bind函数绑定,//真实的地址信息结构体根据地址族指定 AF_INET:man 7 ipstruct sockaddr_in sin;sin.sin_family = AF_INET; //必须填AF_INET;sin.sin_port = htons(PORT); //端口号的网络字节序sin.sin_addr.s_addr = inet_addr(IP);//本机IP//绑定服务器的地址信息---> 必须绑定 bindif(bind(sfd, (struct sockaddr*)&sin, sizeof(sin)) < 0){ERR_MSG("bind");return -1;}printf("bind success\n");//将套接字设置为被动监听状态 listenif(listen(sfd, 128) < 0){ERR_MSG("listen");return -1;}printf("listen success\n");int newfd = -1;struct sockaddr_in cin; //存储客户端的地址信息socklen_t addrlen = sizeof(cin); //真实的地址信息结构体的大小pthread_t tid;struct Climsg info;while(1){//主线程只负责连接//获取一个已经完成的客户端信息,生成一个新的文件描述符 accept//accept函数在阻塞的同时,会先预选一个newfd文件描述符,//当解除阻塞的时候,若该预选的newfd没有被使用的时候,直接返回该预选的文件描述符;//当解除阻塞的时候,若该预选的newfd已经被使用了,则需要重新遍历一个没有被使用的newfd返回newfd = accept(sfd, (struct sockaddr*)&cin, &addrlen);if(newfd < 0){ERR_MSG("newfd");return -1;}printf("[%s:%d]客户端连接成功 newfd=%d\n", \inet_ntoa(cin.sin_addr), ntohs(cin.sin_port), newfd);//能运行到当前位置,则代表有客户端连接成功,此时需要一个分支线程用于与客户端交互//分支线程只负责交互info.newfd = newfd; info.cin = cin;if(pthread_create(&tid, NULL, deal_cli_msg, (void*)&info) != 0){fprintf(stderr, "pthread_create failed __%d__\n", __LINE__);return -1;}//分离线程,分支线程退出后,由内核自动回收pthread_detach(tid);}//关闭文件名描述符if(close(sfd) < 0){ERR_MSG("close");return -1;}return 0;
}//线程执行体函数
void* deal_cli_msg(void* arg) //void* arg = (void*)&info
{int newfd = ((struct Climsg*)arg)->newfd;struct sockaddr_in cin = ((struct Climsg*)arg)->cin;char buf[128] = "";ssize_t res = 0;while(1){//清空字符串bzero(buf, sizeof(buf)); //memset//接收res = recv(newfd, buf, sizeof(buf), 0);if(res < 0){ERR_MSG("recv");break;}else if(0 == res){printf("[%s:%d]客户端下线 newfd=%d\n", \inet_ntoa(cin.sin_addr), ntohs(cin.sin_port), newfd);break;}printf("[%s:%d] newfd=%d : %s\n", \inet_ntoa(cin.sin_addr), ntohs(cin.sin_port), newfd, buf);//发送strcat(buf, "*_*");if(send(newfd, buf, sizeof(buf), 0) < 0){ERR_MSG("send");break;}printf("send success\n");}close(newfd);pthread_exit(NULL);
}
3、思维导图