apps/examples/netloop: Update network loopback example so that it can be used to test TCP KeepAlive.

This commit is contained in:
Gregory Nutt 2018-03-12 13:34:41 -06:00
parent b3f638b40c
commit 18c07e5713
4 changed files with 83 additions and 4 deletions

View File

@ -433,6 +433,13 @@ examples/flowc
A simple test of serial hardware flow control. A simple test of serial hardware flow control.
examples/ft80x
^^^^^^^^^^^^^^
This examples has ports of several FTDI demos for the FTDI/BridgeTek FT80x
GUI chip. As an example configuration, see
nuttx/configs/viewtool-stm32f107/ft80x/defconfig.
examples/fstest examples/fstest
^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^
@ -729,6 +736,7 @@ examples/json
examples/leds examples/leds
^^^^^^^^^^^^ ^^^^^^^^^^^^
This is a simple test of the board LED driver at nuttx/drivers/leds/userled_*.c. This is a simple test of the board LED driver at nuttx/drivers/leds/userled_*.c.
examples/lis2csh_reader examples/lis2csh_reader

View File

@ -13,6 +13,11 @@ config EXAMPLES_NETLOOP
if EXAMPLES_NETLOOP if EXAMPLES_NETLOOP
if NSH_BUILTIN_APPS if NSH_BUILTIN_APPS
config EXAMPLES_NETLOOP_KEEPALIVE
bool "Enable TCP KeepAlive test"
default n
depends on NET_TCP_KEEPALIVE
config EXAMPLES_NETLOOP_STACKSIZE config EXAMPLES_NETLOOP_STACKSIZE
int "Loopback test stack size" int "Loopback test stack size"
default 2048 default 2048

View File

@ -35,7 +35,7 @@
-include $(TOPDIR)/Make.defs -include $(TOPDIR)/Make.defs
# Device Driver poll()/select() Example # Network loopback Example
ASRCS = ASRCS =
CSRCS = lo_listener.c CSRCS = lo_listener.c
@ -48,7 +48,7 @@ APPNAME = netloop
PRIORITY = $(CONFIG_EXAMPLES_NETLOOP_PRIORITY) PRIORITY = $(CONFIG_EXAMPLES_NETLOOP_PRIORITY)
STACKSIZE = $(CONFIG_EXAMPLES_NETLOOP_STACKSIZE) STACKSIZE = $(CONFIG_EXAMPLES_NETLOOP_STACKSIZE)
CONFIG_XYZ_PROGNAME ?= poll$(EXEEXT) CONFIG_XYZ_PROGNAME ?= netloop$(EXEEXT)
PROGNAME = $(CONFIG_XYZ_PROGNAME) PROGNAME = $(CONFIG_XYZ_PROGNAME)
include $(APPDIR)/Application.mk include $(APPDIR)/Application.mk

View File

@ -41,6 +41,7 @@
#include <sys/types.h> #include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/time.h>
#include <stdio.h> #include <stdio.h>
#include <unistd.h> #include <unistd.h>
@ -53,6 +54,7 @@
#include <arpa/inet.h> #include <arpa/inet.h>
#include <netinet/in.h> #include <netinet/in.h>
#include <netinet/tcp.h>
#include "netloop.h" #include "netloop.h"
@ -82,6 +84,11 @@ static int lo_client(void)
int ret; int ret;
int i; int i;
#ifdef CONFIG_EXAMPLES_NETLOOP_KEEPALIVE
struct timeval tv;
int value;
#endif
/* Create a new TCP socket */ /* Create a new TCP socket */
sockfd = socket(PF_INET, SOCK_STREAM, 0); sockfd = socket(PF_INET, SOCK_STREAM, 0);
@ -92,6 +99,50 @@ static int lo_client(void)
return ret; return ret;
} }
#ifdef CONFIG_EXAMPLES_NETLOOP_KEEPALIVE
/* Enable TCP KeepAlive */
value = TRUE;
ret = setsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE, &value, sizeof(int));
if (ret < 0)
{
ret = -errno;
fprintf(stderr, "setsockopt(SO_KEEPALIVE) failed: %d\n", ret);
return ret;
}
tv.tv_sec = 5;
tv.tv_usec = 0;
ret = setsockopt(sockfd, SOL_TCP, TCP_KEEPIDLE, &tv, sizeof(struct timeval));
if (ret < 0)
{
ret = -errno;
fprintf(stderr, "setsockopt(TCP_KEEPIDLE) failed: %d\n", ret);
return ret;
}
tv.tv_sec = 1;
tv.tv_usec = 0;
ret = setsockopt(sockfd, SOL_TCP, TCP_KEEPINTVL, &tv, sizeof(struct timeval));
if (ret < 0)
{
ret = -errno;
fprintf(stderr, "setsockopt(TCP_KEEPIDLE) failed: %d\n", ret);
return ret;
}
value = 3;
ret = setsockopt(sockfd, SOL_TCP, TCP_KEEPCNT, &value, sizeof(int));
if (ret < 0)
{
ret = -errno;
fprintf(stderr, "setsockopt(SO_KEEPALIVE) failed: %d\n", ret);
return ret;
}
#endif
/* Connect the socket to the server */ /* Connect the socket to the server */
myaddr.sin_family = AF_INET; myaddr.sin_family = AF_INET;
@ -105,6 +156,7 @@ static int lo_client(void)
printf("lo_client: connect failure: %d\n", ret); printf("lo_client: connect failure: %d\n", ret);
goto errout_with_socket; goto errout_with_socket;
} }
printf("lo_client: Connected\n"); printf("lo_client: Connected\n");
/* Then send and receive messages */ /* Then send and receive messages */
@ -163,8 +215,22 @@ static int lo_client(void)
goto errout_with_socket; goto errout_with_socket;
} }
#ifdef CONFIG_EXAMPLES_NETLOOP_KEEPALIVE
/* Send four messages, then wait a longer amount of time ... longer than
* TCP_KEEPIDLE + TCP_KEEPINTVL * TCP_KEEPCNT = 5 + 3 * 1 = 8 seconds.
*/
if (i != 0 && (i & 3) == 0)
{
printf("lo_client: Long sleep\n");
sleep(12);
}
else
#endif
{
printf("lo_client: Sleeping\n"); printf("lo_client: Sleeping\n");
sleep(8); sleep(6);
}
} }
close(sockfd); close(sockfd);