mirror of https://github.com/F-Stack/f-stack.git
helloword sever demo support IPv6.
This commit is contained in:
parent
1e14958312
commit
35fdfcdb22
|
@ -21,6 +21,9 @@ struct kevent events[MAX_EVENTS];
|
||||||
/* kq */
|
/* kq */
|
||||||
int kq;
|
int kq;
|
||||||
int sockfd;
|
int sockfd;
|
||||||
|
#ifdef INET6
|
||||||
|
int sockfd6;
|
||||||
|
#endif
|
||||||
|
|
||||||
char html[] =
|
char html[] =
|
||||||
"HTTP/1.1 200 OK\r\n"
|
"HTTP/1.1 200 OK\r\n"
|
||||||
|
@ -68,10 +71,14 @@ int loop(void *arg)
|
||||||
if (event.flags & EV_EOF) {
|
if (event.flags & EV_EOF) {
|
||||||
/* Simply close socket */
|
/* Simply close socket */
|
||||||
ff_close(clientfd);
|
ff_close(clientfd);
|
||||||
|
#ifdef INET6
|
||||||
|
} else if (clientfd == sockfd || clientfd == sockfd6) {
|
||||||
|
#else
|
||||||
} else if (clientfd == sockfd) {
|
} else if (clientfd == sockfd) {
|
||||||
|
#endif
|
||||||
int available = (int)event.data;
|
int available = (int)event.data;
|
||||||
do {
|
do {
|
||||||
int nclientfd = ff_accept(sockfd, NULL, NULL);
|
int nclientfd = ff_accept(clientfd, NULL, NULL);
|
||||||
if (nclientfd < 0) {
|
if (nclientfd < 0) {
|
||||||
printf("ff_accept failed:%d, %s\n", errno,
|
printf("ff_accept failed:%d, %s\n", errno,
|
||||||
strerror(errno));
|
strerror(errno));
|
||||||
|
@ -104,10 +111,11 @@ int main(int argc, char * argv[])
|
||||||
{
|
{
|
||||||
ff_init(argc, argv);
|
ff_init(argc, argv);
|
||||||
|
|
||||||
|
assert((kq = ff_kqueue()) > 0);
|
||||||
|
|
||||||
sockfd = ff_socket(AF_INET, SOCK_STREAM, 0);
|
sockfd = ff_socket(AF_INET, SOCK_STREAM, 0);
|
||||||
printf("sockfd:%d\n", sockfd);
|
|
||||||
if (sockfd < 0) {
|
if (sockfd < 0) {
|
||||||
printf("ff_socket failed\n");
|
printf("ff_socket failed, sockfd:%d, errno:%d, %s\n", sockfd, errno, strerror(errno));
|
||||||
exit(1);
|
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));
|
int ret = ff_bind(sockfd, (struct linux_sockaddr *)&my_addr, sizeof(my_addr));
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
printf("ff_bind failed\n");
|
printf("ff_bind failed, sockfd:%d, errno:%d, %s\n", sockfd, errno, strerror(errno));
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = ff_listen(sockfd, MAX_EVENTS);
|
ret = ff_listen(sockfd, MAX_EVENTS);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
printf("ff_listen failed\n");
|
printf("ff_listen failed, sockfd:%d, errno:%d, %s\n", sockfd, errno, strerror(errno));
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
EV_SET(&kevSet, sockfd, EVFILT_READ, EV_ADD, 0, MAX_EVENTS, NULL);
|
EV_SET(&kevSet, sockfd, EVFILT_READ, EV_ADD, 0, MAX_EVENTS, NULL);
|
||||||
|
|
||||||
assert((kq = ff_kqueue()) > 0);
|
|
||||||
|
|
||||||
/* Update kqueue */
|
/* Update kqueue */
|
||||||
ff_kevent(kq, &kevSet, 1, NULL, 0, NULL);
|
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);
|
ff_run(loop, NULL);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue