嵌入式学习-网络编程-Day5
一、思维导图
二、作业
1.使用poll实现TCP服务器的并发
include <myhead.h>
int main(int argc, const char *argv[])
{
int sfd=-1;
if((sfd=socket(AF_INET,SOCK_STREAM,0))==-1)
{
perror("socket error");
return -1;
}
int reuse = 1;
if(setsockopt(sfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse))==-1)
{
perror("setsockopt error");
return -1;
}
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(8888);
sin.sin_addr.s_addr = inet_addr("192.168.122.39");
if(bind(sfd,(struct sockaddr*)&sin,sizeof(sin))==-1)
{
perror("bind error");
return -1;
}
if(listen(sfd,128)==-1)
{
perror("listen error");
return -1;
}
struct sockaddr_in cin;
socklen_t socklen = sizeof(cin);
struct sockaddr_in cin_arr[1024];
int count_fd=4;
struct pollfd pfd[count_fd];
pfd[0].fd=0;
pfd[3].fd=sfd;
pfd[0].events=POLLIN;
pfd[3].events=POLLIN;
int maxfd=sfd;
int newfd=-1;
char rbuf[128]="";
char wbuf[128]="";
while(1)
{
int flag=poll(pfd,count_fd,-1);
if(flag<0)
{
perror("poll error");
return -1;
}
if(pfd[sfd].revents=POLLIN)
{
newfd=accept(sfd,(struct sockaddr*)&cin,&socklen);
if(newfd==-1)
{
perror("accept error");
return -1;
}
cin_arr[newfd]=cin;
printf("[%s:%d]发来连接请求,newfd=%d\n",inet_ntoa(cin_arr[newfd].sin_addr),ntohs(cin_arr[newfd].sin_port),newfd);
pfd[newfd].events=POLLIN;
pfd[newfd].fd=newfd;
if(newfd>maxfd)
{
maxfd=newfd;
count_fd++;
}
}
if(pfd[0].revents==POLLIN)
{
memset(wbuf,0,sizeof(wbuf));
fgets(wbuf,sizeof(wbuf),stdin);
wbuf[strlen(wbuf)-1]=0;
for(int i=4;((i<=count_fd)&&(pfd[i].fd!=-1));i++)
{
send(i,wbuf,sizeof(wbuf),0);
}
printf("发送成功\n");
}
for(int i=4;i<=maxfd&&pfd[i].fd!=-1;i++)
{
if(pfd[i].revents==POLLIN)
{
memset(rbuf,0,sizeof(rbuf));
int res=recv(i,rbuf,sizeof(rbuf),0);
if(res==0)
{
printf("客户端已经下线\n");
close(i);
pfd[i].fd=-1;
}
printf("[%s:%d]:%s\n",inet_ntoa(cin_arr[i].sin_addr),\
ntohs(cin_arr[i].sin_port),rbuf);
strcat(rbuf,"^_^");
send(i,rbuf,sizeof(rbuf),0);
printf("发送成功\n");
}
}
}
return 0;
}
使用select实现TCP客户端的并发
#include
#define SER_IP "192.168.122.39"
#define SER_PORT 8888
int main(int argc, const char *argv[])
{
int cfd=-1;
if((cfd=socket(AF_INET,SOCK_STREAM,0))==-1)
{
perror("socket error\n");
return -1;
}
struct sockaddr_in cin;
cin.sin_family=AF_INET;
cin.sin_port=htons(6666);
cin.sin_addr.s_addr=inet_addr("192.168.122.39");
if(bind(cfd,(struct sockaddr*)&cin,sizeof(cin))==-1)
{
perror("bind error");
return -1;
}
struct sockaddr_in sin;
sin.sin_family=AF_INET;
sin.sin_port=htons(8888);
sin.sin_addr.s_addr=inet_addr("192.168.122.39");
if(connect(cfd,(struct sockaddr*)&sin,sizeof(sin))==-1)
{
perror("connect error");
return -1;
}
printf("connect success\n");
fd_set readfds,tempfds;
FD_ZERO(&readfds);
FD_SET(0,&readfds);
FD_SET(cfd,&readfds);
int maxfd=cfd;
char buf[128]="";
while(1)
{
tempfds=readfds;
int res=select(maxfd+1,&tempfds,NULL,NULL,NULL);
if(res==-1)
{
perror("select error");
return -1;
}else if(res==0)
{
printf("timeout\n");
return -1;
}
if(FD_ISSET(0,&tempfds))
{
memset(buf,0,sizeof(buf));
read(0,buf,sizeof(buf));
buf[strlen(buf)-1]=0;
if(strcmp(buf,"quit")==0)
{
printf("已退出\n");
close(cfd);
break;
}
write(cfd,buf,sizeof(buf));
printf("发送成功\n");
}
if(FD_ISSET(cfd,&tempfds))
{
recv(cfd,buf,sizeof(buf),0);
printf("[%s:%d]:%s\n",SER_IP,SER_PORT,buf);
}
}
return 0;
}