Original:
Objective
This section uses basic Linux basic functions plus epoll calls to write a complete server and client example that can be run on Linux, and the functions of the client and service side are as follows:
The client reads a line from the standard input and sends it to the server
The server reads a line from the network and then outputs it to the client
The client receives a response from the server, outputting this line to the standard output
Service-side code
The code is as follows:
#include <unistd.h>#include<sys/types.h>/*Basic system data Types*/#include<sys/socket.h>/*Basic Socket Definitions*/#include<netinet/inch.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>#defineMaxepollsize 10000#defineMAXLINE 10240intHandleintconnfd);intSetnonblocking (intsockfd) { if(Fcntl (SOCKFD, F_SETFL, Fcntl (SOCKFD, F_GETFD,0)| O_nonblock) = =-1) { return-1; } return 0;}intMainintargcChar**argv) { intServport =6888; intListenq =1024x768; intLISTENFD, CONNFD, KDPFD, Nfds, N, nread, Curfds,acceptcount =0; structsockaddr_in servaddr, cliaddr; socklen_t Socklen=sizeof(structsockaddr_in); structepoll_event ev; structepoll_event Events[maxepollsize]; structRlimit RT; CharBuf[maxline]; /*set the maximum number of files per process allowed to open*/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); LISTENFD= Socket (Af_inet, Sock_stream,0); if(LISTENFD = =-1) {perror ("can ' t create socket file"); return-1; } intopt =1; SetSockOpt (LISTENFD, Sol_socket, SO_REUSEADDR,&opt,sizeof(opt)); if(Setnonblocking (LISTENFD) <0) {perror ("Setnonblock Error"); } if(Bind (LISTENFD, (structSOCKADDR *) &servaddr,sizeof(structSOCKADDR)) = =-1) {perror ("bind error"); return-1; } if(Listen (LISTENFD, listenq) = =-1) {perror ("Listen error"); return-1; } /*Create a Epoll handle and add the listener socket to the Epoll collection*/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 (;;) { /*wait for an event to occur*/Nfds= Epoll_wait (KDPFD, events, Curfds,-1); if(Nfds = =-1) {perror ("epoll_wait"); Continue; } /*Handle All Events*/ for(n =0; n < Nfds; ++N) {if(EVENTS[N].DATA.FD = =LISTENFD) {CONNFD= Accept (LISTENFD, (structSOCKADDR *) &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; } //Handling client Requests if(Handle (EVENTS[N].DATA.FD) <0) {epoll_ctl (KDPFD, Epoll_ctl_del, EVENTS[N].DATA.FD,&ev); Curfds--; }}} close (LISTENFD); return 0;}intHandleintCONNFD) { intnread; CharBuf[maxline]; Nread= Read (CONNFD, buf, MAXLINE);//Read Client socket stream if(Nread = =0) {printf ("client Close the connection\n"); Close (CONNFD); return-1; } if(Nread <0) {perror ("Read Error"); Close (CONNFD); return-1; } write (CONNFD, buf, nread);//Responding to clients return 0;}
Compiling and starting the service side
gcc epollserver.c-o epollserver. /epollserver
Client Code
As for the client can refer to this article Linux/unix server and client socket programming Get started instance echoclient example download compile
Linux IO multiplexing epoll network programming and Source code (RPM)