1.使用seclect实现TCP客户端的多路复用
#include <head.h>
#define SERPORT 8888
#define SERIP "192.168.125.165"
#define PORT 6666
#define IP "192.168.125.165"
int main(int argc, const char *argv[])
{
//1.创建套接字
int cfd = -1;
if((cfd = socket(AF_INET,SOCK_STREAM,0)) == -1)
{
perror("socket error");
return -1;
}
printf("cfd = %d\n",cfd);
//设置端口号快速重用
int reuse = -1;
if(setsockopt(cfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse)) == -1)
{
perror("setsockopt error");
return -1;
}
//2.绑定地址信息结构体(非必须)
struct sockaddr_in cin;
cin.sin_family = AF_INET;
cin.sin_port = htons(PORT);
cin.sin_addr.s_addr = inet_addr(IP);
if(bind(cfd,(struct sockaddr*)&cin,sizeof(cin)) == -1)
{
perror("bind error");
return -1;
}
printf("bind success:_%d_%s_%s\n",__LINE__ , __FILE__ , __func__);
//3.连接地址
//定义服务器地址信息结构体
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(SERPORT);
sin.sin_addr.s_addr =inet_addr(SERIP);
if(connect(cfd,(struct sockaddr*)&sin,sizeof(sin)) == -1)
{
perror("connect error");
return -1;
}
printf("connect success:_%d_%s_%s\n",__LINE__ , __FILE__ , __func__);
//定义readfds集合存放文件描述符
fd_set readfds;
//清空集合
FD_ZERO(&readfds);
//把cfd,0加入集合
FD_SET(cfd,&readfds);
FD_SET(0,&readfds);
int res = 0; //用来接收select的返回值
fd_set tempfds; //定义一个临时集合
//4.收发数据
char wbuf[128] = "";
while(1)
{
tempfds = readfds; //让临时集合获取readfds值
res = select(cfd+1,&tempfds,NULL,NULL,NULL);
if(res < 0)
{
perror("select error");
return -1;
}else if(res == 0)
{
printf("time out\n");
return -1;
}
if(FD_ISSET(0,&tempfds))
{
bzero(wbuf,sizeof(wbuf));
fgets(wbuf,sizeof(wbuf),stdin);
wbuf[strlen(wbuf) -1] = 0;
if(strcmp(wbuf,"quit") == 0)
{
printf("退出\n");
break;
}
send(cfd,wbuf,sizeof(wbuf),0);
}
if(FD_ISSET(cfd,&tempfds))
{
bzero(wbuf,sizeof(wbuf));
res = recv(cfd,wbuf,sizeof(wbuf),0);
if(res == 0)
{
printf("服务器下线\n");
break;
}
printf("[%s:%d]:%s\n",SERIP,SERPORT,wbuf);
}
}
//5.关闭套接字
close(cfd);
return 0;
}
2.使用poll函数实现TCP服务器端的多路复用
#include <head.h>
#define PORT 8888
#define IP "192.168.125.85"
int main(int argc, const char *argv[])
{
//1.创建套接字
int sfd = -1;
if((sfd = socket(AF_INET,SOCK_STREAM,0)) == -1)
{
perror("socket error");
return -1;
}
printf("sfd = %d\n",sfd);
//端口号快速重用
int reuse = -1;
if((setsockopt(sfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse))) == -1)
{
perror("setsockopt error");
return -1;
}
//2.绑定地址到套接字
//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.2bind绑定地址
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__);
int len = 4; //用于计数,有多少个文件描述符
//定义pollfd类型的结构体数组,作为容器
struct pollfd fds[len];
//将0号和sfd文件描述符放入集合中
fds[0].fd = 0;
fds[0].events = POLLIN;
fds[3].fd = sfd;
fds[3].events = POLLIN;
//4.接收连接请求并返回新的套接字通信文件描述符
int newfd = -1;
struct sockaddr_in cin;
socklen_t addrlen = sizeof(cin);
int maxfd = sfd; //定义一个最大值存放最大文件描述符,主要作为下表
int res = 0; //用于接收select返回的值
char rbuf[128] = ""; //定义一个容器存放读取数据
struct sockaddr_in arr_cin[1024];
while(1)
{
//使用poll函数进行阻塞
int res = poll(fds,len,-1);
if(res < 0)
{
perror("poll error");
return -1;
}else if(res == 0)
{
printf("time out\n");
return -1;
}
//服务器端触发事件
if(fds[0].revents == POLLIN)
{
bzero(rbuf,sizeof(rbuf));
fgets(rbuf,sizeof(rbuf),stdin);
rbuf[strlen(rbuf) -1]=0;
for(int j=4;j<=maxfd&&fds[j].fd!=-1;j++ )
{
send(j,rbuf,sizeof(rbuf),0);
}
}
//客户端连接请求,sfd触发事件
if(fds[sfd].revents == POLLIN)
{
if((newfd = accept(sfd,(struct sockaddr*)&cin,&addrlen)) == -1)
{
perror("accept error");
return -1;
}
arr_cin[newfd] = cin;
printf("[%s:%d]:连接成功,newfd = %d\n",inet_ntoa(arr_cin[newfd].sin_addr),ntohs(arr_cin[newfd].sin_port),newfd);
//将新的文件描述符放入集合
fds[newfd].fd = newfd;
fds[newfd].events = POLLIN;
//更新maxfd值
if(newfd > maxfd)
{
maxfd = newfd;
len++;
}
}
for(int index = 4;index<=maxfd&&fds[index]!=-1;index++)
{
if(fds[index].revents == POLLIN)
//收发数据
{
bzero(rbuf,sizeof(rbuf));
res = recv(index,rbuf,sizeof(rbuf),0);
if(res == 0)
{
printf("客户端下线\n");
close(index);
//从集合中移除
fds[index].fd = -1;
}
printf("[%s:%d]:%s\n",inet_ntoa(arr_cin[index].sin_addr),ntohs(arr_cin[index].sin_port),rbuf);
strcat(rbuf,"*_*");
send(index,rbuf,sizeof(rbuf),0);
}
}
}
//6.关闭文件描述符
close(sfd);
return 0;
}