1、思维导图
2、select实现TCP并发服务器
#include "head.h"
#define SET_IP "192.168.122.78"
#define SET_PORT 8888
#define CLI_IP "192.168.122.78"
#define CLI_PORT 5555
int main(int argc, const char *argv[])
{
int bfd=socket(AF_INET,SOCK_STREAM,0);
if(bfd==-1){
perror("socket error");
return -1;
}
printf("socket success bfd=%d\n",bfd);
struct sockaddr_in cin;
cin.sin_family=AF_INET;
cin.sin_port=htons(CLI_PORT);
cin.sin_addr.s_addr=inet_addr(CLI_IP);
if(bind(bfd,(struct sockaddr*)&cin,sizeof(cin))==-1){
perror("bind error");
return -1;
}
printf("bind success\n");
struct sockaddr_in sin;
sin.sin_family=AF_INET;
sin.sin_port=htons(SET_PORT);
sin.sin_addr.s_addr=inet_addr(SET_IP);
if(connect(bfd,(struct sockaddr*)&sin,sizeof(sin))==-1){
perror("connect error");
return -1;
}
printf("connect success\n");
struct pollfd pfds[2];
pfds[0].fd=0;
pfds[0].events=POLLIN;
pfds[1].fd=0;
pfds[1].events=POLLIN;
char wbuf[128]="";
while(1){
int res=poll(pfds,2,-1);
if(res==0){
printf("time out\n");
return -1;
}else if(res==-1){
perror("poll error\n");
return -1;
}
if(pfds[0].revents==POLLIN){
printf("请输入>>>");
fgets(wbuf,sizeof(wbuf),stdin);
wbuf[strlen(wbuf)-1]='\0';
if(strcmp(wbuf,"quit")==0)
{
break;
}
send(bfd,wbuf,strlen(wbuf),0);}
if(pfds[1].revents==POLLIN){
bzero(wbuf,sizeof(wbuf));
recv(bfd,wbuf,sizeof(wbuf),0);
printf("收到服务器的消息为:%s\n",wbuf);
}}
close(bfd);
return 0;
}
3、使用poll完成TCP的客户端
#include "head.h"
#define SER_IP "192.168.125.71"
#define SER_PORT 8888
int main(int argc, const char *argv[])
{
int afd=socket(AF_INET,SOCK_STREAM,0);
if(afd==-1){
perror("socket error\n");
return -1;
}
printf("socket success afd=%d\n",afd);
int reuse=1;
if(setsockopt(afd,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(SER_PORT);
sin.sin_addr.s_addr=inet_addr(SER_IP);
if(bind(afd,(struct sockaddr*)&sin,sizeof(sin))==-1)
{
perror("bind error");
return -1;
}
printf("bind success\n");
if(listen(afd,128)==-1){
perror("listen error");
return -1;
}
printf("listen success\n");
struct sockaddr_in cin;
socklen_t socklen=sizeof(cin);
int newfd=-1;
fd_set readfds,tempfds;
FD_ZERO(&readfds);
FD_SET(0,&readfds);
FD_SET(afd,&readfds);
int maxfd=afd;
struct sockaddr_in cin_arr[1024];
while(1){
tempfds=readfds;
int res=select(afd+1,&tempfds,NULL,NULL,NULL);
if(res==0){
printf("time out\n");
return -1;
}else if(res==-1){
perror("select error");
return -1;
}
for(int i=0;i<=maxfd;i++){
if(!FD_ISSET(i,&tempfds))
{
continue;
}
if(i=afd){
newfd=accept(afd,(struct sockaddr*)&cin,&socklen);
if(newfd==-1){
perror("accept error");
return -1;
}
printf("[%s:%d]:已连接,newfd=%d\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),newfd);
FD_SET(newfd,&readfds);
cin_arr[newfd]=cin;
if(maxfd<newfd){
maxfd=newfd;
}
}else if(i==0){
char buf[128]="";
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1]=0;
printf("触发键盘输入事件:%s\n",buf);
for (int i=4;i<=maxfd;i++){
send(i,buf,strlen(buf),0);
}}else{
char rbuf[128]="";
bzero(rbuf,sizeof(rbuf));
int ret=recv(newfd,rbuf,sizeof(rbuf),0);
if(ret==0){
printf("客户端已下线\n");
close(i);
FD_CLR(i,&readfds);
for(int j=maxfd;j>afd;j--){
if(FD_ISSET(j,&readfds)){
maxfd=j;
break;
}
}
continue;
}
printf("[%s:%d]:%s\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),rbuf);
for(int i=4;i<=maxfd;i++){
send(i,rbuf,strlen(rbuf),0);}
}
}
}
close(afd);
return 0;
}