select函数实现
#include <mystdio.h>
#define PORT 8888
#define IP "192.168.124.18"
int main(int argc, const char *argv[])
{
//创建流式套接字文件
int sfd = socket(AF_INET,SOCK_STREAM,0);//AF_INET IPv4 SOCK_STREAM 流式套接字
if(sfd < 0)
{
perror("socket");
return -1;
}
printf("socket seccuss sfd = %d __%d__\n",sfd,__LINE__);
//允许端口号被快速重复使用
int reuse = 1;
if(setsockopt(sfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse))<0)
{
perror("setsockopt");
return -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地址
//绑定服务器自身的地址信息
if(bind(sfd,(struct sockaddr*)&sin,sizeof(sin)) < 0)
{
perror("bind");
return -1;
}
printf("bind seccuss __%d__\n",__LINE__);
//将套接字设置为被动监听状态
if(listen(sfd,128) < 0)
{
perror("listen");
return -1;
}
printf("listen seccuss __%d__\n",__LINE__);
//创建读集合
fd_set readfds,tempfds;
//初始化读集合
FD_ZERO(&readfds);
FD_ZERO(&tempfds);
//要检测的文件描述符加入到读集合中
FD_SET(0,&readfds);
FD_SET(sfd,&readfds);
struct sockaddr_in cin; //存储客户端的信息
socklen_t addrlen = sizeof(cin);
int maxfd = sfd;
int s_res;
int newfd = -1;
char buf[128];
ssize_t res;
while(1)
{
tempfds = readfds;
//监测集合中的文件描述符是否准备就绪
s_res = select(maxfd+1,&tempfds,NULL,NULL,NULL);
if(s_res < 0)
{
perror("select");
return -1;
}
else if(0 == s_res)
{
printf("time out \n");
break;
}
//判断哪个文件描述符准备就绪,走对应的处理函数
for(int i=0;i<=maxfd;i++)
{
if(0 == FD_ISSET(i,&tempfds))
continue;
if(0 == i)
{
printf("键盘输入\n");
bzero(buf,sizeof(buf));
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1] = '\0';
printf("%s\n",buf);
}
else if(i == sfd)
{
printf("触发客户端连接\n");
newfd = accept(sfd,(struct sockaddr*)&cin,&addrlen);
if(newfd <0)
{
perror("accept");
return -1;
}
printf("[%s:%d] 客户端连接成功 newfd=%d\n",\
inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),newfd);
FD_SET(newfd,&readfds);
maxfd = newfd>maxfd ? newfd:maxfd;
}
else
{
printf("触发客户端交互事件\n");
bzero(buf,sizeof(buf));
res = recv(i,buf,sizeof(buf),0); //接收
if(res < 0)
{
perror("recv");
return -1;
}
else if(0 == res)
{
printf("[%s:%d] 客户端下线 newfd=%d __%d__\n",\
inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),i,__LINE__);
close(i);
FD_CLR(i,&readfds); //将newfd从readfds中剔除
for(;maxfd>0;maxfd--)
{
if(FD_ISSET(maxfd,&readfds))
break;
}
continue;
}
printf("[%s:%d] %s newfd=%d __%d__\n",\
inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),buf,i,__LINE__);
//发送
strcat(buf,"*_*");
if(send(i,buf,sizeof(buf),0)<0)
{
fprintf(stderr,"line:%d",__LINE__);
perror("send");
return -1;
}
printf("send seccuss\n");
}
}
}
close(sfd);
return 0;
}
poll函数实现
#include <mystdio.h>
#define CLI_PORT 6666
#define CLI_IP "192.168.124.18"
int main(int argc, const char *argv[])//argv[1] IP argv[2] 端口号
{
if(argc <3)
{
printf("请在命令传参端口号和IP地址\n");
return -1;
}
//创建流式套接字文件
int cfd = socket(AF_INET,SOCK_STREAM,0);
if(cfd < 0)
{
perror("socket");
return -1;
}
printf("socket seccuss cfd = %d __%d__\n",cfd,__LINE__);
//填充服务器的地址信息,给connet函数连接服务器时使用
struct sockaddr_in sin;
sin.sin_family = AF_INET; //AF_INET
sin.sin_port = htons(atoi(argv[1])); //服务器绑定的端口号
sin.sin_addr.s_addr = inet_addr(argv[2]); //服务器绑定的IP地址
//连接指定的服务器
if(connect(cfd,(struct sockaddr*)&sin,sizeof(sin))<0)
{
perror("connect");
return -1;
}
printf("connet server[%s:%s] seccuss __%d__\n",argv[2],argv[1],__LINE__);
struct pollfd fds[2];
fds[0].fd = 0;
fds[0].events = POLLIN;
fds[1].fd = cfd;
fds[1].events = POLLIN;
char buf[128]="";
ssize_t res;
int p_res;
while(1)
{
//poll,监测集合中是否有文件描述符准备就绪
p_res = poll(fds,2,-1);
if(p_res < 0)
{
perror("poll");
return -1;
}
else if(0 == p_res)
{
printf("time out\n");
break;
}
if((fds[0].revents & POLLIN) )
{
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1] = '\0';
//发送数据
res = send(cfd,buf,sizeof(buf),0);
if(res < 0)
{
perror("send");
return -1;
}
printf("send seccuss\n");
}
if(fds[1].revents & POLLIN)
{
bzero(buf,sizeof(buf));
res = recv(cfd,buf,sizeof(buf),0);
if(res < 0)
{
perror("recv");
return -1;
}
else if(0 == res)
{
printf("服务器下线 __%d__\n",__LINE__);
break;
}
printf("%s __%d__\n",buf,__LINE__);
}
}
close(cfd);
return 0;
}