//head.h 头文件
//TcpGrpSer.c 服务器端
//TcpGrpUsr.c 客户端
select函数
功能:阻塞函数,让内核去监测集合中的文件描述符是否准备就绪,若准备就绪则解除阻塞。
原型:
#include <sys/select.h>#include <sys/time.h>#include <sys/types.h>#include <unistd.h>int select(int nfds, fd_set *readfds, fd_set *writefds,fd_set *exceptfds, struct timeval *timeout); 参数:int nfds:所有集合中最大的文件描述符+1; fd_set *readfds, fd_set *writefds,fd_set *exceptfds:读集合,写集合,其他集合。用不上的集合填NULL;struct timeval *timeout:设置超时时间; 1) 填NULL,不设置超时时间,会一直阻塞直到文件描述符准备就绪,解除阻塞;2) 设置超时时间;struct timeval {long tv_sec; /* seconds */ 秒long tv_usec; /* microseconds */ 微秒}; 返回值:>0, 成功,返回成功触发事件的文件描述符个数;=0, 超时了=-1,函数运行失败,更新errno; 操作集合的函数: void FD_CLR(int fd, fd_set *set); 将指定的fd从集合中删除int FD_ISSET(int fd, fd_set *set); 判断fd是否在集合中,若存在返回真,否则返回假void FD_SET(int fd, fd_set *set); 将fd添加到集合中void FD_ZERO(fd_set *set); 清空
head.h
#ifndef __HEAD_H__
#define __HEAD_H__#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<strings.h>
#include<unistd.h>
#include<math.h>
#include<errno.h>
#include<fcntl.h>
#include<signal.h>#include<sys/stat.h>
#include<sys/wait.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/msg.h>
#include<sys/shm.h>
#include<sys/time.h>
#include<sys/sem.h>#include<pthread.h>
#include<semaphore.h>#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<sys/select.h>
#include<poll.h>#define NUM 10
#define ERR_MSG(msg) do{\printf("line: %d\n",__LINE__);\perror(msg);\
}while(0)
#define PORT 6666 //端口号的网络字节序 1024~49151
#define IP "192.168.250.100" //ifconfig查看本机IP (ipv4)#endif
TcpGrpSer.c
#include "head.h"int main(int argc, const char *argv[])
{//创建流式套接字int sfd = socket(AF_INET,SOCK_STREAM,0);if(sfd<0){ERR_MSG("socket");return -1;}//填充服务器自身的地址信息结构体//真实的地址信息结构体根据地址族制定AF_INET ; man 7 ipstruct sockaddr_in sin; sin.sin_family = AF_INET; //必须填充AF_INETsin.sin_port = htons(PORT); //端口号的网络字节序 1024~49151sin.sin_addr.s_addr = inet_addr(IP); //ifconfig查看本机IPint reuse = 1;if(setsockopt(sfd, SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse)) < 0) //允许端口快速被重复使用{ERR_MSG("setsockopt");return -1;}//绑定连接if(bind(sfd,(struct sockaddr*)&sin,sizeof(sin))<0){ERR_MSG("bind");return -1;}printf("bind success\n");//设置监听if(listen(sfd,128) < 0){ERR_MSG("listen");return -1;}//创建读集合fd_set readfds,tempfds;//清空集合FD_ZERO(&readfds);FD_ZERO(&tempfds);//将集合监测的文件描述符放入集合FD_SET(0,&readfds);FD_SET(sfd,&readfds);//用顺序表存储文件描述符的端口信息//监测文件描述符是否准备就绪int maxfd = sfd;int s_res = 0;struct sockaddr_in cin;socklen_t len = sizeof(cin);struct sockaddr_in savcin[1024];int newfd = -1;char buf[128]="";ssize_t res = 0;while(1){tempfds = readfds;//备份readfdss_res = select(maxfd+1,&tempfds,NULL,NULL,NULL);if(s_res<0){ERR_MSG("select");return -1;}else if(0 == s_res){printf("超时...\n");break;}//与客户端通信for(int i=0;i<=maxfd;i++){if(!FD_ISSET(i,&tempfds))continue;if(0 == i){printf("触发键盘输入事件\n");int sndfd=-1;res=scanf("%d %s",&sndfd,buf);while(getchar() !=10);if(res!=2){printf("请输入正确数据格式:[fd(4~1023)] string\n"); continue;}//判断文件是否合法if(sndfd<sfd||sndfd>1023||!FD_ISSET(sndfd,&readfds)){printf("sndfd = %d 是非法文件描述符\n",sndfd);continue;}if(send(sndfd,buf,sizeof(buf),0)<0){ERR_MSG("send");}bzero(buf,sizeof(buf));}else if(i == sfd){printf("触发客户端连建事件\n");newfd = accept(sfd,(struct sockaddr *)&cin,&len);if(newfd < 0){perror("accept");return -1;}savcin[newfd]=cin;printf("[%s:%d] 客户端连接成功 newfd = %d __%d__ \n",\inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),newfd,__LINE__);//将新生成的newfd添加到readfdsFD_SET(newfd,&readfds);//更新maxfdmaxfd = maxfd > newfd? maxfd:newfd;}else{printf("触发客户端连建事件__%d__\n",__LINE__);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} sfd = %d,__%d__ 已下线,结束对话\n",\inet_ntoa(savcin[i].sin_addr),ntohs(savcin[i].sin_port),i,__LINE__); close(i);//关闭文件描述符FD_CLR(i,&readfds);/* //更新maxfdint j=maxfd;for(;j<=0;j--){if(FD_ISSET(j,&readfds)) break;}maxfd = j;*///更新maxfdwhile(!FD_ISSET(maxfd,&readfds)&&maxfd-->=0);continue;}printf("{%s:%d} sfd = %d : %s,__%d__\n",\inet_ntoa(savcin[i].sin_addr),ntohs(savcin[i].sin_port),i,buf,__LINE__); //发送数据strcat(buf,"*_*");if(send(i,buf,sizeof(buf),0)<0){ERR_MSG("send");return -1;}printf("发送成功\n");}}}if(close(sfd)<0){ERR_MSG("close");return -1;}return 0;
}
TcpGrpUsr.c
#include "head.h"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;}//填充服务器自身的地址信息结构体//真实的地址信息结构体根据地址族制定AF_INET ;struct sockaddr_in sin; sin.sin_family = AF_INET; //必须填充AF_INETsin.sin_port = htons(PORT); //端口号的网络字节序 1024~49151sin.sin_addr.s_addr = inet_addr(IP); //ifconfig查看本机IPif(connect(sfd,(struct sockaddr *)&sin,sizeof(sin))<0){perror("connect");return -1;}printf("连接成功\n");//创建集合struct pollfd fds[2];fds[0].fd = 0;fds[0].events = POLLIN;fds[1].fd = sfd;fds[1].events = POLLIN;char buf[128]="";int res=0;while(1){//阻塞方式监测集合res = poll(fds,2,-1);if(res < 0){ERR_MSG("poll");return -1;}else if(0 == res){printf("time out...\n"); //超时break;}//判断0文件描述符是否有POLLIN事件if((fds[0].revents & POLLIN)){fgets(buf,sizeof(buf),stdin);buf[strlen(buf)-1] = 0;if(send(sfd,buf,sizeof(buf),0) < 0){ERR_MSG("send");return -1;}printf("发送成功\n");}//判断sfd文件描述符是否有POLLIN事件if(fds[1].revents & POLLIN){//接收数据bzero(buf,sizeof(buf));res = recv(sfd,buf,sizeof(buf),0);if(res<0){ERR_MSG("recv");return -1;}else if(res == 0){printf("[%s:%d] 服务器下线__%d__ \n",\inet_ntoa(sin.sin_addr),ntohs(sin.sin_port),__LINE__);break;}printf("[%s:%d] cfd = %d : %s__%d__ \n",\inet_ntoa(sin.sin_addr),ntohs(sin.sin_port),sfd,buf,__LINE__);} }if(close(sfd)<0){ERR_MSG("close");return -1;}return 0;
}