1.CP的select服务器
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <string.h>
#include <unistd.h>
#include <sys/select.h>
#include <sys/time.h>
#define ERR_MSG(msg) do{\
fprintf(stderr, "line:%d\n",__LINE__);\
perror(msg);\
}while(0)
#define IP "192.168.2.108"//ifconfig出来的本机ip
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("允许端口快速复用");
//
struct sockaddr_in sin;
sin.sin_family = AF_INET;//必须填充AF_INET
sin.sin_port = htons(8888);//1024~49151
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;
}
//创建一个读集合,存放要监测的文件描述符
fd_set readfds, tempfds;
FD_ZERO(&readfds); //清空合集
FD_SET(0, &readfds);
FD_SET(sfd, &readfds);
int maxfd = sfd; //存储集合中最大的文件描述符
struct sockaddr_in saveCin[1020];
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(sfd+1, &tempfds, NULL, NULL, NULL);
if (s_res <0)
{
ERR_MSG("select");
return -1;
}
else if (0 == s_res)
{
printf("time out.....\n");
break;
}
//循环遍历所有新产生的newfd,是否有对应的事件产生
//即所有的newfd对应的客户端是否有发送数据给服务器
for (int i = 0; i <= maxfd ; i++)
{
if (FD_ISSET(i,&tempfds) == 0)
{
continue;
}
if (0 == i)
{
printf("出发键盘事件》》》》》");
fflush(stdout);
fgets(buf, sizeof(buf), stdin);
buf[strlen(buf)-1] = 0;
printf("%s\n",buf);
}
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] newfed = %d 连接成功__%d__\n",\
inet_ntoa(cin.sin_addr), ntohs(cin.sin_port), newfd, __LINE__);
saveCin[newfd - 4] = cin;
//将newfd添加到readfds集合中
FD_SET(newfd, &readfds);
//更新maxfd
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] newfed = %d 客户端下线 __%d__\n", \
inet_ntoa(saveCin[i-4].sin_addr),ntohs(saveCin[i-4].sin_port),i,__LINE__);
close(i);
FD_CLR(i, &readfds);
//更新maxfd:从大到小遍历集合中最大的文件描述符,更新给maxfd
int j=maxfd;
for( ; j>=0; j--)
{
if(FD_ISSET(j, &readfds))
{
break;
}
}
maxfd = j;
//当客户端下线后,需要继续便利后面的文件描述符是否产生事件,所以需要使用continue
continue;
}
printf("[%s : %d] newfed = %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;
}
2.TCP的poll客户端
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <string.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.2.108"//ifconfig出来的本机ip
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;//必须填充AF_INET
sin.sin_port = htons(8888);//1024~49151
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("触发服务器交互事件");
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 <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <string.h>
#include <unistd.h>
#include <sys/select.h>
#include <sys/time.h>
#define ERR_MSG(msg) do{\
fprintf(stderr, "line:%d\n",__LINE__);\
perror(msg);\
}while(0)
#define IP "192.168.2.108"//ifconfig出来的本机ip
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;//必须填充AF_INET
sin.sin_port = htons(8888);//1024~49151
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");
//创建一个读集合,存放要监测的文件描述符
fd_set readfds;
FD_ZERO(&readfds); //清空合集
FD_SET(0, &readfds);
FD_SET(cfd, &readfds);
int maxfd = cfd; //存储集合中最大的文件描述符
char buf[128] = "";
ssize_t res = 0;
int s_res = 0;
while(1)
{
s_res = select(maxfd+1, &readfds, 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,&readfds))
{
printf("触发键盘输入事件\n");
bzero(buf, sizeof(buf));
printf("请输入>>>");
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(FD_ISSET(cfd, &readfds))
{
printf("触发服务器交互事件");
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;
}