From f8d27ebd76de94e8c0d5f799c2b3c4728b9eeea0 Mon Sep 17 00:00:00 2001 From: yuanjm Date: Wed, 3 Feb 2021 11:27:06 +0800 Subject: [PATCH] examples: Make tcp_server example support tcp keepalive function --- examples/protocols/sockets/tcp_server/README.md | 6 ++++++ .../sockets/tcp_server/main/Kconfig.projbuild | 17 +++++++++++++++++ .../sockets/tcp_server/main/tcp_server.c | 14 +++++++++++++- 3 files changed, 36 insertions(+), 1 deletion(-) diff --git a/examples/protocols/sockets/tcp_server/README.md b/examples/protocols/sockets/tcp_server/README.md index a9d2a1de6a..d4324038a4 100644 --- a/examples/protocols/sockets/tcp_server/README.md +++ b/examples/protocols/sockets/tcp_server/README.md @@ -46,6 +46,12 @@ Set following parameters under Example Configuration Options: * Set `Port` number of the socket, that server example will create. +* Set `TCP keep-alive idle time(s)` value of TCP keep alive idle time. This time is the time between the last data transmission. + +* Set `TCP keep-alive interval time(s)` value of TCP keep alive interval time. This time is the interval time of keepalive probe packets. + +* Set `TCP keep-alive packet retry send counts` value of TCP keep alive packet retry send counts. This is the number of retries of the keepalive probe packet. + Configure Wi-Fi or Ethernet under "Example Connection Configuration" menu. See "Establishing Wi-Fi or Ethernet Connection" section in [examples/protocols/README.md](../../README.md) for more details. ## Build and Flash diff --git a/examples/protocols/sockets/tcp_server/main/Kconfig.projbuild b/examples/protocols/sockets/tcp_server/main/Kconfig.projbuild index 65f38224c6..967184d59e 100644 --- a/examples/protocols/sockets/tcp_server/main/Kconfig.projbuild +++ b/examples/protocols/sockets/tcp_server/main/Kconfig.projbuild @@ -16,4 +16,21 @@ menu "Example Configuration" help Local port the example server will listen on. + config EXAMPLE_KEEPALIVE_IDLE + int "TCP keep-alive idle time(s)" + default 5 + help + Keep-alive idle time. In idle time without receiving any data from peer, will send keep-alive probe packet + + config EXAMPLE_KEEPALIVE_INTERVAL + int "TCP keep-alive interval time(s)" + default 5 + help + Keep-alive probe packet interval time. + + config EXAMPLE_KEEPALIVE_COUNT + int "TCP keep-alive packet retry send counts" + default 3 + help + Keep-alive probe packet retry count. endmenu diff --git a/examples/protocols/sockets/tcp_server/main/tcp_server.c b/examples/protocols/sockets/tcp_server/main/tcp_server.c index 1d57c751e4..6f73e85901 100644 --- a/examples/protocols/sockets/tcp_server/main/tcp_server.c +++ b/examples/protocols/sockets/tcp_server/main/tcp_server.c @@ -24,7 +24,10 @@ #include -#define PORT CONFIG_EXAMPLE_PORT +#define PORT CONFIG_EXAMPLE_PORT +#define KEEPALIVE_IDLE CONFIG_EXAMPLE_KEEPALIVE_IDLE +#define KEEPALIVE_INTERVAL CONFIG_EXAMPLE_KEEPALIVE_INTERVAL +#define KEEPALIVE_COUNT CONFIG_EXAMPLE_KEEPALIVE_COUNT static const char *TAG = "example"; @@ -62,6 +65,10 @@ static void tcp_server_task(void *pvParameters) char addr_str[128]; int addr_family = (int)pvParameters; int ip_protocol = 0; + int keepAlive = 1; + int keepIdle = KEEPALIVE_IDLE; + int keepInterval = KEEPALIVE_INTERVAL; + int keepCount = KEEPALIVE_COUNT; struct sockaddr_storage dest_addr; if (addr_family == AF_INET) { @@ -123,6 +130,11 @@ static void tcp_server_task(void *pvParameters) break; } + // Set tcp keepalive option + setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, &keepAlive, sizeof(int)); + setsockopt(sock, IPPROTO_TCP, TCP_KEEPIDLE, &keepIdle, sizeof(int)); + setsockopt(sock, IPPROTO_TCP, TCP_KEEPINTVL, &keepInterval, sizeof(int)); + setsockopt(sock, IPPROTO_TCP, TCP_KEEPCNT, &keepCount, sizeof(int)); // Convert ip address to string if (source_addr.ss_family == PF_INET) { inet_ntoa_r(((struct sockaddr_in *)&source_addr)->sin_addr, addr_str, sizeof(addr_str) - 1);