分别通过select、多进程、多线程实现一个并发服务器
#include
#define IP "192.168.250.100"
#define PORT 8888
int deal_cli_msg(int newfd,struct sockaddr_in cin)
{
char buf[128] = "";
while(1)
{
bzero(buf,sizeof(buf));
int res = recv(newfd,buf,sizeof(buf),0);
if(res == 0)
{
printf("客户端已下线\n");
break;
}
else if(res < 0)
{
perror("recv error");
return -1;
}
strcat(buf,"->is read\n");
send(newfd,buf,sizeof(buf),0);
}
close(newfd);
return 0;
}
void handler(int signo)
{
if(signo == SIGCHLD)
{
while(waitpid(-1,NULL,WNOHANG) > 0);
}
}
int main(int argc, const char *argv[])
{
int sfd = socket(AF_INET,SOCK_STREAM,0);
if(sfd == -1)
{
perror("socket error");
return -1;
}
printf("socket success,sfd = %d\n",sfd);
int reuse = 1;
if(setsockopt(sfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse)) == -1)
{
perror("setsocket error");
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)) == -1)
{
perror("bind error");
return -1;
}
printf("bind success");
if(listen(sfd,128) == -1)
{
perror("listen error");
return -1;
}
printf("listen success");
struct sockaddr_in cin;
cin.sin_family = AF_INET;
socklen_t socklen = sizeof(cin);
pid_t pid;
if(signal(SIGCHLD,handler) == SIG_ERR)
{
perror("signal error");
return -1;
}
while(1)
{
int newfd = accept(sfd,(struct sockaddr*)&cin,&socklen);
if(newfd == -1)
{
perror("accept error");
return -1;
}
printf("accept success");
pid = fork();
if(pid > 0)
{
close(newfd);
}
else if(pid == 0)
{
close(sfd);
deal_cli_msg(newfd,cin);
exit(EXIT_SUCCESS);
}
else
{
perror("fork error");
return -1;
}
}
close(sfd);
return 0;
}
#include
#define PORT 8888
#define IP "192.168.250.100"
struct msg_info
{
int newfd;
struct sockaddr_in cin;
};
void *deal_cli_msg(void *arg)
{
int newfd = ((struct msg_info*)arg) -> newfd;
struct sockaddr_in cin = ((struct msg_info*)arg) -> cin;
char buf[128] = "";
while(1)
{
bzero(buf,sizeof(buf));
int res = recv(newfd,buf,sizeof(buf),0);
if(res == 0)
{
printf("客户端已下线\n");
break;
}
else if(res < 0)
{
perror("recv error");
return NULL;
}
strcat(buf,"->is read\n");
send(newfd,buf,sizeof(buf),0);
}
close(newfd);
pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
int sfd = socket(AF_INET,SOCK_STREAM,0);
if(sfd == -1)
{
perror("socket error");
return -1;
}
printf("socket success,sfd = %d\n",sfd);
int reuse = 1;
if(setsockopt(sfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse)) == -1)
{
perror("setsockopt error");
return -1;
}
printf("setsockopt success\n");
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)) == -1)
{
perror("bind error");
return -1;
}
printf("bind success\n");
if(listen(sfd,128) == -1)
{
perror("listen error");
return -1;
}
printf("listen success\n");
struct sockaddr_in cin;
cin.sin_family = AF_INET;
socklen_t socklen = sizeof(cin);
while(1)
{
int newfd = accept(sfd,(struct sockaddr*)&cin,&socklen);
if(newfd == -1)
{
perror("accept error");
return -1;
}
printf("accept success\n");
struct msg_info info = {newfd,cin};
pthread_t tid;
if(pthread_create(&tid,NULL,deal_cli_msg,&info) != 0)
{
printf("pthread_create error\n");
return -1;
}
if(pthread_detach(tid) != 0)
{
printf("pthread_detach error\n");
return -1;
}
}
close(sfd);
return 0;
}
#include
#define PORT 8888 //端口号
#define IP "192.168.250.100" //IP地址
//封装处理客户端信息函数
int deal_cli_msg(int newfd, struct sockaddr_in cin)
{
//5、收发数据使用newfd完成通信
char buf[128] = "";
while(1)
{
//清空字符串
bzero(buf, sizeof(buf));
//read(newfd, buf, sizeof(buf)); //从套接字中读取客户端发来的消息
int res = recv(newfd, buf, sizeof(buf), 0); //从套接字中读取客户端发来的消息
//buf[strlen(buf)-1] = '\0';
//判断收到的结果
if(res == 0)
{
printf("客户端已经下线\n");
break;
}else if(res < 0)
{
perror("recv error");
return -1;
}
printf("[%s:%d]:%s\n", inet_ntoa(cin.sin_addr), ntohs(cin.sin_port), buf);
//将读取的信息,加上一些字符发送回去
strcat(buf, " -> is read\n");
// write(newfd, buf, sizeof(buf));
send(newfd, buf, sizeof(buf), 0);
}
close(newfd); //关闭通信的套接字
}
int main(int argc, const char *argv[])
{
//1、创建用于接受连接的套接字
int sfd = socket(AF_INET, SOCK_STREAM, 0);
if(sfd == -1)
{
perror("socket error");
return -1;
}
printf("socket success sfd = %d\n", sfd); //4
//设置端口号快速重用
int reuse = 1;
if(setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) == -1)
{
perror("setsockopt error");
return -1;
}
printf("设置端口快速重用成功 _%d_ %s_ %s_\n", __LINE__, __FILE__, __func__);
//2、绑定IP地址和端口号
//2.1、填充要绑定的地址信息结构体
struct sockaddr_in sin;
sin.sin_family = AF_INET; //表明是ipv4
sin.sin_port = htons(PORT); //端口号
sin.sin_addr.s_addr = inet_addr(IP); //IP地址
//2.2、绑定
if(bind(sfd, (struct sockaddr*)&sin, sizeof(sin))==-1)
{
perror("bind error");
return -1;
}
printf("bind success _%d_ %s_ %s_\n", __LINE__, __FILE__, __func__);
//3、将套接字设置成被动监听状态
if(listen(sfd, 128) == -1)
{
perror("listen error");
return -1;
}
printf("listen success _%d_ %s_ %s_\n", __LINE__, __FILE__, __func__);
//4、阻塞等待客户端连接请求,如果有新的客户端连接,则创建一个新的用于通信的套接字
//4.1、定义客户端地址信息结构体
struct sockaddr_in cin; //客户端地址信息结构体
cin.sin_family = AF_INET;
socklen_t socklen = sizeof(cin); //客户端地址信息的大小
定义一个用于检测文件描述符的集合
fd_set readfds, tempfds; //在栈区定义
清空容器中的内容
FD_ZERO(&readfds);
将要检测的文件描述符放入集合中
FD_SET(sfd, &readfds); //将sfd文件描述符放入
FD_SET(0, &readfds); //将0号文件描述符放入
//定义一个容器
char buf[128] = "";
int res = 0; //接收select的返回值
while(1)
{
将集合内容复制一份
tempfds = readfds;
使用select阻塞等待集合中的文件描述符有事件产生
res = select(sfd+1, &tempfds, NULL, NULL, NULL);
if(res == -1)
{
perror("select error");
return -1;
}else if(res == 0)
{
printf("time out\n");
return -1;
}
判断sfd是否还在集合中
if(FD_ISSET(sfd, &tempfds))
{
//4.2、阻塞接收客户端的链接请求,并且获取客户端的地址信息
int newfd = accept(sfd, (struct sockaddr*)&cin, &socklen);
if(newfd == -1)
{
perror("accept error");
return -1;
}
printf("accept success _%d_ %s_ %s_\n", __LINE__, __FILE__, __func__);
deal_cli_msg(newfd,cin);
}
if(FD_ISSET(0, &tempfds))
{
//从终端获取数据
fgets(buf, sizeof(buf), stdin); //从终端获取数据
buf[strlen(buf)-1]='\0';
printf("触发终端输入事件:%s\n", buf);
}
}
//6、关闭所有套接字
close(sfd); //关闭监听
return 0;
}