select的TCP服务器/客户端
select的服务器
代码示例
#include
#include
#include
#include
#include
#include
#include
#define ERR_MSG(msg) do{\
fprintf(stderr,"__%d__",__LINE__);\
perror(msg);\
}while(0);
#define PORT 8888
#define IP "192.168.31.156"
int update_maxfd(int maxfd,fd_set readfds);
int main(int argc, const char *argv[])
{
int sfd = socket(AF_INET,SOCK_STREAM,0);
if(sfd < 0)
{
ERR_MSG("socket");
return -1;
}
printf("create socket success\n");
int reuse = 1;
if(setsockopt(sfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse)) < 0)
{
ERR_MSG("setsockopt");
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(bind(sfd,(struct sockaddr*)&sin,sizeof(sin)) < 0 )
{
ERR_MSG("bind");
return -1;
}
printf("bind success\n");
if(listen(sfd,10) < 0)
{
ERR_MSG("listen");
return -1;
}
printf("listen success\n");
struct sockaddr_in cin;
socklen_t addrlen = sizeof(cin);
fd_set readfds,tempfds;
FD_ZERO(&readfds);
FD_SET(0,&readfds);
FD_SET(sfd,&readfds);
int maxfd = sfd;
int select_res = 0;
char buf[128] = "";
ssize_t res = 0;
int newfd = -1;
struct timeval tm;
tm.tv_sec = 300;
tm.tv_usec = 0;
struct sockaddr_in arr[1020];
while(1)
{
tempfds = readfds;
select_res = select(maxfd+1,&tempfds,NULL,NULL,&tm);
if(select_res < 0)
{
ERR_MSG("select");
break;
}
else if(0 == select_res)
{
fprintf(stderr,"select time out\n");
break;
}
for(int i = 0;i<=maxfd;i++)
{
if(FD_ISSET(i,&tempfds) == 0)
{
continue;
}
if(0 == i)
{
printf("触发键盘输入事件:");
fflush(stdout);
bzero(buf, sizeof(buf));
int sendfd;
int res = scanf("%d %s", &sendfd, buf);
while(getchar() != 10);
if(res != 2)
{
fprintf(stderr, "请输入正确格式:文件描述符 要发送的内容\n");
continue;
}
if(FD_ISSET(sendfd, &readfds) == 0)
{
fprintf(stderr, "sendfd=%d 文件描述符不在集合中\n", sendfd);
continue;
}
if(send(sendfd, buf, sizeof(buf), 0) < 0)
{
ERR_MSG("send");
return -1;
}
printf("send message success\n");
}
else if(sfd == i)
{
printf("触发客户端连接事件:");
fflush(stdout);
newfd = accept(sfd,(struct sockaddr*)&cin,&addrlen);
if(newfd < 0)
{
ERR_MSG("accept");
return -1;
}
printf("[%s : %d] newfd = %d\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),newfd);
arr[newfd-4] = cin;
FD_SET(newfd,&readfds);
maxfd = maxfd>newfd?maxfd:newfd;
}
else
{
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客户端退出\n",inet_ntoa(arr[i-4].sin_addr),ntohs(arr[i-4].sin_port),i);
close(i);
FD_CLR(i,&readfds);
maxfd = update_maxfd(maxfd,readfds);
}
else
printf("[%s : %d] newfd = %d : %s \n",inet_ntoa(arr[i-4].sin_addr),ntohs(arr[i-4].sin_port),i,buf);
}
}
}
close(sfd);
return 0;
}
int update_maxfd(int maxfd,fd_set readfds)
{
int i=maxfd;
for(;i>0;i--)
{
if(FD_ISSET(i,&readfds))
return i;
}
return 0;
}
select的客户端
代码示例
#include
#include
#include
#include
#include
#include
#include
#define ERR_MSG(msg) do{\
fprintf(stderr,"__%d__",__LINE__);\
perror(msg);\
}while(0);
#define PORT 8888
#define IP "192.168.31.156"
int main(int argc, const char *argv[])
{
int sfd = socket(AF_INET,SOCK_STREAM,0);
if(sfd < 0)
{
ERR_MSG("socket");
return -1;
}
printf("create socket success\n");
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(PORT);
sin.sin_addr.s_addr = inet_addr(IP);
if(connect(sfd,(struct sockaddr*)&sin,sizeof(sin))<0)
{
ERR_MSG("connect");
return -1;
}
printf("connect success\n");
fd_set readfds,tempfds;
FD_ZERO(&readfds);
FD_SET(0,&readfds);
FD_SET(sfd,&readfds);
int maxfd = sfd;
int select_res = 0;
char buf[128];
ssize_t res=0;
while(1)
{
tempfds = readfds;
select_res = select(maxfd+1,&tempfds,NULL,NULL,NULL);
if(select_res < 0)
{
ERR_MSG("select");
break;
}
else if(0 == select_res)
{
fprintf(stderr,"select time out\n");
break;
}
for(int i=0;i<=maxfd;i++)
{
if(FD_ISSET(i,&tempfds) == 0)
continue;
if(0 == i)
{
bzero(buf,sizeof(buf));
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1] = '\0';
if(send(sfd, buf, sizeof(buf),0) < 0)
{
ERR_MSG("send");
return -1;
}
}
else if(sfd == i)
{
bzero(buf,sizeof(buf));
res = recv(sfd,buf,sizeof(buf),0);
if(res < 0 )
{
ERR_MSG("recv");
return -1;
}
else if(0 == res)
{
printf("server is off-line\n");
return 0;
}
else
printf("%ld : %s\n",res,buf);
}
}
}
close(sfd);
return 0;
}