思维导图
select的TCP服务器模型
#include <myhead.h>
#define ERR_MSG(msg) do{\
fprintf(stderr, "line:%d %s %s", __LINE__, __FILE__, __func__);\
perror("msg");\
}while(0)
#define IP "192.168.2.54"
#define PORT 6666
int main(int argc, const char *argv[])
{
int sfd = socket(AF_INET, SOCK_STREAM, 0);
if(sfd < 0)
{
ERR_MSG("socket");
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;
}
if(listen(sfd, 128) < 0)
{
ERR_MSG("listen");
return -1;
}
struct sockaddr_in cin;
socklen_t addrlen = sizeof(cin);
int newfd = -1;
struct sockaddr_in saveCin[1024-3];
char buf[128] = "";
ssize_t res = 0;
fd_set readfds, tempfds;
FD_ZERO(&readfds);
FD_ZERO(&tempfds);
FD_SET(0, &readfds);
FD_SET(sfd, &readfds);
int res_select = 0;
int maxfd = sfd;
while(1)
{
tempfds = readfds;
res_select = select(maxfd+1, &tempfds, NULL, NULL, NULL);
if(res_select < 0)
{
ERR_MSG("select");
return -1;
}
else if(0 == res_select)
{
printf("time out...\n");
break;
}
for(int i=0; i<=maxfd; i++)
{
if(!FD_ISSET(i, &tempfds))
continue;
if(0 == i)
{
int sendfd;
res = scanf("%d %s", &sendfd, buf);
while(getchar() != '\n');
if(res != 2)
{
fprintf(stderr, "请输入正确格式: fd string\n" );
continue;
}
if(sendfd<3 || sendfd>1024 || FD_ISSET(sendfd, &readfds) == 0)
{
fprintf(stderr, "sndfd=%d文件描述符错误\n", sendfd);
continue;
}
if(send(sendfd, buf, sizeof(buf), 0) < 0)
{
ERR_MSG("send");
return -1;
}
printf("send success\n");
}
else if(sfd == i)
{
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);
saveCin[newfd-3] = 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(saveCin[i-3].sin_addr), ntohs(saveCin[i-3].sin_port), i);
close(i);
FD_CLR(i, &readfds);
int j = maxfd;
while(j-->=0 && !FD_ISSET(j, &readfds));
maxfd = j;
continue;
}
printf("[%s:%d] newfd:%d : %s\n",\
inet_ntoa(saveCin[i-3].sin_addr), ntohs(saveCin[i-3].sin_port), newfd, buf);
}
}
}
close(sfd);
return 0;
}
select的TCP客户端模型
#include <myhead.h>
#define ERR_MSG(msg) do{\
fprintf(stderr, "line:%d %s %s", __LINE__, __FILE__, __func__);\
perror(msg);\
}while(0)
#define IP "192.168.2.54"
#define PORT 6666
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(PORT);
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 __%d__\n", __LINE__);
fd_set readfds, tempfds;
FD_ZERO(&readfds);
FD_ZERO(&tempfds);
FD_SET(0, &readfds);
FD_SET(cfd, &readfds);
int maxfd = cfd;
int s_res = 0;
char buf[128] = "";
ssize_t 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;
}
if(FD_ISSET(0, &tempfds))
{
res = scanf( "%s" , buf);
while(getchar() != 10);
if(send(cfd, buf, sizeof(buf), 0) < 0)
{
ERR_MSG("send");
return -1;
}
}
if(FD_ISSET(cfd, &tempfds))
{
res = recv(cfd, buf, sizeof(buf), 0);
if(res < 0)
{
ERR_MSG("recv");
return -1;
}
else if(res == 0)
{
printf("服务器下线\n");
break;
}
printf("[服务器]:%s\n", buf);
}
}
close(cfd);
return 0;
}