作业:
- 将TCP的select服务器重新写一遍(上交)
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<unistd.h>
#include<arpa/inet.h>
#include<sys/time.h>
#include<sys/select.h>
#define ERR_MSG(msg) do{\
fprintf(stderr,"line:%d",__LINE__);\
perror(msg);\
}while(0)
#define IP "192.168.31.115"
int main(int argc, const char *argv[])
{
int sfd=socket(AF_INET,SOCK_STREAM,0);
if(sfd<0)
{
ERR_MSG("socket");
return -1;
}
int reuse=1;
if(setsockopt(sfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse))<0)
{
ERR_MSG("setsockopt");
return -1;
}
printf("允许端口快速重用\n");
struct sockaddr_in sin;
sin.sin_family=AF_INET;
sin.sin_port=htons(8888);
sin.sin_addr.s_addr=inet_addr(IP);
if(bind(sfd,(struct sockaddr*)&sin,sizeof(sin))<0)
{
ERR_MSG("bind");
return -1;
}
printf("bind success __%d__\n",__LINE__);
if(listen(sfd,128)<0)
{
ERR_MSG("listen");
return -1;
}
printf("listen success __%d__\n",__LINE__);
fd_set readfds,tempfds;
FD_ZERO(&readfds);
FD_SET(0,&readfds);
FD_SET(sfd,&readfds);
int maxfd=sfd;
struct sockaddr_in saveCin[1024-4];
struct sockaddr_in cin;
socklen_t addrlen=sizeof(cin);
int newfd=-1;
char buf[128]="";
ssize_t res=0;
int s_res=0;
while(1)
{
tempfds=readfds;
s_res=select(maxfd+1,&tempfds,NULL,NULL,NULL);
if(s_res<0)
{
ERR_MSG("select");
return -1;
}
else if(0==s_res)
{
printf("time out...\n");
break;
}
printf("__%d__\n",__LINE__);
for(int i=0;i<=maxfd;i++)
{
if(FD_ISSET(i,&tempfds)==0)
continue;
if(0==i)
{
printf("触发键盘输入事件>>>");
fflush(stdout);
int sndfd;
res=scanf("%d %s",&sndfd,buf);
while(getchar()!='\n');
if(res!=2)
{
fprintf(stderr,"请输入正确格式:fd string\n");
continue;
}
if(sndfd<=sfd||sndfd>=1024||FD_ISSET(sndfd,&readfds))
{
fprintf(stderr,"sndfd=%d 是非法的文件描述符\n",sndfd);
continue;
}
if(send(sndfd,buf,sizeof(buf),0)<0)
{
ERR_MSG("send");
return -1;
}
printf("send to %d success\n",sndfd);
}
else if(sfd==i)
{
printf("触发客户端连接事件\n");
fflush(stdout);
newfd=accept(sfd,(struct sockaddr*)&cin,&addrlen);
if(newfd<0)
{
ERR_MSG("accept");
return -1;
}
printf("[%s:%d] newfd=%d 连接成功 __%d__\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),newfd,__LINE__);
saveCin[newfd-4]=cin;
FD_SET(newfd,&readfds);
maxfd=maxfd>newfd?maxfd:newfd;
}
else
{
fprintf(stderr,"触发客户端交互事件>>>");
bzero(buf,sizeof(buf));
res=recv(i,buf,sizeof(buf),0);
if(res<0)
{
ERR_MSG("recv");
return -1;
}
else if(0==res)
{
printf("[%s:%d] newfd=%d 客户端下线 __%d__",inet_ntoa(saveCin[i-4].sin_addr),ntohs(saveCin[i-4].sin_port),i,__LINE__);
close(i);
FD_CLR(i,&readfds);
int j=maxfd;
for(;j>=0;j--)
{
if(FD_ISSET(j,&readfds))
break;
}
maxfd=j;
continue;
}
printf("[%s:%d] newfd=%d : %s __%d___\n",inet_ntoa(saveCin[i-4].sin_addr),ntohs(saveCin[i-4].sin_port),i,buf,__LINE__);
strcat(buf,"*_*");
if(send(i,buf,sizeof(buf),0)<0)
{
ERR_MSG("send");
return -1;
}
printf("发送成功\n");
}
}
}
close(sfd);
return 0;
}
- 将TCP的poll客户端写一遍(上交)
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<unistd.h>
#include<poll.h>
#define ERR_MSG(msg) do{\
fprintf(stderr,"line:%d\n",__LINE__);\
perror(msg);\
}while(0)
#define IP "192.168.31.115"
int main(int argc, const char *argv[])
{
int cfd=socket(AF_INET,SOCK_STREAM,0);
if(cfd<0)
{
ERR_MSG("socket");
return -1;
}
struct sockaddr_in sin;
sin.sin_family=AF_INET;
sin.sin_port=htons(8888);
sin.sin_addr.s_addr=inet_addr(IP);
if(connect(cfd,(struct sockaddr*)&sin,sizeof(sin))<0)
{
ERR_MSG("connect");
return -1;
}
printf("connect success\n");
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=0;
int p_res=-1;
while(1)
{
p_res=poll(fds,2,-1);
if(p_res<0)
{
ERR_MSG("poll");
return -1;
}
else if(0==p_res)
{
printf("time out...\n");
break;
}
if(fds[0].revents&POLLIN)
{
printf("触发键盘输入事件\n");
bzero(buf,sizeof(buf));
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1]='\0';
if(send(cfd,buf,sizeof(buf),0)<0)
{
ERR_MSG("send");
return -1;
}
printf("发送成功\n");
}
if(fds[1].revents&POLLIN)
{
printf("触发服务器交互事件\n");
bzero(buf,sizeof(buf));
res=recv(cfd,buf,sizeof(buf),0);
if(res<0)
{
ERR_MSG("recv");
return -1;
}
else if(0==res)
{
printf("服务器下线\n");
break;
}
printf(":%s__%d__\n",buf,__LINE__);
}
}
close(cfd);
return 0;
}
3.将TCP的select客户端写一遍(上交)
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<unistd.h>
#include<arpa/inet.h>
#include<sys/time.h>
#include<sys/select.h>
#define ERR_MSG(msg) do{\
fprintf(stderr,"line:%d",__LINE__);\
perror(msg);\
}while(0)
#define IP "192.168.31.115"
int main(int argc, const char *argv[])
{
int cfd=socket(AF_INET,SOCK_STREAM,0);
if(cfd<0)
{
ERR_MSG("socket");
return -1;
}
printf("socket create success cfd=%d __%d__\n",cfd,__LINE__);
struct sockaddr_in sin;
sin.sin_family=AF_INET;
sin.sin_port=htons(6666);
sin.sin_addr.s_addr=inet_addr(IP);
if(bind(cfd,(struct sockaddr*)&sin,sizeof(sin))<0)
{
ERR_MSG("bind");
return -1;
}
printf("bind success __%d__\n",__LINE__);
fd_set readfds,tempfds;
FD_ZERO(&readfds);
FD_SET(0,&readfds);
FD_SET(cfd,&readfds);
int maxfd=cfd;
struct sockaddr_in saveCin[1024-4];
struct sockaddr_in cin;
socklen_t addrlen=sizeof(cin);
int newfd=-1;
char buf[128]="";
ssize_t res=0;
int s_res=0;
while(1)
{
tempfds=readfds;
s_res=select(maxfd+1,&tempfds,NULL,NULL,NULL);
if(s_res<0)
{
ERR_MSG("select");
return -1;
}
else if(0==s_res)
{
printf("time out...\n");
break;
}
printf("__%d__\n",__LINE__);
for(int i=0;i<=maxfd;i++)
{
if(FD_ISSET(i,&tempfds)==0)
continue;
if(0==i)
{
if(sendto(cfd,buf,sizeof(buf),0,(struct sockaddr*)&sin,sizeof(sin))<0)
{
ERR_MSG("sendto");
return -1;
}
printf("sendto to %s success\n",buf);
}
else if(cfd==i)
{
bzero(buf,sizeof(buf));
printf("请输入>>>");
res=recvfrom(cfd,buf,sizeof(buf),0,(struct sockaddr*)&cin,&addrlen);
if(res<0)
{
ERR_MSG("recvfrom");
return -1;
}
printf("[%s:%d]:%s\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),buf);
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1]=0;
}
}
}
close(cfd);
return 0;
}