作业:
1. 多进程并发服务器根据流程图重新编写。(上交)
#include <myhead.h>
#define ERR_MSG(msg) do{\
fprintf(stderr,"__%d__",__LINE__);\
perror(msg);\
}while(0)
#define PORT 6666
#define IP "192.168.114.37"
//回收僵尸进程
void handler(int signum)
{
if(signum==17)
{
while(waitpid(-1,NULL,WNOHANG)>0);
}
}
//子进程与客户端交互
int deal_cli_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(res==0)
{
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," zyz");
if(send(newfd,buf,sizeof(buf),0)<0)
{
ERR_MSG("send");
return -1;
}
printf("send success\n");
}
close(newfd);
return 0;
}
int main(int argc, const char *argv[])
{
signal(17,handler);
//创建流式套接字
int sfd=socket(AF_INET,SOCK_STREAM,0);
if(sfd<0)
{
ERR_MSG("socket");
return -1;
}
struct sockaddr_in sin;
sin.sin_family=AF_INET;
sin.sin_port=htons(PORT);
sin.sin_addr.s_addr=inet_addr(IP);
if(bind(sfd,(struct sockaddr *)&sin,sizeof(sin))<0)
{
ERR_MSG("bind");
return -1;
}
printf("bind success\n");
if(listen(sfd,128)<0)
{
ERR_MSG("listen");
return -1;
}
struct sockaddr_in cin;
socklen_t addrlen=sizeof(cin);
pid_t cpid;
while(1)
{
int newfd=accept(sfd,(struct sockaddr*)&cin,&addrlen);
if(newfd<0)
{
ERR_MSG("accept");
return -1;
}
printf("[%s:%d]客户端连接成功newfd=%d\n",\
inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),newfd);
cpid=fork();
if(cpid==0)
{
//子进程
close(sfd);
//与客户端进行交互
deal_cli_msg(newfd,cin);
exit(0);//退出子进程
}
close(newfd);
}
close(sfd);//关闭文件描述符
return 0;
}
2. 多线程并发服务器重新写一遍。(上交)
#include <myhead.h>
#define ERR_MSG(msg) do{\
fprintf(stderr,"__%d__",__LINE__);\
perror(msg);\
}while(0)
#define PORT 6666
#define IP "192.168.114.37"
struct Climsg
{
int newfd;
struct sockaddr_in cin;
};
int deal_cli_msg(int newfd,struct sockaddr_in cin);
void *task1(void *arg)
{
int newfd=((struct Climsg*)arg)->newfd;
struct sockaddr_in cin=((struct Climsg*)arg)->cin;
deal_cli_msg(newfd,cin);
}
int main(int argc, const char *argv[])
{
//创建流式套接字 socket
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;//必须填AF_INET
sin.sin_port =htons(PORT);//端口号的网络字节序
sin.sin_addr.s_addr=inet_addr(IP);//本机ip
//绑定服务器的地址信息--->必须绑定 bind
if(bind(sfd,(struct sockaddr *)&sin,sizeof(sin)))
{
ERR_MSG("bind");
return -1;
}
printf("bind success\n");
//将套接字设置为被动监听状态 listen
if(listen(sfd,128)<0)//128是指定未完成连接队列容量,不要填0/1
{
ERR_MSG("listen");
return -1;
}
printf("listen success\n");
//获取一个已经完成的客户端信息,生成一个新的文件描述符 accept
int newfd=-1;
struct sockaddr_in cin;//存储客户端的地址信息
socklen_t addrlen=sizeof(cin);//真实的地址信息结构体的大小
pthread_t ctid;
struct Climsg info;
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);
//newfd=accept(sfd,&cin,NULL);
//能运行到此位置,代表客户端连接成功,子进程只负责交互
info.newfd=newfd;
info.cin=cin;
if(pthread_create(&ctid,NULL,task1,(void*)&info)!=0)
{
printf("创建子线程失败\n");
return -1;
}
//分离线程,分支线程退出后,由内核自动回收
pthread_detach(ctid);
}
//关闭文件描述符
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(res==0)
{
printf("[%s:%d]客户端下线 newfd=%d:%s\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),newfd,buf);
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);
pthread_exit(NULL);
return 0;
}
思维导图:https://mubu.com/app/edit/home/3grwxwSBWRD#m