Example: keep run when accept failed.

This commit is contained in:
logwang 2017-11-21 11:42:09 +08:00
parent f17ba62bb6
commit c7fe235229
2 changed files with 26 additions and 23 deletions

View File

@ -1,6 +1,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <strings.h>
#include <sys/types.h>
#include <sys/socket.h>
@ -72,8 +73,10 @@ int loop(void *arg)
//printf("A client has left the server...,fd:%d\n", clientfd);
} else if (clientfd == sockfd) {
int nclientfd = ff_accept(sockfd, NULL, NULL);
assert(nclientfd > 0);
if (nclientfd < 0) {
printf("ff_accept failed:%d, %s\n", errno, strerror(errno));
continue;
}
/* Add to event list */
kevSet.data = 0;
@ -145,5 +148,3 @@ int main(int argc, char * argv[])
ff_run(loop, NULL);
return 0;
}

View File

@ -66,31 +66,35 @@ int loop(void *arg)
/* Handle new connect */
if (events[i].data.fd == sockfd) {
int nclientfd = ff_accept(sockfd, NULL, NULL);
assert(nclientfd > 0);
if (nclientfd < 0) {
printf("ff_accept failed:%d, %s\n", errno, strerror(errno));
continue;
}
/* Add to event list */
ev.data.fd = nclientfd;
ev.events = EPOLLIN;
assert(ff_epoll_ctl(epfd, EPOLL_CTL_ADD, nclientfd, &ev) == 0);
//fprintf(stderr, "A new client connected to the server..., fd:%d\n", nclientfd);
ev.data.fd = nclientfd;
ev.events = EPOLLIN;
assert(ff_epoll_ctl(epfd, EPOLL_CTL_ADD, nclientfd, &ev) == 0);
//printf("A new client connected to the server..., fd:%d\n", nclientfd);
} else {
if (events[i].events & EPOLLERR ) {
/* Simply close socket */
ff_epoll_ctl(epfd, EPOLL_CTL_DEL, events[i].data.fd, NULL);
ff_epoll_ctl(epfd, EPOLL_CTL_DEL, events[i].data.fd, NULL);
ff_close(events[i].data.fd);
//fprintf(stderr, "A client has left the server...,fd:%d\n", events[i].data.fd);
//printf("A client has left the server...,fd:%d\n", events[i].data.fd);
} else if (events[i].events & EPOLLIN) {
char buf[256];
size_t readlen = ff_read( events[i].data.fd, buf, sizeof(buf));
//fprintf(stderr, "bytes are available to read..., readlen:%d, fd:%d\n", readlen, events[i].data.fd);
if(readlen > 0){
//printf("bytes are available to read..., readlen:%d, fd:%d\n", readlen, events[i].data.fd);
if(readlen > 0) {
ff_write( events[i].data.fd, html, sizeof(html));
} else {
ff_epoll_ctl(epfd, EPOLL_CTL_DEL, events[i].data.fd, NULL);
ff_close( events[i].data.fd);
//fprintf(stderr, "A client has left the server...,fd:%d\n", events[i].data.fd);
}
} else {
ff_epoll_ctl(epfd, EPOLL_CTL_DEL, events[i].data.fd, NULL);
ff_close( events[i].data.fd);
//printf("A client has left the server...,fd:%d\n", events[i].data.fd);
}
} else {
fprintf(stderr, "unknown event: %8.8X\n", events[i].events);
printf("unknown event: %8.8X\n", events[i].events);
}
}
}
@ -135,5 +139,3 @@ int main(int argc, char * argv[])
ff_run(loop, NULL);
return 0;
}