select服务器
#include
#define IP "192.168.115.92"
#define PORT 8888
int main(int argc, const char *argv[])
{
//1.创建套接字文件
int sfd;
sfd=socket(AF_INET,SOCK_STREAM,0);
if(sfd==-1)
{
perror("socket");
return -1;
}
printf("create success\n");
//设置段口号快速重用
int reuse =1;
if(setsockopt(sfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse))==-1)
{
perror("setsockopt");
return -1;
}
//2.绑定
//2.1把ip地址和端口号写入
struct sockaddr_in sockaddr;
sockaddr.sin_family=AF_INET;
sockaddr.sin_port=htons(PORT);
sockaddr.sin_addr.s_addr=inet_addr(IP);
if(bind(sfd,(struct sockaddr*)&sockaddr,sizeof(sockaddr))==-1)
{
perror("bind");
return -1;
}
printf("bind success\n");
//3.监听
if(listen(sfd,128)==-1)
{
perror("listen");
return -1;
}
申请空间把文件描述符放进去
fd_set readfds,tempfds;
int maxfd=sfd;
FD_ZERO(&readfds);
FD_ZERO(&tempfds);
FD_SET(0,&readfds);
FD_SET(sfd,&readfds);
//4.等待链接
struct sockaddr_in cin;
cin.sin_family=AF_INET;
socklen_t len=sizeof(cin);
int newfd;
char buf[128]="";
int num;
char buf1[128]="";
struct sockaddr_in savein[1024];
//循环等待
while(1)
{
//阻塞等待一个文件描述符相应
tempfds=readfds;
num=select(maxfd+1,&tempfds,NULL,NULL,NULL);
if(num<0)
{
perror("select");
return -1;
}
else if(num==0)
{
printf("超时\n");
return -1;
}
//执行被唤醒的文件描述符的工作
for(int i=0;i<=maxfd;i++)
{
if(FD_ISSET(i,&tempfds)==0)
{
continue;
}
//阻塞等待连接用于通信
if(3==i)
{
newfd=accept(sfd,(struct sockaddr *)&cin,&len);
if(newfd==-1)
{
perror("accept");
return -1;
}
printf("accept success\n");
FD_SET(newfd,&readfds);
if(newfd>maxfd)
{
maxfd=newfd;
}
//把客户信息存到结构体里
savein[newfd]=cin;
}
//终端输入
else if(0==i)
{
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1]=0;
printf("buf=%s\n",buf);
for(int i=4;i<=maxfd;i++)
{
send(i,buf,sizeof(buf),0);
}
}
//通信
else
{
bzero(buf,sizeof(buf));
int rev;
rev=recv(i,buf,sizeof(buf),0);
if(rev<0)
{
perror("rev");
return -1;
}
else if(rev==0)
{
printf("客户端关闭\n");
FD_CLR(i,&readfds);
close(i);
for(int j=maxfd;j>=0;j--)
{
if(FD_ISSET(j,&readfds))
{
maxfd=j;
break;
}
}
continue;
}
printf("[%s:%d]:%s\n",inet_ntoa(savein[i].sin_addr),ntohs(savein[i].sin_port),buf);
}
}
}
close(sfd);
return 0;
}
select 客户端
#include
#define IP "192.168.115.92"
#define PORT 8888
int main(int argc, const char *argv[])
{
//创建套接字
int cfd;
cfd=socket(AF_INET,SOCK_STREAM,0);
if(cfd==-1)
{
perror("socket");
return -1;
}
printf("create success\n");
//连接
struct sockaddr_in sin;
sin.sin_family=AF_INET;
sin.sin_port=htons(8888);
sin.sin_addr.s_addr=inet_addr("192.168.115.92");
if(connect(cfd,(struct sockaddr*)&sin,sizeof(sin))==-1)
{
perror("connect");
return -1;
}
printf("connect success\n");
//通信
fd_set readfds,tempfds;
FD_ZERO(&readfds);
FD_ZERO(&tempfds);
FD_SET(0,&readfds);
FD_SET(cfd,&readfds);
char buf[128]="";
while(1)
{
tempfds=readfds;
int sel=select(cfd+1,&tempfds,NULL,NULL,NULL);
if(sel<0)
{
perror("select");
return -1;
}
else if(sel==0)
{
printf("超时\n");
return -1;
}
if(FD_ISSET(0,&tempfds))
{
bzero(buf,sizeof(buf));
printf("请输入:");
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1]=0;
send(cfd,buf,strlen(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;
}
poll服务端
#include
#define IP "192.168.115.92"
#define PORT 8888
int main(int argc, const char *argv[])
{
//1.创建套接字文件
int sfd;
sfd=socket(AF_INET,SOCK_STREAM,0);
if(sfd==-1)
{
perror("socket");
return -1;
}
printf("create success\n");
//设置段口号快速重用
int reuse =1;
if(setsockopt(sfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse))==-1)
{
perror("setsockopt");
return -1;
}
//2.绑定
//2.1把ip地址和端口号写入
struct sockaddr_in sockaddr;
sockaddr.sin_family=AF_INET;
sockaddr.sin_port=htons(PORT);
sockaddr.sin_addr.s_addr=inet_addr(IP);
if(bind(sfd,(struct sockaddr*)&sockaddr,sizeof(sockaddr))==-1)
{
perror("bind");
return -1;
}
printf("bind success\n");
//3.监听
if(listen(sfd,128)==-1)
{
perror("listen");
return -1;
}
申请空间把文件描述符放进去
struct pollfd pp[1024];
pp[0].fd=0;
pp[0].events=POLLIN;
pp[1].fd=sfd;
pp[1].events=POLLIN;
//4.等待链接
struct sockaddr_in cin;
cin.sin_family=AF_INET;
socklen_t len=sizeof(cin);
int newfd;
char buf[128]="";
int num;
char buf1[128]="";
struct sockaddr_in savein[1024];
//循环等待
while(1)
{
//阻塞等待一个文件描述符相应
int po=poll(pp,1024,-1);
if(po<0)
{
perror("poll");
return -1;
}
else if(po==0)
{
printf("超时\n");
return -1;
}
//执行被唤醒的文件描述符的工作
//阻塞等待连接用于通信
if(pp[1].revents==POLLIN)
{
newfd=accept(sfd,(struct sockaddr *)&cin,&len);
if(newfd==-1)
{
perror("accept");
return -1;
}
printf("accept success\n");
pp[newfd].fd=newfd;
pp[newfd].events=POLLIN;
//把客户信息存到结构体里
savein[newfd]=cin;
}
//终端输入
if(pp[0].revents==POLLIN)
{
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1]=0;
printf("buf=%s\n",buf);
for(int i=4;i<=1024;i++)
{
send(i,buf,sizeof(buf),0);
}
}
//通信
for(int i=4;i<1024;i++)
{
if(pp[i].revents==POLLIN)
{
bzero(buf,sizeof(buf));
int rev;
rev=recv(i,buf,sizeof(buf),0);
if(rev<0)
{
perror("rev");
return -1;
}
else if(rev==0)
{
printf("客户端关闭\n");
pp[i].revents=-1;
close(i);
continue;
}
printf("[%s:%d]:%s\n",inet_ntoa(savein[i].sin_addr),ntohs(savein[i].sin_port),buf);
printf("%d\n",i);
}
}
}
close(sfd);
return 0;
}
poll客户端
#include
#define IP "192.168.115.92"
#define PORT 8888
int main(int argc, const char *argv[])
{
//创建套接字
int cfd;
cfd=socket(AF_INET,SOCK_STREAM,0);
if(cfd==-1)
{
perror("socket");
return -1;
}
printf("create success\n");
//连接
struct sockaddr_in sin;
sin.sin_family=AF_INET;
sin.sin_port=htons(8888);
sin.sin_addr.s_addr=inet_addr("192.168.115.92");
if(connect(cfd,(struct sockaddr*)&sin,sizeof(sin))==-1)
{
perror("connect");
return -1;
}
printf("connect success\n");
//通信
struct pollfd pollfd[2]={{0,POLLIN},{cfd,POLLIN}};
char buf[128]="";
while(1)
{
int po=poll(pollfd,2,-1);
if(po<0)
{
perror("poll");
return -1;
}
else if(po==0)
{
printf("超时\n");
return -1;
}
if(pollfd[0].revents==POLLIN)
{
bzero(buf,sizeof(buf));
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1]=0;
send(cfd,buf,strlen(buf),0);
}
if(pollfd[1].revents==POLLIN)
{
bzero(buf,sizeof(buf));
recv(cfd,buf,sizeof(buf),0);
printf("服务器发来的数据:%s\n",buf);
}
}
//关闭
close(cfd);
return 0;
}