思维导图
select实现客户端
#include<head.h>
int main(int argc, const char *argv[])
{
int sfd=socket(AF_INET,SOCK_STREAM,0);
if(sfd==-1)
{
perror("socket error");
return -1;
}
//绑定
struct sockaddr_in sin;
sin.sin_family=AF_INET;
sin.sin_port=htons(6666);
sin.sin_addr.s_addr=inet_addr("192.168.122.22");
if(bind(sfd,(struct sockaddr*)&sin,sizeof(sin))==-1)
{
perror("bind error");
return -1;
}
//填充服务器地址信息结构体
struct sockaddr_in cin;
cin.sin_family=AF_INET;
cin.sin_port=htons(8888);
cin.sin_addr.s_addr=inet_addr("192.168.122.22");
printf("bind success %s %s %d\n",__FILE__,__func__,__LINE__);
//
if(connect(sfd,(struct sockaddr*)&cin,sizeof(cin))==-1)
{
perror("connect error");
return -1;
}
char buf[128]="";
fd_set readfds,tempfds;
FD_ZERO(&readfds);
FD_SET(0,&readfds);
FD_SET(sfd,&readfds);
tempfds=readfds;
while(1)
{
readfds=tempfds;
int res=select(sfd+1,&readfds,NULL,NULL,NULL);
if(res==-1)
{
perror("select error");
return -1;
}
else if(res==0)
{
printf("timeout\n");
return -1;
}
if(FD_ISSET(sfd,&readfds))
{
bzero(buf,sizeof(buf));
recvfrom(sfd,buf,sizeof(buf),0,0,0);
printf("收到消息为%s\n",buf);
if(strcmp(buf,"quit")==0)
{
break;
}
}
if(FD_ISSET(0,&readfds))
{
char rbuf[128]="";
fgets(rbuf,sizeof(rbuf),stdin);
rbuf[strlen(rbuf)-1]=0;
send(sfd,rbuf,sizeof(rbuf),0);
printf("发送成功\n");
if(strcmp(rbuf,"quit")==0)
{
break;
}
}
}
close(sfd);
return 0;
}
使用poll实现服务器端
#include<head.h>
#define SER_PORT 8888 //服务器端口号
#define SER_IP "192.168.122.22" //服务器ip地址
int main(int argc, const char *argv[])
{
int sfd=socket(AF_INET,SOCK_STREAM,0);
if(sfd==-1)
{
perror("cfd error");
return -1;
}
struct sockaddr_in cin;
cin.sin_family=AF_INET;
cin.sin_port=htons(SER_PORT);
cin.sin_addr.s_addr=inet_addr(SER_IP);
if(bind(sfd,(struct sockaddr*)&cin,sizeof(cin))==-1)
{
perror("bind error");
return -1;
}
printf("绑定成功\n");
if(listen(sfd,128)==-1)
{
perror("listen error");
return -1;
}
struct sockaddr_in sin;
socklen_t socklen=sizeof(sin);
struct sockaddr_in sin_arr[1024];
int newfd=-1;
struct pollfd pfd[1024];
pfd[0].fd=0;
pfd[0].events=POLLIN;
pfd[1].fd=sfd;
pfd[1].events=POLLIN;
int n=2;
while(1)
{
int res=poll(pfd,n,-1);
if(res==-1)
{
perror("poll error");
return -1;
}else if(res==0)
{
printf("timeout\n");
return -1;
}
if(pfd[0].revents==POLLIN)
{
char buf[128]="";
bzero(buf,sizeof(buf));
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1]=0;
for(int i=2;i<n;i++)
{
send(pfd[i].fd,buf,sizeof(buf),0);
}
printf("发送成功\n");
if(strcmp(buf,"quit")==0)
{
close(sfd);
return 0;
}
}
if(pfd[1].revents==POLLIN)
{
int newfd=accept(sfd,(struct sockaddr*)&sin,&socklen);
if(newfd==-1)
{
perror("newfd error");
return -1;
}
printf("%s,%d 链接成功 %s %s %d\n",inet_ntoa(sin.sin_addr),ntohs(cin.sin_port),__FILE__,__func__,__LINE__);
sin_arr[n]=sin;
pfd[n].fd=newfd;
pfd[n].events=POLLIN;
n++;
}
for(int cli=2;cli<n;cli++)
{
if(pfd[cli].revents==POLLIN)
{
char sbuf[128]="";
bzero(sbuf,sizeof(sbuf));
int rea=recv(pfd[cli].fd,sbuf,sizeof(sbuf),0);
if(rea==0)
{
printf("客户端下线\n");
close(pfd[cli].fd);
for(int j=cli;j<n;j++)
{
pfd[j]=pfd[j+1];
}
n--;
continue;
}
printf("[%s:%d]:%s\n",inet_ntoa(sin_arr[cli].sin_addr),ntohs(sin_arr[cli].sin_port),sbuf);
strcat(sbuf,"yyds");
send(pfd[cli].fd,sbuf,sizeof(sbuf),0);
printf("发送成功\n");
}
}
}
return 0;
}