并发服务器模型
- 可以同事处理多个客户端的连接请求(accept)和交互请求(recv send)
- 创建多进程或者多线程来实现
- 父进程、主线程只负责连接(accept),子进程、分支线程只负责与客户端进行交互(recv send)
2 多进程并发服务器
#include <myhead.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <sys/wait.h>
#define ERR_MSG(msg) do{\
fprintf(stderr,"__%d__:",__LINE__);\
perror(msg);\
}while(0)
#define PORT 8888 //端口号
#define IP "192.168.1.5" //本机IP
int rec_snd_msg(int newfd,struct sockaddr_in cin);
//回收僵尸进程
void handler(int sig)
{
while(waitpid(-1,NULL,WNOHANG)>0);
}
int main(int argc, const char *argv[])
{
//捕获17)SIGCHLD信号,为信号注册新的处理函数
if(signal(17, handler) == SIG_ERR)
{
ERR_MSG("signal");
return -1;
}
//定义流式套接字
int sfd;
if((sfd=socket(AF_INET,SOCK_STREAM,0))<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函数绑定
struct sockaddr_in sin;
sin.sin_family =AF_INET;
sin.sin_port =htons(PORT); //端口号的网络字节序
sin.sin_addr.s_addr =inet_addr(IP); //本机IP
if(bind(sfd,(struct sockaddr*)&sin,sizeof(sin))<0)
{
ERR_MSG("bind");
return -1;
}
printf("bind success\n");
//将套接字设置为被动监听状态 listen
if(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);
pid_t cpid=-1;
while(1)
{
if((newfd=accept(sfd,(struct sockaddr*)&cin,&addrlen))<0)
{
ERR_MSG("accept");
return -1;
}
cpid=fork();
if(0==cpid)
{
close(sfd);
rec_snd_msg(newfd,cin);
exit(0);
}
close(newfd);
}
close(sfd);
return 0;
}
int rec_snd_msg(int newfd,struct sockaddr_in cin)
{
char buf[128]="";
ssize_t res=0;
while(1)
{
//清空字符串
bzero(buf,sizeof(buf));
//接收
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\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),newfd);
//发送
strcat(buf,"*_*");
if(send(newfd,buf,sizeof(buf),0)<0)
{
ERR_MSG("send");
return -1;
}
printf("send success\n");
}
close(newfd);
return 0;
}
![](https://i-blog.csdnimg.cn/blog_migrate/a1631db0f322465ff3b3c3b78374824d.png)
2 多线程并发服务器
#include <myhead.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#define ERR_MSG(msg) do{\
fprintf(stderr,"__%d__:",__LINE__);\
perror(msg);\
}while(0)
#define PORT 8888 //端口号,范围1024~49151
#define IP "192.168.1.5" //本机IP
//需要传入到分支的线程参数
struct Climsg{
int newfd;
struct sockaddr_in cin;
};
void* deal_cli_msg(void* arg);
int main(int argc, const char *argv[])
{
//创建流式套接字
int 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 ip
struct sockaddr_in sin;
sin.sin_family =AF_INET;
sin.sin_port =htons(PORT);
sin.sin_addr.s_addr=inet_addr(IP);
//绑定服务器到地址信息 --->必须绑定bind
if(bind(sfd,(struct sockaddr*)&sin,sizeof(sin))<0)
{
ERR_MSG("bind");
return -1;
}
printf("bind success\n");
//将套接字设置为被动监听状态听 listen
if(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(sfd,NULL,NULL);
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)
{
printf("create error\n");
return -1;
}
pthread_detach(tid);
}
//关闭文件描述符
close(newfd);
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);
}
![](https://i-blog.csdnimg.cn/blog_migrate/c6f23a6e1b0f0496bfacc92ac8a97f3a.png)