1.使用selec实现客户端
#include<myhead.h>
#define PORT 9527
#define IP "192.168.114.169"
int main(int argc, const char *argv[])
{
//创建套接字
int cfd=socket(AF_INET,SOCK_STREAM,0);
if(cfd == -1)
{
perror("socket error");
return -1;
}
//连接
struct sockaddr_in sin;
sin.sin_family=AF_INET;
sin.sin_port=htons(PORT);
sin.sin_addr.s_addr=inet_addr(IP);
if(connect(cfd,(struct sockaddr*)&sin,sizeof(sin))==-1)
{
perror("connect error");
return -1;
}
//收发
char buf[128]="";
//定义一个用于检测的文件描述符集合
fd_set readfds,tempfds;
//清空
FD_ZERO(&readfds);
//设置集合内容
FD_SET(0,&readfds);
FD_SET(cfd,&readfds);
int res=0;
while(1)
{ tempfds=readfds;
//阻塞等待事件
res=select(cfd+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))
{
bzero(buf,sizeof(buf));
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1]='\0';
send(cfd,buf,sizeof(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;
}
2.使用poll实现服务端
#include<myhead.h>
#define PORT 9527 //端口
#define IP "192.168.114.169"//IP地址
int main(int argc, const char *argv[])
{
//1.创建用于接收连接的套接字
int sfd=socket(AF_INET,SOCK_STREAM,0);
if(sfd==-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;
} //2.绑定端口和IP
//2.1地址结构体信息
struct sockaddr_in sin;
sin.sin_family=AF_INET;
sin.sin_port=htons(PORT);
sin.sin_addr.s_addr=inet_addr(IP);
//2.2
if( bind(sfd,(struct sockaddr *)&sin,sizeof(sin))==-1)
{
perror("bind error");
return -1;
}
//3.将套接字设置为被动监听
if( listen(sfd,128)==-1)
{
perror("listen error");
return -1;
}
//4.阻塞等待接收连接
struct sockaddr_in cin;
cin.sin_family=AF_INET;
socklen_t socklen=sizeof(cin);
int newfd;
//文件描述符集合数组
struct pollfd fds[1024];
fds[0].fd=0;
fds[0].events=POLLIN;
fds[1].fd=sfd;
fds[1].events=POLLIN;
struct sockaddr_in save[1024];
int res=0;
char buf[128]="";
int maxfd=sfd;
while(1)
{
res=poll(fds,maxfd+1,-1);
if(res == -1)
{
perror("poll error");
return -1;
}
else if(res == 0)
{
printf("timeout\n");
return -1;
}
if(fds[1].revents==POLLIN)
{ if((newfd=accept(sfd,(struct sockaddr *)&cin,&socklen))==-1)
{
perror("accept error");
return -1;
}
printf("accept success newfd=%d\n",newfd);
fds[newfd].fd=newfd;
fds[newfd].events=POLLIN;
save[newfd]=cin;
if(newfd>maxfd)
{ maxfd=newfd;}
}
if(fds[0].revents==POLLIN)
{
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1]='\0';
printf("触发终端输入:%s\n",buf);
for(int i=4;i<=maxfd;i++)
{send(fds[i].fd,buf,sizeof(buf),0);}
}
for(int i=4;i<maxfd+1;i++)
{ if(fds[i].revents==POLLIN)
{
char buf[128]="";
bzero(buf,sizeof(buf));
int rs= recv(i,buf,sizeof(buf),0);
if(rs == 0)
{
printf("客户端下线\n");
close(fds[i].fd);
fds[i].fd=-1;
for(int j=maxfd;j>3;j--)
{
if(fds[j].fd!=-1)
{
maxfd=fds[j].fd;
break;
}
}
continue;
}
else if(rs<0)
{
perror("recv error");
return -1;
}
printf("[%s:%d]:%s\n",inet_ntoa(save[i].sin_addr),ntohs(save[i].sin_port),buf);
}
}
}
//6.关闭套接字
close(sfd);
return 0;
}