From 35fdfcdb22b81cbcd2af22ab0ec030b6d5aec426 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?fengbojiang=28=E5=A7=9C=E5=87=A4=E6=B3=A2=29?= Date: Thu, 8 Aug 2019 22:03:57 +0800 Subject: [PATCH] helloword sever demo support IPv6. --- example/main.c | 52 +++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 43 insertions(+), 9 deletions(-) diff --git a/example/main.c b/example/main.c index 73752bd98..78ba53b37 100644 --- a/example/main.c +++ b/example/main.c @@ -21,6 +21,9 @@ struct kevent events[MAX_EVENTS]; /* kq */ int kq; int sockfd; +#ifdef INET6 +int sockfd6; +#endif char html[] = "HTTP/1.1 200 OK\r\n" @@ -68,10 +71,14 @@ int loop(void *arg) if (event.flags & EV_EOF) { /* Simply close socket */ ff_close(clientfd); +#ifdef INET6 + } else if (clientfd == sockfd || clientfd == sockfd6) { +#else } else if (clientfd == sockfd) { +#endif int available = (int)event.data; do { - int nclientfd = ff_accept(sockfd, NULL, NULL); + int nclientfd = ff_accept(clientfd, NULL, NULL); if (nclientfd < 0) { printf("ff_accept failed:%d, %s\n", errno, strerror(errno)); @@ -104,10 +111,11 @@ int main(int argc, char * argv[]) { ff_init(argc, argv); + assert((kq = ff_kqueue()) > 0); + sockfd = ff_socket(AF_INET, SOCK_STREAM, 0); - printf("sockfd:%d\n", sockfd); if (sockfd < 0) { - printf("ff_socket failed\n"); + printf("ff_socket failed, sockfd:%d, errno:%d, %s\n", sockfd, errno, strerror(errno)); exit(1); } @@ -119,23 +127,49 @@ int main(int argc, char * argv[]) int ret = ff_bind(sockfd, (struct linux_sockaddr *)&my_addr, sizeof(my_addr)); if (ret < 0) { - printf("ff_bind failed\n"); + printf("ff_bind failed, sockfd:%d, errno:%d, %s\n", sockfd, errno, strerror(errno)); exit(1); } - ret = ff_listen(sockfd, MAX_EVENTS); + ret = ff_listen(sockfd, MAX_EVENTS); if (ret < 0) { - printf("ff_listen failed\n"); + printf("ff_listen failed, sockfd:%d, errno:%d, %s\n", sockfd, errno, strerror(errno)); exit(1); } EV_SET(&kevSet, sockfd, EVFILT_READ, EV_ADD, 0, MAX_EVENTS, NULL); - - assert((kq = ff_kqueue()) > 0); - /* Update kqueue */ ff_kevent(kq, &kevSet, 1, NULL, 0, NULL); +#ifdef INET6 + sockfd6 = ff_socket(AF_INET6, SOCK_STREAM, 0); + if (sockfd6 < 0) { + printf("ff_socket failed, sockfd6:%d, errno:%d, %s\n", sockfd6, errno, strerror(errno)); + exit(1); + } + + struct sockaddr_in6 my_addr6; + bzero(&my_addr6, sizeof(my_addr6)); + my_addr6.sin6_family = AF_INET6; + my_addr6.sin6_port = htons(80); + my_addr6.sin6_addr = in6addr_any; + + ret = ff_bind(sockfd6, (struct linux_sockaddr *)&my_addr6, sizeof(my_addr6)); + if (ret < 0) { + printf("ff_bind failed, sockfd6:%d, errno:%d, %s\n", sockfd6, errno, strerror(errno)); + exit(1); + } + + ret = ff_listen(sockfd6, MAX_EVENTS); + if (ret < 0) { + printf("ff_listen failed, sockfd6:%d, errno:%d, %s\n", sockfd6, errno, strerror(errno)); + exit(1); + } + + EV_SET(&kevSet, sockfd6, EVFILT_READ, EV_ADD, 0, MAX_EVENTS, NULL); + ff_kevent(kq, &kevSet, 1, NULL, 0, NULL); +#endif + ff_run(loop, NULL); return 0; }