tcp服务器 select
#include<my_head.h>
#define SER_PORT 8888
#define SER_IP "192.168.31.102"
int main(int argc, const char *argv[])
{
int sfd = socket(AF_INET,SOCK_STREAM,0);
if(sfd < 0){
perror("socket error");
return -1;
}
printf("sfd:%d\n",sfd);
//设置地址可快速重用
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(SER_PORT);
sin.sin_addr.s_addr = inet_addr(SER_IP);
if(bind(sfd,(struct sockaddr*)&sin,sizeof(sin))==-1)
{
perror("bind error");
return -1;
}
printf("bind success\n");
if(listen(sfd,128)==-1)
{
perror("listen error");
return -1;
}
printf("listen success\n");
struct sockaddr_in cin;
socklen_t addlen = sizeof(cin);
fd_set readfds;
fd_set tempfds;
struct sockaddr_in cinaddr[1024];
FD_ZERO(&readfds);
FD_SET(sfd,&readfds);
int newfd = 0;
int maxfd = sfd;
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("time out\n");
return -1;
}
for(int i = 0;i <= maxfd;i++)
{
if(!FD_ISSET(i,&tempfds))
{//判断文件描述符不再集合中,直接下一次循环
continue;
}
if(i==sfd)//执行到此,文件描述符在集合中,sfd存在表示触发
{
newfd = accept(sfd,(struct sockaddr*)&cin,&addlen);
if(newfd == -1)
{
perror("accpet error");
return -1;
}
printf("[%s:%d]:发来连接请求\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port));
FD_SET(newfd,&readfds);//将newfd放入集合
//更新maxfd
if(newfd >= maxfd)
{
maxfd = newfd;
}
cinaddr[newfd] = cin;//将客户端信息存放在数据中
}else
{//处理通信
char buf[512] = "";
bzero(buf,sizeof(buf));
if(recv(i,buf,sizeof(buf),0)==0)
{//接收到信息为0,表示客户端下线
printf("客户端下线\n");
close(i);
FD_CLR(i,&readfds);//将套接子描述符从集合中删除
//更新maxfd
for(int j = maxfd; j >= sfd;j--)
{//从最大的文件描述符开始判断是否存在集合中 以此找到在集合中最大的文件描述符
if(FD_ISSET(j,&readfds))
{
maxfd = j;
break;
}
}
continue;
}
printf("[%s:%d]:%s\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),buf);
//将消息转发给所有客户端
for(int k = 4;k<=maxfd;k++)
{
if(k!=i){
send(k,buf,strlen(buf),0);
}
}
}
}
}
close(sfd);
return 0;
}
tcp客户端 --poll
#include<myhead.h>
#include <poll.h>
#define SER_PORT 8888
#define SER_IP "192.168.31.102"
#define CLI_PORT 6666
#define CLI_IP "192.168.31.100"
int main(int argc, const char *argv[])
{
int cfd = socket(AF_INET,SOCK_STREAM,0);
if(cfd == -1)
{
perror("socket error");
return -1;
}
int reuser = 1;
if(setsockopt(cfd,SOL_SOCKET,SO_REUSEADDR,&reuser,sizeof(reuser))==-1)
{
perror("setsockopt error");
return -1;
}
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(cfd,(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(SER_PORT);
sin.sin_addr.s_addr = inet_addr(SER_IP);
if(connect(cfd,(struct sockaddr*)&sin,sizeof(sin))==-1)
{
perror("connect error");
return -1;
}
printf("connect success\n");
struct pollfd pfd[2];
pfd[0].fd = 0;
pfd[0].events = POLLIN;
pfd[1].fd = cfd;
pfd[1].events = POLLIN;
char rbuf[128] = "";
char wbuf[128] = "";
while(1)
{
int res = poll(pfd,2,-1);
if(res < 0)
{
perror("poll error");
return -1;
}else if(res == 0)
{
printf("time out\n");
return -1;
}
if(pfd[0].revents == POLLIN)
{
fgets(wbuf,sizeof(wbuf),stdin);
wbuf[strlen(wbuf)-1] = 0;
send(cfd,wbuf,strlen(wbuf),0);
printf("发送成功\n");
}
if(pfd[1].revents == POLLIN)
{
recv(cfd,rbuf,sizeof(rbuf),0);
printf("收到信息:%s\n",rbuf);
}
}
close(cfd);
return 0;
}