版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/ASJBFJSB/article/details/84633571
服务器采用单进程/线程编程,在同一时刻,服务器只能与一个客户端进行交互。只有与当前客户端的通信结束后,才能为下一个客户端进行服务。所以,如果采用线程,让主线程连接客户端,而函数线程为每个客户端进行服务,这样就可以保证服务器可以同时为多个客户端提供服务,实现并发。
采用多线程的优势
1.线程占用资源少。
2.从CPU的工作角度上看,线程的切换速度要比进程快。
3.资源共享,线程之间的通信更加简单。
4.编码实现相对简单。
代码:ser.c
#include <sys/scoket.h>
#include <arpa/inet>
#include <netinet/in.h>
#include <assert.h>
#Include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <signal.h>
#include <string.h>
#include <stdlib.h>
#include <pthread.h>
void* fun(void* arg){
int c = (int)arg;
char buff[128] = {0};
while(1){
int n = recv(c,buff,127,0);
if(n<=0){
printf("client quit!\n");
close(c);
break;
}
printf("%d : %s",c,buff);
send(c,"OK",2,0);
}
pthread_exit(NULL);
}
int main(){
int fd = socket(AF_INET,SOCK_STREAM,0);
assert(fd!=-1);
struct sockaddr_in cli,ser;
ser.sin_family = AF_INET;
ser.sin_port = htons(6000);
ser.sin_addr.s_addr = inet_addr("127.0.0.1");
int res = binf(fd,(struct sockaddr*)&ser,sizeof(ser));
assert(res!=-1);
res = listen(fd,5);
assert(res!=-1);
while(1){
socklen_t len = sizeof(cli);
int c = accept(fd,(struct sockaddr*)&cli,&len);
if(c==-1){
printf("link error!\n");
continue;
}
pthread_t id;
pthread_create(&id,NULL,fun,(void*)c);//值传递,不能地址传递
}
close(fd);
return 0;
}
执行结果:
查看线程数量:
需要注意的问题:
在向函数线程传递文件描述符的参数必须以值传递的形式进行传递,因为进程中的各个线程共享进程的PCB,如果以传地址或传引用的形式传输文件描述符,之前的正在被服务的客户端文件描述符可能被修改造成程序出错。
采用服务器端多线程编程的缺点:
1.要考虑线程安全问题。
2.线程之间不是相互独立的。
3.进程中的栈帧的大小是有限的,只能启动有限个数的线程。