之前做了一个单线程的服务器,显然这样的服务器是没什么用途的,所以今天就做一个基于进程的并发服务器与基于线程的并发服务器
基于进程
优点:
进程之间相对独立,有自己的PCB,不会影响其他进程的工作,客户端之间的工作也就不会互相干扰
缺点:
优点有时候就是缺点,由于进程间独立,进程之间的通信(同步)就变得相当困难,必须使用显式的通信机制
进程对服务器的资源占有也较大,同样进程的转换需要陷入内核,时间较慢
注意事项:
由于子进程会完全复制父进程的文件表,所以必须小心的关闭各自的文件描述符
父进程关闭送给子进程的accept返回的描述符,子进程关闭父进程的监听描述符
父进程保留监听描述符才能收到其他客户端的连接请求
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
int echo(int connfd){
char *exit = "exit is getted";
write(connfd, "Please input", 100);
while(1){
char client_word[100];
read(connfd, client_word, 100);
printf("%s \n", client_word);
strcat(client_word, " is getted");
write(connfd, client_word, 100);
if(strcmp(client_word, exit) == 0){
printf("client exit\n");
return 1;
}else{
continue;
}
}
}
int main(int argc, char **argv){
int port, listenfd, connfd, clientlen;
struct sockaddr_in clientadder;
struct hostent *hp;
port = atoi(argv[1]);
if((listenfd = socket(AF_INET, SOCK_STREAM, 0)) < 0){
printf( "socket error\n" );
}
clientadder.sin_family = AF_INET;
clientadder.sin_addr.s_addr = htonl(INADDR_ANY);
clientadder.sin_port = htons(port);
if(bind(listenfd, (struct sockaddr *)&clientadder, sizeof(clientadder)) < 0){
printf( "bind error\n" );
}
if (listen(listenfd, 10) < 0){
printf( "listen error\n" );
}
while(1){
printf( "wait for client\n" );
clientlen = sizeof(clientadder);
if((connfd = accept(listenfd, (struct sockaddr *)&clientadder, &clientlen)) < 0){//父进程创建连接描述符
printf( "accept error\n" );
}else{
if(fork() == 0){
close(listenfd);//子进程关闭来自父进程的监听描述符
printf("connect successful!\n");
if (echo(connfd)){
printf("exit from echo\n");
}else{
printf("error from echo\n");
}
write(connfd, "exit", 100);
close(connfd);//关闭父进程送进来的连接描述符
exit(0);
}
close(connfd);//父进程关闭已被子进程复制的连接描述符
}
}
printf("gg\n");
}
基于线程
优点:
线程占用资源少,相互共享资源,通信同步简单
缺点:
必须小心线程间的相互影响,有可能B线程会修改A线程的内存空间,造成奇怪的错误
同时,线程间极其容易造成竞争,必须小心地写出线程安全的代码,或者利用PV控制危险的空间
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <pthread.h>
int echo(int connfd){
char *exit = "exit is getted";
write(connfd, "Please input", 100);
while(1){
char client_word[100];
read(connfd, client_word, 100);
printf("%s \n", client_word);
strcat(client_word, " is getted");
write(connfd, client_word, 100);
if(strcmp(client_word, exit) == 0){
printf("client exit\n");
return 1;
}else{
continue;
}
}
}
void *thread(void *vargp){//线程例程
printf("connect successful!\n");
int connfd2 = *((int *)vargp);
pthread_detach(pthread_self());
//printf("connfd2 = %d \n",connfd2);
if (echo(connfd2)){
printf("exit from echo\n");
}else{
printf("error from echo\n");
}
write(connfd2, "exit", 100);
free((int *)vargp);
close(connfd2);
return NULL;
}
int main(int argc, char **argv){
int port, listenfd, connfd, clientlen;
struct sockaddr_in clientadder;
struct hostent *hp;
port = atoi(argv[1]);
if((listenfd = socket(AF_INET, SOCK_STREAM, 0)) < 0){
printf( "socket error\n" );
}
clientadder.sin_family = AF_INET;
clientadder.sin_addr.s_addr = htonl(INADDR_ANY);
clientadder.sin_port = htons(port);
if(bind(listenfd, (struct sockaddr *)&clientadder, sizeof(clientadder)) < 0){
printf( "bind error\n" );
}
if (listen(listenfd, 10) < 0){
printf( "listen error\n" );
}
while(1){
printf( "wait for client\n" );
clientlen = sizeof(clientadder);
int *connfd = malloc(sizeof(int));//由于线程间容易产生竞争,所以必须给每个线程的连接描述符分配独立的内存空间
if((*connfd = accept(listenfd, (struct sockaddr *)&clientadder, &clientlen)) < 0){
printf( "accept error\n" );
}else{
pthread_t this_id;
printf("connfd = %d \n",*connfd);
pthread_create(&this_id, NULL, thread, connfd);//创建一个线程,跳向线程例程
}
}
printf("gg\n");
}
客户端程序与之前那篇单线程的服务器完全相同,这也就体现了CS模式优越性,修改服务器时,客户端受影响不大