多进程并发服务器是一种经典的服务器架构,它通过创建多个子进程来处理客户端连接,从而实现并发处理多个客户端请求的能力。
概念:
优点:
缺点:
综上所述,多进程并发服务器能够满足高并发处理需求,并具有稳定性和简单性等优点。然而,它也存在资源消耗较高和调试困难等缺点,根据实际需求和应用场景选择合适的服务器架构是很重要的。
案例
利用多进程,在TCP通信的基础上,实现服务器可以同时连接多个客户端。实现大小写转换。
service.c
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
void handler(int sig)
{
if (sig == SIGCHLD)
{
while(waitpid(0,NULL,WNOHANG)>0);
}
}
int main(int argc, char const *argv[])
{
//1.建立socket套接字
int socked = socket(AF_INET, SOCK_STREAM, 0);
if (socked < 0)
{
perror("socket is err");
return -1;
}
//2.bind绑定服务器ip和端口号
struct sockaddr_in saddr, caddr;
saddr.sin_family = AF_INET;
saddr.sin_port = htons(atoi(argv[1]));
saddr.sin_addr.s_addr = inet_addr("0.0.0.0");
int len = sizeof(caddr);
int rev = bind(socked, (struct sockaddr *)(&saddr), sizeof(saddr));
if (rev < 0)
{
perror("bind is err");
return 0;
}
//3.listen设置最大同时链接数量
rev = listen(socked, 32);
if (rev < 0)
{
perror("rev is err");
return 0;
}
//4.建立子进程负责服务器给客户端发送消息,并且主进程不结束,此进程不结束
char buf[1024] = {0};
int size=0;
while (1)
{
int acceptfd = accept(socked, (struct sockaddr *)(&caddr), &len);
if (acceptfd < 0)
{
perror("accept is err");
return 0;
}
pid_t pid = fork();
if (pid < 0)
{
perror("pid is err");
return 0;
}
else if (pid == 0)
{
//子进程,每个子进程维护一个客户端
//不许要监听,直接关掉
close(socked);
while (1)
{
int flage = recv(acceptfd, buf, sizeof(buf), 0);
if (flage < 0)
{
perror("recv is err");
}
else if (flage == 0)
{
printf("ip:%s is close\n", inet_ntoa(caddr.sin_addr));
close(acceptfd);
break;
}
else
{
size = strlen(buf);
for (int i = 0; i < size; ++i)
{
if (buf[i] >= 'a' && buf[i] <= 'z')
buf[i] = buf[i] + ('A' - 'a');
else
buf[i] = buf[i] + ('a' - 'A');
}
printf("%d %s\n",getpid(),buf);
send(acceptfd, buf, sizeof(buf), 0);
}
}
}
else
{
//主进程回收子线程资源
close(acceptfd);
//异步节约资源
signal(SIGCHLD, handler);
}
}
return 0;
}
client.c
#include
#include
#include
#include
#include
#include
#include
#include
#include
int main(int argc, char const *argv[])
{
//1.socket建立文件描述符
int fd = socket(AF_INET, SOCK_STREAM, 0);
if (fd < 0)
{
perror("socket is err");
}
//2.connect连接服务器
struct sockaddr_in saddr;
saddr.sin_family = AF_INET;
saddr.sin_port = htons(atoi(argv[2]));
saddr.sin_addr.s_addr = inet_addr(argv[1]);
int flage = connect(fd, (struct sockaddr *)(&saddr), sizeof(saddr));
if (flage < 0)
{
perror("connect is err");
}
//3.服务器端不断发送数据,接受服务器转化后的数据
char buf[1024] = {0};
while (1)
{
//memset(buf,0,sizeof(buf));
fgets(buf, sizeof(buf), stdin);
if (strncmp(buf,"quit#",5)==0)
{
break;
}
if (buf[strlen(buf) - 1] == '\n')
buf[strlen(buf) - 1] = '\0';
send(fd, buf, sizeof(buf), 0);
flage = recv(fd, buf, sizeof(buf), 0);
if (flage < 0)
{
perror("recv is err");
}
else
{
fprintf(stdout, "%s\n", buf);
}
}
close(fd);
return 0;
}
多线程并发服务器是一种服务器架构,通过创建多个线程来处理客户端连接,实现并发处理多个客户端请求的能力。下面是多线程并发服务器的概念和一些优点:
概念:
优点:
缺点:
综上所述,多线程并发服务器能够满足高并发处理需求,并具有资源效率、响应速度、简单性和可扩展性等优点。然而,它也存在数据同步问题和调试困难等缺点,根据实际需求和应用场景选择合适的服务器架构是很重要的。
案例
利用多线程,在TCP通信的基础上,实现服务器可以同时连接多个客户端。实现大小写转换。
service.c
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
struct client
{
int acceptfd;
struct sockaddr_in caddr;
};
void *my_pthread(void *p)
{
char buf[1024] = {0};
int size = 0;
struct client *cl = (struct client *)p;
int acceptfd = cl->acceptfd;
struct sockaddr_in caddr = cl->caddr;
while (1)
{
int flage = recv(acceptfd, buf, sizeof(buf), 0);
if (flage < 0)
{
perror("recv is err");
}
else if (flage == 0)
{
printf("ip:%s is close\n", inet_ntoa(caddr.sin_addr));
close(acceptfd);
break;
}
else
{
size = strlen(buf);
for (int i = 0; i < size; ++i)
{
if (buf[i] >= 'a' && buf[i] <= 'z')
buf[i] = buf[i] + ('A' - 'a');
else
buf[i] = buf[i] + ('a' - 'A');
}
send(acceptfd, buf, sizeof(buf), 0);
}
}
close(acceptfd);
return 0;
}
int main(int argc, char const *argv[])
{
//1.建立socket套接字
int socked = socket(AF_INET, SOCK_STREAM, 0);
if (socked < 0)
{
perror("socket is err");
return -1;
}
//2.bind绑定服务器ip和端口号
struct sockaddr_in saddr, caddr;
saddr.sin_family = AF_INET;
saddr.sin_port = htons(atoi(argv[1]));
saddr.sin_addr.s_addr = inet_addr("0.0.0.0");
int len = sizeof(caddr);
int rev = bind(socked, (struct sockaddr *)(&saddr), sizeof(saddr));
if (rev < 0)
{
perror("bind is err");
return 0;
}
//3.listen设置最大同时链接数量
rev = listen(socked, 32);
if (rev < 0)
{
perror("rev is err");
return 0;
}
//4.建立子进程负责服务器给客户端发送消息,并且主进程不结束,此进程不结束
int i = 0;
pthread_t tid;
struct client ti[1024] = {0};
while (1)
{
int acceptfd = accept(socked, (struct sockaddr *)(&caddr), &len);
if (acceptfd < 0)
{
perror("accept is err");
return 0;
}
ti[i].acceptfd = acceptfd;
ti[i].caddr = caddr;
pthread_create(&tid, NULL, my_pthread, (void *)&ti[i]);
pthread_detach(tid);
++i;
}
return 0;
}
client.c
#include
#include
#include
#include
#include
#include
#include
#include
#include
int main(int argc, char const *argv[])
{
//1.socket建立文件描述符
int fd = socket(AF_INET, SOCK_STREAM, 0);
if (fd < 0)
{
perror("socket is err");
}
//2.connect连接服务器
struct sockaddr_in saddr;
saddr.sin_family = AF_INET;
saddr.sin_port = htons(atoi(argv[2]));
saddr.sin_addr.s_addr = inet_addr(argv[1]);
int flage = connect(fd, (struct sockaddr *)(&saddr), sizeof(saddr));
if (flage < 0)
{
perror("connect is err");
}
//3.服务器端不断发送数据,接受服务器转化后的数据
char buf[1024] = {0};
while (1)
{
//memset(buf,0,sizeof(buf));
fgets(buf, sizeof(buf), stdin);
if (strncmp(buf,"quit#",5)==0)
{
break;
}
if (buf[strlen(buf) - 1] == '\n')
buf[strlen(buf) - 1] = '\0';
send(fd, buf, sizeof(buf), 0);
flage = recv(fd, buf, sizeof(buf), 0);
if (flage < 0)
{
perror("recv is err");
}
else
{
fprintf(stdout, "%s\n", buf);
}
}
close(fd);
return 0;
}