1.select实现TCP服务器端多路复用
#include <myhead.h>
#define PORT 8888
#define IP "192.168.114.177"
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\n");
int reuse = 1;
if(setsockopt(sfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse)) ==-1){
perror("setsockopt error");
return -1;
}
printf("设置端口号快速重启成功\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);
fd_set readfds,tempfds;
FD_ZERO(&readfds);
FD_SET(sfd,&readfds);
FD_SET(0,&readfds);
char buf[128] = "";
int res = 0;
int newfd=-1;
int maxfd = sfd;
struct sockaddr_in saveCin[1024];
while(1){
tempfds=readfds;
res = select(maxfd+1,&tempfds,NULL,NULL,NULL);
if(res == -1){
perror("select error");
return -1;
}else if(res == 0){
printf("time out\n");
return -1;
}
for(int i=0;i <= maxfd;i++){
if(!FD_ISSET(i,&tempfds)){
continue;
}
if(i == sfd)
{
//4.2、阻塞接收客户端的链接请求,并且获取客户端的地址信息
newfd = accept(sfd, (struct sockaddr*)&cin, &socklen);
if(newfd == -1)
{
perror("accept error");
return -1;
}
printf("accept success\n");
FD_SET(newfd,&readfds);
if(maxfd<newfd){
maxfd = newfd;
}
saveCin[newfd] = cin;
}else if(i == 0){
char buf1[128] = "";
bzero(buf,sizeof(buf));
//从终端获取数据
fgets(buf, sizeof(buf), stdin); //从终端获取数据
buf[strlen(buf)-1]='\0';
printf("触发终端输入事件:%s\n", buf);
sprintf(buf1, "%s%s", "系统消息:", buf);
for(int j=4;j<=maxfd;j++){
send(j,buf1,sizeof(buf1),0);
}
}else{
bzero(buf,sizeof(buf));
int ret = recv(i,buf,sizeof(buf),0);
if(ret < 0){
perror("recv error");
return -1;
}else if(ret == 0){
printf("客户端已经下线\n");
close(i);
FD_CLR(i,&readfds);
for(int i=maxfd;i>=0;i++){
if(FD_ISSET(i,&readfds)){
maxfd = i;
break;
}
}
continue;
}
printf("[%s:%d]:%s\n",inet_ntoa(saveCin[i].sin_addr),ntohs(saveCin[i].sin_port),buf);
strcat(buf,"*_*");
send(i,buf,sizeof(buf),0);
}
}
}
close(sfd);
return 0;
}
2.poll实现TCP客户端多路复用
#include <myhead.h>
#define PORT 8888
#define IP "192.168.114.177"
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\n");
int reuse = 1;
if(setsockopt(sfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse)) ==-1){
perror("setsockopt error");
return -1;
}
printf("设置端口号快速重启成功\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);
fd_set readfds,tempfds;
FD_ZERO(&readfds);
FD_SET(sfd,&readfds);
FD_SET(0,&readfds);
char buf[128] = "";
int res = 0;
int newfd=-1;
int maxfd = sfd;
struct sockaddr_in saveCin[1024];
while(1){
tempfds=readfds;
res = select(maxfd+1,&tempfds,NULL,NULL,NULL);
if(res == -1){
perror("select error");
return -1;
}else if(res == 0){
printf("time out\n");
return -1;
}
for(int i=0;i <= maxfd;i++){
if(!FD_ISSET(i,&tempfds)){
continue;
}
if(i == sfd)
{
//4.2、阻塞接收客户端的链接请求,并且获取客户端的地址信息
newfd = accept(sfd, (struct sockaddr*)&cin, &socklen);
if(newfd == -1)
{
perror("accept error");
return -1;
}
printf("accept success\n");
FD_SET(newfd,&readfds);
if(maxfd<newfd){
maxfd = newfd;
}
saveCin[newfd] = cin;
}else if(i == 0){
char buf1[128] = "";
bzero(buf,sizeof(buf));
//从终端获取数据
fgets(buf, sizeof(buf), stdin); //从终端获取数据
buf[strlen(buf)-1]='\0';
printf("触发终端输入事件:%s\n", buf);
sprintf(buf1, "%s%s", "系统消息:", buf);
for(int j=4;j<=maxfd;j++){
send(j,buf1,sizeof(buf1),0);
}
}else{
bzero(buf,sizeof(buf));
int ret = recv(i,buf,sizeof(buf),0);
if(ret < 0){
perror("recv error");
return -1;
}else if(ret == 0){
printf("客户端已经下线\n");
close(i);
FD_CLR(i,&readfds);
for(int i=maxfd;i>=0;i++){
if(FD_ISSET(i,&readfds)){
maxfd = i;
break;
}
}
continue;
}
printf("[%s:%d]:%s\n",inet_ntoa(saveCin[i].sin_addr),ntohs(saveCin[i].sin_port),buf);
strcat(buf,"*_*");
send(i,buf,sizeof(buf),0);
}
}
}
close(sfd);
return 0;
}
3.select实现TCP客户端端多路复用
#include<myhead.h>
#define PORT 8888
#define IP "192.168.114.177"
int main(int argc, const char *argv[])
{
//创建套接字
int cfd=socket(AF_INET,SOCK_STREAM,0);
if(cfd == -1)
{
perror("socket 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(connect(cfd,(struct sockaddr*)&sin,sizeof(sin))==-1)
{
perror("connect error");
return -1;
}
//收发
char buf[128]="";
//定义一个用于检测的文件描述符集合
fd_set readfds,tempfds;
//清空
FD_ZERO(&readfds);
//设置集合内容
FD_SET(0,&readfds);
FD_SET(cfd,&readfds);
int res=0;
while(1)
{ tempfds=readfds;
//阻塞等待事件
res=select(cfd+1,&tempfds,NULL,NULL,NULL);
if(res==-1)
{
perror("select error");
return -1;
}
else if(res ==0 )
{
printf("timeout\n");
return -1;
}
//终端输入数据发送至服务器事件
if(FD_ISSET(0,&tempfds))
{
bzero(buf,sizeof(buf));
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1]='\0';
send(cfd,buf,sizeof(buf),0);
}
//从服务器接收数据事件
if(FD_ISSET(cfd,&tempfds))
{
bzero(buf,sizeof(buf));
recv(cfd,buf,sizeof(buf),0);
printf("接收数据为:%s\n",buf);
}
}
//关闭套接字
close(cfd);
return 0;
}