[riot-notifications] [RIOT-OS/RIOT] gnrc_sock_tcp: add gnrc sock tcp (#16494)

Simon Brummer notifications at github.com
Tue Aug 10 13:11:34 CEST 2021


> 
> 
> I tried to build a simple echo server, but it will only accept the first connection:
> 
> ```c
> #include "net/sock/tcp.h"
> 
> #define SOCK_TCP_QUEUE_SIZE (1)
> 
> static sock_tcp_queue_t sock_queue;
> static sock_tcp_t socks[SOCK_TCP_QUEUE_SIZE];
> static sock_tcp_t *client;
> 
> static char echo_stack[THREAD_STACKSIZE_DEFAULT +
>                        THREAD_EXTRA_STACKSIZE_PRINTF];
> // HACK
> #undef  SOCK_NO_TIMEOUT
> #define SOCK_NO_TIMEOUT (UINT32_MAX - 1)
> 
> static void *echo_thread(void *arg)
> {
>     (void)arg;
> 
>     static char rx_buf[64];
> 
>     while (1) {
>         ssize_t res = sock_tcp_accept(&sock_queue, &client, SOCK_NO_TIMEOUT);
>         if (res) {
>             printf("accept error: %s\n", strerror(-res));
>             continue;
>         }
> 
>         puts("connected");
> 
>         while (1) {
>             res = sock_tcp_read(client, rx_buf, sizeof(rx_buf), SOCK_NO_TIMEOUT);
>             if (res == -ETIMEDOUT) {
>                 continue;
>             }
>             if (res < 0) {
>                 printf("read error: %s\n", strerror(-res));
>                 break;
>             }
>             printf("%.*s", res, rx_buf);
>             res = sock_tcp_write(client, rx_buf, res);
>         }
> 
>         sock_tcp_disconnect(client);
>     }
> 
>     return NULL;
> }
> 
> int echo_start(void)
> {
>     sock_tcp_ep_t ep = SOCK_IPV6_EP_ANY;
>     ep.port = 23; // not htons(23); ?
> 
>     int res = sock_tcp_listen(&sock_queue, &ep, socks, ARRAY_SIZE(socks), 0);
>     if (res) {
>         return res;
>     }
> 
>     /* initiate echo server */
>     thread_create(echo_stack, sizeof(echo_stack),
>                   THREAD_PRIORITY_MAIN - 1, THREAD_CREATE_STACKTEST,
>                   echo_thread, NULL, "echo");
> 
>     return 0;
> }
> ```
> 
> After I disconnect (I have to kill `telnet` / `nc`) it will not accept another connection.

This might actually be standard behavior. Are you sure the sock_tcp_disconnect was finished before you try the next connection attempt? If you try to establish a new connection before sock_tcp_disconnect was finisched, the only TCB that could accept the connection is still in use. Depending on what side terminates the connection, sock_tcp_disconnect can take up to 2 minutes before it is usable again. This duration is defined by the TCP standard.
 

  

-- 
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub:
https://github.com/RIOT-OS/RIOT/pull/16494#issuecomment-895941158
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.riot-os.org/pipermail/notifications/attachments/20210810/3f35a86d/attachment.htm>


More information about the notifications mailing list