作业:多进程多线程并发服务器实现一遍提交。
1.多进程
服务器
#include <myhead.h>
#define IP "192.168.124.40"
#define PORT 9999
void *fun(void *fd)
{
int newfd=*(int *)fd;
char buff[1024];
while(1)
{
int res=recv(newfd,buff,sizeof(buff),0);
if(res==0)
{
printf("客户端下线\n");
break;
}
printf("%s\n",buff);
strcat(buff,"回家跨年");
send(newfd,buff,sizeof(buff),0);
}
pthread_exit(NULL);//子线程退出
}
int main(int argc, const char *argv[])
{
//1.创建套接字
int oldfd=socket(AF_INET,SOCK_STREAM,0);
if(oldfd==-1)
{
perror("socket");
return -1;
}
//端口号快速复用
int k=987;
if(setsockopt(oldfd,SOL_SOCKET,SO_REUSEADDR,&k,sizeof(k))==-1)
{
perror("setsockopt");
return -1;
}
printf("端口号复用成功\n");
//2.绑定服务器
struct sockaddr_in server={
.sin_family=AF_INET,
.sin_port=htons(PORT),
.sin_addr.s_addr=inet_addr(IP)
};
if(bind(oldfd,(struct sockadr *)&server,sizeof(server))==-1)
{
perror("bind");
return -1;
}
//3.服务器监听
if(listen(oldfd,20)==-1)//oldfd描述符最大可监听20个客户端的请求
{
perror("listen");
return -1;
}
//4.连接新的客户端
struct sockaddr_in client;
int client_len=sizeof(client);
while(1)
{
int newfd=accept(oldfd,(struct sockaddr *)&client,&client_len)
{
perror("accept");
return -1;
}
printf("连接客户端的信息:%s:%d",inet_ntoa(client.sin_addr),ntohs(client.sin_port));
//5.创建子父进程
pid_t pid=fork();
char buff[1024];
if(pid>0)//父进程
{
if(signal(SIGCHLD,handle)==SIG_ERR)//绑定子进程退出新信号
{
perror("signal");
return -1;
}
close(newfd);//父进程关闭新的描述符
}
else if(pid==0)
{
//子进程关闭旧描述符,收发信息
close(oldfd);
while(1)
{
int res=recv(newfd,buff,sizeof(buff),0);//收客户端的消息
if(res==0)
{
printf("客户端已退出\n");
break;
}
printf("%s\n",buff);//输出收到的消息
strcat(buff,"枪出如龙");
send(newfd,buff,sizeof(buff),0);//向连接的客户端发送消息
}
exit(EXIT_SUCCESS);//子进程退出
}
else
{
perror("fork");
return -1;
}
}
close(oldfd);
return 0;
}
客户端
#include <myhead.h>
#define IP "192.168.124.40"
#define SERPORT 9999
int main(int argc, const char *argv[])
{
//1、创建套接字
//2、绑定(不是必须绑定)
//3、连接
//4、收发消息
int oldfd = socket(AF_INET,SOCK_STREAM,0);
if(oldfd==-1)
{
perror("socket");
return -1;
}
#if 0
//绑定固定的IP和端口号(不是必须的)
struct sockaddr_in client = {
.sin_family =AF_INET,
.sin_port = htons(7899),//自定义端口号
.sin_addr.s_addr = inet_addr("192.168.124.34")
};
if(bind(oldfd,(struct sockaddr *)&client,sizeof(client))==-1)
{
perror("bind");
return -1;
}
#endif
//连接服务器
struct sockaddr_in server = {
.sin_family =AF_INET,
.sin_port = htons(SERPORT),//注意端口号需要服务器端口
.sin_addr.s_addr = inet_addr(IP)
};
if(connect(oldfd,(struct sockaddr *)&server,sizeof(server))==-1)
{
perror("connect");
return -1;
}
//收发消息
char buff[1024];
while(1)
{
fgets(buff,sizeof(buff),stdin);
buff[strlen(buff)-1] = '\0';
send(oldfd,buff,sizeof(buff),0);
if(strcmp(buff,"quit")==0)//退出客户端
{
break;
}
bzero(buff,sizeof(buff));
recv(oldfd,buff,sizeof(buff),0);//阻塞接收服务器消息
printf("服务器发来消息:%s\n",buff);
}
close(oldfd);
return 0;
}
2.多线程
服务器
#include <myhead.h>
#define IP "192.168.124.34"
#define PORT 8888
void *fun(void *fd)
{
int newfd = *(int *)fd;
char buff[1024];
while(1)
{
int res = recv(newfd,buff,sizeof(buff),0);
if(res==0)
{
printf("客户端下线\n");
break;
}
printf("%s\n",buff);
strcat(buff,"5点放学");
send(newfd,buff,sizeof(buff),0);
}
pthread_exit(NULL);//子线程退出
}
int main(int argc, const char *argv[])
{
//创建套接字
int oldfd = socket(AF_INET,SOCK_STREAM,0);
if(oldfd==-1)
{
perror("socket");
return -1;
}
//绑定
struct sockaddr_in server = {
.sin_family =AF_INET,
.sin_port = htons(PORT),
.sin_addr.s_addr =inet_addr(IP)
};
if(bind(oldfd,(struct sockaddr *)&server,sizeof(server))==-1)
{
perror("bind");
return -1;
}
//监听
if(listen(oldfd,20)==-1)
{
perror("listen");
return -1;
}
struct sockaddr_in client;
int client_len = sizeof(client);
int newfd;
pthread_t tid;
while(1)
{
//接收新客户端连入请求
newfd = accept(oldfd,(struct sockaddr *)&client,&client_len);
if(newfd==-1)
{
perror("accept");
return -1;
}
//创建子线程与客户端通话
if(pthread_create(&tid,NULL,fun,&newfd)==-1)
{
perror("pthread_create");
return -1;
}
}
pthread_join(tid,NULL);//回收子线程资源
close(newfd);
close(oldfd);
return 0;
}
客户端
#include <myhead.h>
#define IP "192.168.124.34"
#define SERPORT 8888
int main(int argc, const char *argv[])
{
//1、创建套接字
//2、绑定(不是必须绑定)
//3、连接
//4、收发消息
int oldfd = socket(AF_INET,SOCK_STREAM,0);
if(oldfd==-1)
{
perror("socket");
return -1;
}
#if 0
//绑定固定的IP和端口号(不是必须的)
struct sockaddr_in client = {
.sin_family =AF_INET,
.sin_port = htons(7899),//自定义端口号
.sin_addr.s_addr = inet_addr("192.168.124.34")
};
if(bind(oldfd,(struct sockaddr *)&client,sizeof(client))==-1)
{
perror("bind");
return -1;
}
#endif
//连接服务器
struct sockaddr_in server = {
.sin_family =AF_INET,
.sin_port = htons(SERPORT),//注意端口号需要服务器端口
.sin_addr.s_addr = inet_addr(IP)
};
if(connect(oldfd,(struct sockaddr *)&server,sizeof(server))==-1)
{
perror("connect");
return -1;
}
//收发消息
char buff[1024];
while(1)
{
fgets(buff,sizeof(buff),stdin);
buff[strlen(buff)-1] = '\0';
send(oldfd,buff,strlen(buff),0);
if(strcmp(buff,"quit")==0)//退出客户端
{
break;
}
bzero(buff,sizeof(buff));
recv(oldfd,buff,sizeof(buff),0);//阻塞接收服务器消息
printf("服务器发来消息:%s\n",buff);
}
return 0;
}