1)epoll 服务器端,TCP UDP都支持。
#include <unistd.h> #include <sys/types.h> /* basic system data types */ #include <sys/socket.h> /* basic socket definitions */ #include <netinet/in.h> /* sockaddr_in{} and other Internet defns */ #include <arpa/inet.h> /* inet(3) functions */ #include <sys/epoll.h> /* epoll function */ #include <fcntl.h> /* nonblocking */ #include <sys/resource.h> /*setrlimit */ #include <stdlib.h> #include <errno.h> #include <stdio.h> #include <string.h> //#define UDP_CONNECT_WAY //#define TCP_CONNECT_WAY #define MAXEPOLLSIZE 10000 #define MAXLINE 10240 int handle(int connfd,struct sockaddr *clientaddr,socklen_t socklen); int setnonblocking(int sockfd) { if (fcntl(sockfd, F_SETFL, fcntl(sockfd, F_GETFD, 0)|O_NONBLOCK) == -1) { return -1; } return 0; } int main(int argc, char **argv) { int servPort = 6888; int listenq = 1024; int listenfd, connfd, kdpfd, nfds, n, nread, curfds,acceptCount = 0; struct sockaddr_in servaddr, cliaddr; socklen_t socklen = sizeof(struct sockaddr_in); struct epoll_event ev; struct epoll_event events[MAXEPOLLSIZE]; struct rlimit rt; char buf[MAXLINE]; /* \u8bbe\u7f6e\u6bcf\u4e2a\u8fdb\u7a0b\u5141\u8bb8\u6253\u5f00\u7684\u6700\u5927\u6587\u4ef6\u6570 */ rt.rlim_max = rt.rlim_cur = MAXEPOLLSIZE; if (setrlimit(RLIMIT_NOFILE, &rt) == -1) { perror("setrlimit error"); return -1; } bzero(&servaddr, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_addr.s_addr = htonl (INADDR_ANY); servaddr.sin_port = htons (servPort); #ifdef UDP_CONNECT_WAY listenfd = socket(AF_INET, SOCK_DGRAM, 0); #else listenfd = socket(AF_INET, SOCK_STREAM, 0); #endif if (listenfd == -1) { perror("can't create socket file"); return -1; } int opt = 1; setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)); if (setnonblocking(listenfd) < 0) { perror("setnonblock error"); } //get buffer int rev_buff_size = 0; int send_buff_size = 0; socklen_t optlen; optlen = sizeof(rev_buff_size); if(0 == getsockopt(listenfd, SOL_SOCKET, SO_RCVBUF, &rev_buff_size, &optlen)) { printf("default socket receive buff size %d bytes = %d KB \n",rev_buff_size,rev_buff_size/1024); } else { printf("errorNo. %d \n",errno); } optlen = sizeof(send_buff_size); if(0 == getsockopt(listenfd, SOL_SOCKET, SO_SNDBUF, &send_buff_size, &optlen)) { printf("default socket send buff size %d bytes = %d KB \n",send_buff_size,send_buff_size/1024); } else { printf("errorNo. %d \n",errno); } if (bind(listenfd, (struct sockaddr *) &servaddr, sizeof(struct sockaddr)) == -1) { perror("bind error"); return -1; } #ifdef UDP_CONNECT_WAY #else if (listen(listenfd, listenq) == -1) { perror("listen error"); return -1; } #endif if(0 == getsockopt(listenfd, SOL_SOCKET, SO_RCVBUF, &rev_buff_size, &optlen)) { printf("1default socket receive buff size %d bytes = %d KB \n",rev_buff_size,rev_buff_size/1024); } else { printf("errorNo. %d \n",errno); } optlen = sizeof(send_buff_size); if(0 == getsockopt(listenfd, SOL_SOCKET, SO_SNDBUF, &send_buff_size, &optlen)) { printf("1default socket send buff size %d bytes = %d KB \n",send_buff_size,send_buff_size/1024); } else { printf("errorNo. %d \n",errno); } /* \u521b\u5efa epoll \u53e5\u67c4\uff0c\u628a\u76d1\u542c socket \u52a0\u5165\u5230 epoll \u96c6\u5408\u91cc */ kdpfd = epoll_create(MAXEPOLLSIZE); ev.events = EPOLLIN | EPOLLET; ev.data.fd = listenfd; if (epoll_ctl(kdpfd, EPOLL_CTL_ADD, listenfd, &ev) < 0) { fprintf(stderr, "epoll set insertion error: fd=%d\n", listenfd); return -1; } curfds = 1; printf("epollserver startup,port %d, max connection is %d, backlog is %d\n", servPort, MAXEPOLLSIZE, listenq); for (;;) { /* \u7b49\u5f85\u6709\u4e8b\u4ef6\u53d1\u751f */ nfds = epoll_wait(kdpfd, events, curfds, -1); if (nfds == -1) { perror("epoll_wait"); continue; } /* \u5904\u7406\u6240\u6709\u4e8b\u4ef6 */ for (n = 0; n < nfds; ++n) { if (events[n].data.fd == listenfd) { #ifdef UDP_CONNECT_WAY #else connfd = accept(listenfd, (struct sockaddr *)&cliaddr,&socklen); if (connfd < 0) { perror("accept error"); continue; } sprintf(buf, "accept form %s:%d\n", inet_ntoa(cliaddr.sin_addr), cliaddr.sin_port); printf("%d:%s", ++acceptCount, buf); if (curfds >= MAXEPOLLSIZE) { fprintf(stderr, "too many connection, more than %d\n", MAXEPOLLSIZE); close(connfd); continue; } if (setnonblocking(connfd) < 0) { perror("setnonblocking error"); } ev.events = EPOLLIN | EPOLLET; ev.data.fd = connfd; if (epoll_ctl(kdpfd, EPOLL_CTL_ADD, connfd, &ev) < 0) { fprintf(stderr, "add socket '%d' to epoll failed: %s\n", connfd, strerror(errno)); return -1; } curfds++; continue; #endif } // \u5904\u7406\u5ba2\u6237\u7aef\u8bf7\u6c42 if (handle(events[n].data.fd,(struct sockaddr *)&cliaddr,socklen) < 0) { epoll_ctl(kdpfd, EPOLL_CTL_DEL, events[n].data.fd,&ev); curfds--; } } } close(listenfd); return 0; } int handle(int connfd,struct sockaddr *clientaddr,socklen_t socklen) { int nread; char buf[MAXLINE]; // socklen_t socklen = 0; // struct sockaddr_in clientaddr; // socklen = sizeof(struct sockaddr); memset(buf,0,sizeof(buf)); #ifdef UDP_CONNECT_WAY nread = recvfrom(connfd,buf,sizeof(buf),0,(struct sockaddr*)clientaddr,&socklen); #else //nread = read(connfd, buf, MAXLINE);//\u8bfb\u53d6\u5ba2\u6237\u7aefsocket\u6d41 nread = recvfrom(connfd,buf,sizeof(buf),0,(struct sockaddr*)clientaddr,&socklen); #endif if (nread == 0) { printf("client close the connection\n"); close(connfd); return -1; } else if (nread < 0) { perror("read error"); close(connfd); return -1; } else { printf("Receive client %s:%d on socket :%d :%s\n", inet_ntoa(((struct sockaddr_in*)clientaddr)->sin_addr),((struct sockaddr_in*)clientaddr)->sin_port,connfd,buf); #ifdef UDP_CONNECT_WAY sendto(connfd,buf,nread,0,(struct sockaddr*)clientaddr,socklen); #else //write(connfd, buf, nread);//\u54cd\u5e94\u5ba2\u6237\u7aef sendto(connfd,buf,nread,0,(struct sockaddr*)clientaddr,socklen); #endif } return 0; }
2)客服端
#include <unistd.h> #include <sys/types.h> /* basic system data types */ #include <sys/socket.h> /* basic socket definitions */ #include <netinet/in.h> /* sockaddr_in{} and other Internet defns */ #include <arpa/inet.h> /* inet(3) functions */ #include <netdb.h> /*gethostbyname function */ #include <stdlib.h> #include <errno.h> #include <stdio.h> #include <string.h> //#define UDP_CONNECT_WAY #define MAXLINE 1024 void handle(int sockfd,struct sockaddr *servaddr,socklen_t addrlen); int main(int argc, char **argv) { char * servInetAddr = "127.0.0.1"; int servPort = 6888; char buf[MAXLINE]; int connfd; struct sockaddr_in servaddr; struct sockaddr_in clientaddr; socklen_t length = 0; if (argc == 2) { servInetAddr = argv[1]; } if (argc == 3) { servInetAddr = argv[1]; servPort = atoi(argv[2]); } if (argc > 3) { printf("usage: echoclient <IPaddress> <Port>\n"); return -1; } #ifdef UDP_CONNECT_WAY connfd = socket(AF_INET, SOCK_DGRAM, 0); #else connfd = socket(AF_INET, SOCK_STREAM, 0); #endif //get buffer int rev_buff_size = 0; int send_buff_size = 0; socklen_t optlen; optlen = sizeof(rev_buff_size); if(0 == getsockopt(connfd, SOL_SOCKET, SO_RCVBUF, &rev_buff_size, &optlen)) { printf("default socket receive buff size %d bytes = %d KB \n",rev_buff_size,rev_buff_size/1024); } else { printf("errorNo. %d \n",errno); } optlen = sizeof(send_buff_size); if(0 == getsockopt(connfd, SOL_SOCKET, SO_SNDBUF, &send_buff_size, &optlen)) { printf("default socket send buff size %d bytes = %d KB \n",send_buff_size,send_buff_size/1024); } else { printf("errorNo. %d \n",errno); } bzero(&servaddr, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_port = htons(servPort); inet_pton(AF_INET, servInetAddr, &servaddr.sin_addr); //set buffer /* rev_buff_size = 130; if(setsockopt(connfd, SOL_SOCKET, SO_RCVBUF, (const char*)&rev_buff_size, sizeof(int))) { printf("Error: Set socket receive buffer failed.\n"); return -1; } send_buff_size = 512; if(setsockopt(connfd, SOL_SOCKET, SO_SNDBUF, (const char*)&send_buff_size, sizeof(int))) { printf("Error: Set socket send buffer failed.\n"); return -1; } //get optlen = sizeof(rev_buff_size); if(0 == getsockopt(connfd, SOL_SOCKET, SO_RCVBUF, &rev_buff_size, &optlen)) { printf("1default socket receive buff size %d bytes = %d KB \n",rev_buff_size,rev_buff_size/1024); } else { printf("errorNo. %d \n",errno); } optlen = sizeof(send_buff_size); if(0 == getsockopt(connfd, SOL_SOCKET, SO_SNDBUF, &send_buff_size, &optlen)) { printf("1default socket send buff size %d bytes = %d KB \n",send_buff_size,send_buff_size/1024); } else { printf("errorNo. %d \n",errno); } */ #if 0 int opt = 1; setsockopt(connfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)); bzero(&clientaddr, sizeof(clientaddr)); clientaddr.sin_family = AF_INET; clientaddr.sin_addr.s_addr = htonl (INADDR_ANY); clientaddr.sin_port = htons (9000); if (bind(connfd, (struct sockaddr *) &clientaddr, sizeof(struct sockaddr)) == -1) { perror("bind error"); return -1; } #endif #ifdef UDP_CONNECT if (connect(connfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0) { perror("connect error"); return -1; } #endif printf("welcome to echoclient\n"); length = sizeof(servaddr); handle(connfd,(struct sockaddr*)&servaddr,length); /* do it all */ close(connfd); printf("exit\n"); exit(0); } void handle(int sockfd,struct sockaddr *servaddr,socklen_t addrlen) { char sendline[MAXLINE], recvline[MAXLINE]; int n; for (;;) { if (fgets(sendline, MAXLINE, stdin) == NULL) { break;//read eof } /* //\u4e5f\u53ef\u4ee5\u4e0d\u7528\u6807\u51c6\u5e93\u7684\u7f13\u51b2\u6d41,\u76f4\u63a5\u4f7f\u7528\u7cfb\u7edf\u51fd\u6570\u65e0\u7f13\u5b58\u64cd\u4f5c if (read(STDIN_FILENO, sendline, MAXLINE) == 0) { break;//read eof } */ #ifdef UDP_CONNECT n = write(sockfd, sendline, strlen(sendline)); #else n = sendto(sockfd,sendline,strlen(sendline),0,(struct sockaddr*)servaddr,addrlen); #endif if(n>0) { printf("send size :%d \n",n); #ifdef UDP_CONNECT n = read(sockfd, recvline, MAXLINE); #else n = recvfrom(sockfd,recvline,sizeof(recvline),0,NULL,NULL); #endif if (n == 0) { printf("echoclient: server terminated prematurely\n"); break; } else if (-1 == n) { perror("read error!"); } else { write(STDOUT_FILENO, recvline, n); } } else { printf("send size :%d \n",n); perror("send fail!"); } //\u5982\u679c\u7528\u6807\u51c6\u5e93\u7684\u7f13\u5b58\u6d41\u8f93\u51fa\u6709\u65f6\u4f1a\u51fa\u73b0\u95ee\u9898 //fputs(recvline, stdout); } }
3)编译
//UDP server
g++ -DUDP_CONNECT_WAY -g echoserver.cpp -oechoserver
// client
g++ -DUDP_CONNECT_WAY -DUDP_CONNECT -gechoclient.cpp -o echoclien
or
g++ -DUDP_CONNECT_WAY -g echoclient.cpp -oechoclien
//////////TCP/////////////
//server
g++ -g echoserver.cpp -o echoserver
//client
g++ -DUDP_CONNECT -g echoclient.cpp -oechoclien
4)参考
http://www.cnblogs.com/ggjucheng/archive/2012/01/17/2324974.html