See a lot of people write C code, the structure for the various components of the package, for example, use reproduced as follows Epoll sample:
//A simple echo server using Epoll in Linux // //2009-11-05 //2013-03-22: Changed a few questions , 1 is the/n format problem. 2 is to remove the original code accidentally added to the ET mode,//is only a simple schematic program. Decide or add recv/send when buffer offset/by sparkling // #include <sys/socket.h> #include <sys/ epoll.h> #include <netinet/in.h> #include <arpa/inet.h> #include <fcntl.h> #include <unistd.h> #include <stdio.h> #include <errno.h> #include < iostream> using namespace Std; #define Max_events struct myevent_s { int fd; void (*call_back) (int fd, int events, void *arg); int events; void *arg; int status; 1:in epoll Wait list, 0 not in Char buff[128]; recv data buffer int len, s_offset; long LAst_active; Last Active Time }; //Set Event void eventset (myevent_s *ev, int fd, void (*call_back) (int, int, void*), void *arg) {  ; ev->fd = FD; ev->call_back = Call_back; ev->events = 0; ev->arg = arg; ev->status = 0; bzero (ev->buff, sizeof (Ev->buff)); Ev->s_offset = 0; Ev->len = 0; ev->last_active = time (NULL); } //add/mod an event to Epoll void eventadd (int epollfd, int events, myevent_s *ev) {   ; struct Epoll_event Epv = {0, {0}}; int op; epv.data.ptr = EV; epv.events = ev->events = events; if (ev->status = = 1) { op = epoll_ctl_mod; } else{ op = epoll _ctl_add; ev->status = 1; } if (Epoll_ctl (EPOLLFD, op, EV->FD, &EPV) < 0) printf ("Event Add failed[fd=%d], evnets[%d]\n", EV->FD, events); Else printf ("Event Add ok[fd=%d", op=%d, Evnets[%0x]\n ", EV->FD, OP, events); } //Delete an event from Epoll void eventdel (int epollfd, myevent_s *ev) { &nbs P struct Epoll_event Epv = {0, {0}}; if (ev->status! = 1) return; epv.data.ptr = EV; ev->status = 0; epoll_ctl (EPOLLFD, Epoll_ctl_del, EV->FD, &EPV); } int G_EPOLLFD; myevent_s g_EVENTS[MAX_EVENTS+1]; G_events[max_events] is used by listen FD void recvdata (int fd, int Events, void *arg); void senddata (int fd, int events, void *arg); //accept new connections from clients void acceptconn (int fd, int events, void *arg) { &nbs p; struct sockaddr_in sin; socklen_t len = sizeof (struct sockaddr_in); int NFD, I; //Accept if (NFD = Accept (FD, (struct sockaddr*) &sin, &len)) = =-1) { if (errno! = Eagain && errno!) = eintr) { } printf ("%s:accept,%d", __func__, errno); return; } do   { for (i = 0; i < max_events; i++) & nbsp; { if (g_events[i]. Status = = 0) { break; } } if (i = = max_events) { printf ("%s:max Connection limit[%d]. ", __func__, max_events); break; } //Set Nonblocking int iret = 0; if (iret = Fcntl (NFD, F_SETFL, O_nonblock)) < 0) { printf ("%s:fcntl nonblocking failed:%d", __func__, Iret); break; } / /Add a read event for receive data Eventset (&g_events[i], NFD, RECV Data, &g_events[i]); Eventadd (G_EPOLLFD, Epollin, &g_events[i]); }while (0); printf ("New conn[%s:%d][time:%d], pos[%d]\n", Inet_ntoa (sin.sin_addr), Ntohs (Sin.sin_port), g_events[i].last_active, i); } //receive DATA void recvdata (int fd, int events, void *arg) { struct myevent_s *ev = (struct Mye vent_s*) Arg; int len; /Receive data len = recv (fd, Ev->buff+ev->len, sizeof (Ev->buff)- 1-ev->len, 0); eventdel (G_EPOLLFD, Ev); if (len > 0) { Ev->len + = len; Ev->buff[len] = ' + '; printf ("c[%d]:%s\n", FD, Ev->buff); //Change to send event Eventset (EV, FD, senddata, Ev); Eventadd (G_EPOLLFD, epollout, Ev); } Else if (len = = 0) { Close (EV->FD); printf ("[fd=%d] pos[%d], closed gracefully.\n", FD, ev-g_events); } Else { Close (EV->FD); printf ("recv[fd=%d] error[%d]:%s\n", FD, errno, Strerror (errno)); } //send data void senddata (int fd, int events, void *arg) { &nb sp; struct myevent_s *ev = (struct myevent_s*) arg; int len; //Send data len = Send (FD, Ev->buff + ev->s_offset, Ev->len- Ev->s_offset, 0); if (len > 0) { printf ("send[fd=%d], [%d<->%d]%s\n", FD, Len, Ev->len, Ev->buff); &nbsP Ev->s_offset + = len; if (ev->s_offset = = Ev->len) { //change to Receive event Eventdel (g_epollFd, Ev); eventset (EV, FD, recvdata, Ev); Eventadd (G_EPOLLFD, Epollin, Ev); } } Else { close (EV->FD); Eventdel (G_EPOLLFD, Ev); printf ("send[fd=%d] error[%d]\n", FD, errno); } } void initlistensocket (int epollfd, short port) { int listenfd = socket (af_inet, sock_stream, 0); fcntl (LISTENFD, f_ SETFL, O_nonblock); Set non-blocking printf ("Server Listen fd=%d\n", LISTENFD); Eventset (&g_events[max_events], LISTENFD, Acceptconn, &g_events[max_events]); //Add Listen socket eventadd (EPOLLFD, Epollin, &g_events[max_ EVENTS]); /Bind & listen sockaddr_in sin; bzero (&sin, sizeof (sin)); sin.sin_family = af_inet; sin.sin_addr.s_addr = Inaddr_any; Sin.sin_port = htons (port); bind (LISTENFD, (const sockaddr*) &sin, sizeof (sin)); Listen (LISTENFD, 5); &NBSP;} int main (int argc, char **argv) { unsigned short port = 12345; Default Port if (argc = = 2) { port = atoi ( ARGV[1]); } //create epoll G_EPOLLFD = epoll_create ( max_events); if (g_epollfd <= 0) printf ("Create Epoll failed.%d\n", G_EPOLLFD); /Create & Bind listen socket, and add to Epoll, set non-blocking I Nitlistensocket (G_EPOLLFD, Port); //Event loop struct epoll_event events[max_events]; printf ("Server running:port[%d]\n", port); int checkpos = 0; while (1) { //A simple timeout check here, Every time, better to use a mini-heap, and add timer event Long now = Time (NULL); for (int i = 0; i <; i++, checkpos++)//doesn ' t check listen fd { if (Checkpos = = max_events) Checkpos = 0; Recycle if (g_events[checkpos].status!) = 1) Continue; Long Duration = Now-g_events[checkpos]. last_active; if (duration >=)//60s timeout & nbsp; { Close (G_EVENTS[CHECKPOS].FD); printf ("[fd=%d ] timeout[%d--%d].\n ", G_EVENTS[CHECKPOS].FD, G_events[cheCkpos].last_active, now); Eventdel (g_ EPOLLFD, &g_events[checkpos]); } } //Wait for events to happen & nbsp; int FDS = epoll_wait (G_EPOLLFD, events, max_events, 1000); if (FDS < 0) { printf ("Epoll_wait error, exit\n"); break; } for (int i = 0; i < FDS; i++) { myevent_s *ev = (struct myevent_s*) Events[i].data.ptr; if (Events[i].events&epollin) & & (Ev->events&epollin))//Read Event { ev-> Call_back (EV->FD, events[i].events, Ev->arg); } if ((events[i].events&epollout) && (ev->events&epollout))/ /write event { Ev->call_back (EV->FD, Events[i]. events, Ev->arg); } } &NBSP;&NBSP;&NBSP;&NBsp } //free resource return 0; }
I was thinking, can't you just use C + + it?
Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.
Use C + + as a substitute for the use of C. Package!