标签:
select.c
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 #include <unistd.h> 5 #include <arpa/inet.h> 6 #include <sys/socket.h> 7 #include <sys/time.h> 8 #include <sys/socket.h> 9 10 #define BUF_SIZE 100 11 void error_handling(char* message); 12 13 int main(int argc,char* argv[]) 14 { 15 int serv_sock,clnt_sock; 16 struct sockaddr_in serv_addr,clnt_addr; 17 struct timeval timeout; 18 fd_set reads,cp_reads; 19 socklen_t adr_sz; 20 int fd_max,str_len,fd_num,i; 21 char buf[BUF_SIZE]; 22 23 if(argc != 2) 24 { 25 printf("usage: %s <port>\n",argv[0]); 26 exit(1); 27 } 28 29 serv_sock = socket(PF_INET,SOCK_STREAM,0); 30 memset(&serv_addr,0,sizeof(serv_addr)); 31 serv_addr.sin_family = AF_INET; 32 serv_addr.sin_addr.s_addr = htonl(INADDR_ANY); 33 serv_addr.sin_port = htons(atoi(argv[1])); 34 35 if(bind(serv_sock,(struct sockaddr*)&serv_addr,sizeof(serv_addr)) == -1) 36 error_handling("bind error"); 37 if(listen(serv_sock,5) == -1) 38 error_handling("listen error"); 39 40 FD_ZERO(&reads); 41 FD_SET(serv_sock,&reads); 42 fd_max = serv_sock; 43 while(1) 44 { 45 cp_reads = reads; 46 timeout.tv_sec = 5; 47 48 if((fd_num = select(fd_max+1,&cp_reads,0,0,&timeout)) == -1) 49 break; 50 if(fd_num == 0) 51 continue; 52 53 for(i = 0;i < fd_max+1;i++) 54 { 55 if(FD_ISSET(i,&cp_reads)) 56 { 57 if(i == serv_sock) 58 { 59 adr_sz = sizeof(clnt_addr); 60 clnt_sock = accept(serv_sock,(struct sockaddr*)&clnt_addr,&adr_sz); 61 FD_SET(clnt_sock,&reads); 62 if(fd_max < clnt_sock) 63 fd_max = clnt_sock; 64 printf("connected client:%d \n",clnt_sock); 65 } 66 else 67 { 68 str_len = read(i,buf,BUF_SIZE); 69 if(str_len == 0) 70 { 71 FD_CLR(i,&reads); 72 close(i); 73 printf("closed client: %d\n",i); 74 } 75 else 76 write(i,buf,str_len); 77 } 78 } 79 } 80 } 81 close(serv_sock); 82 return 0; 83 } 84 85 void error_handling(char* message) 86 { 87 fputs(message,stderr); 88 fputc(‘\n‘,stderr); 89 exit(1); 90 }
epoll.c
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 #include <unistd.h> 5 #include <arpa/inet.h> 6 #include <sys/socket.h> 7 #include <sys/epoll.h> 8 9 #define BUF_SIZE 100 10 #define EPOLL_SIZE 50 11 void error_handling(char* message); 12 int main(int argc,char* argv[]) 13 { 14 struct sockaddr_in serv_addr,clnt_addr; 15 int serv_sock,clnt_sock; 16 socklen_t adr_sz; 17 int str_len,i; 18 char buf[BUF_SIZE]; 19 20 struct epoll_event *ep_events; 21 struct epoll_event event; 22 int epfd,event_cnt; 23 24 if(argc != 2) 25 { 26 printf("usage:%s <port>\n",argv[0]); 27 exit(1); 28 } 29 30 serv_sock = socket(PF_INET,SOCK_STREAM,0); 31 serv_addr.sin_family = AF_INET; 32 serv_addr.sin_addr.s_addr = htonl(INADDR_ANY); 33 serv_addr.sin_port = htons(atoi(argv[1])); 34 35 if(bind(serv_sock,(struct sockaddr*)&serv_addr,sizeof(serv_addr)) == -1) 36 error_handling("bind error"); 37 if(listen(serv_sock,5) == -1) 38 error_handling("listen error"); 39 40 epfd = epoll_create(EPOLL_SIZE); 41 ep_events = malloc(sizeof(struct epoll_event)*EPOLL_SIZE); 42 43 event.events = EPOLLIN; 44 event.data.fd = serv_sock; 45 epoll_ctl(epfd,EPOLL_CTL_ADD,serv_sock,&event); 46 47 while(1) 48 { 49 event_cnt = epoll_wait(epfd,ep_events,EPOLL_SIZE,-1); 50 if(event_cnt == -1) 51 { 52 puts("epoll_wait error"); 53 break; 54 } 55 for(i = 0;i < event_cnt;i++) 56 { 57 if(ep_events[i].data.fd == serv_sock) 58 { 59 adr_sz = sizeof(clnt_addr); 60 clnt_sock = accept(serv_sock,(struct sockaddr*)&clnt_addr,&adr_sz); 61 event.events = EPOLLIN; 62 event.data.fd = clnt_sock; 63 epoll_ctl(epfd,EPOLL_CTL_ADD,clnt_sock,&event); 64 printf("connected client:%d\n",clnt_sock); 65 } 66 else 67 { 68 str_len = read(ep_events[i].data.fd,buf,BUF_SIZE); 69 if(str_len == 0) 70 { 71 epoll_ctl(epfd,EPOLL_CTL_DEL,ep_events[i].data.fd,NULL); 72 close(ep_events[i].data.fd); 73 printf("closed client:%d\n",ep_events[i].data.fd); 74 } 75 else 76 write(ep_events[i].data.fd,buf,str_len); 77 } 78 } 79 } 80 close(serv_sock); 81 close(epfd); 82 return 0; 83 } 84 85 void error_handling(char* message) 86 { 87 fputs(message,stderr); 88 fputc(‘\n‘,stderr); 89 exit(1); 90 }
socket学习笔记——select与epoll函数的使用(linux)
标签:
原文地址:http://www.cnblogs.com/boyiliushui/p/4736963.html