Dear All,

I run a TCP server on esp32. I use a packet sender program to test whether
ESP32 receives multiple messages using TCP connection, I use official TCP
Sock example in offfical docs of RIOT OS. I use lwIP stack to establish TCP.

The code and makefile are as follows, i tried several ways to recive
multiple messages. However then a message receives after that " Error
accepting new sock "  is given. ;

Thanks for your help.


#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include "thread.h"
#include "xtimer.h"
#include "od.h"
#include "net/af.h"
#include "net/sock/tcp.h"
#include "net/ipv6.h"
#include "shell.h"
#include "net/af.h"
#include "net/sock/tcp.h"
#include "lwip/netif.h"
#define SOCK_QUEUE_LEN  (1U)
sock_tcp_t sock_queue;
uint8_t buf[128];

/* import "ifconfig" shell command, used for printing addresses */
//extern int _gnrc_netif_config(int argc, char **argv);


static int print_ip(void)
{

    for (struct netif *iface = netif_list; iface != NULL; iface =
iface->next) {
        printf("%s_%02u: ", iface->name, iface->num);

        char addrstr[IPV6_ADDR_MAX_STR_LEN];
        for (int i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++) {
            if (!ipv6_addr_is_unspecified((ipv6_addr_t
*)&iface->ip6_addr[i])) {
                printf(" inet6 %s\n", ipv6_addr_to_str(addrstr,
(ipv6_addr_t *)&iface->ip6_addr[i],
                                                       sizeof(addrstr)));
            }
        }

        puts("");
    }
    return 0;
}






int main(void)
{
    print_ip();
    sock_tcp_ep_t local = SOCK_IPV6_EP_ANY;
    sock_tcp_queue_t queue;
    local.port = 12345;
    if (sock_tcp_listen(&queue, &local, &sock_queue, 1,
SOCK_FLAGS_REUSE_EP) < 0) {
        puts("Error creating listening queue");
        return 1;
    }
    puts("Listening on port 12345");
  /*

    puts("Configured network interfaces2:");
    _gnrc_netif_config(0, NULL);

*/
    while (1) {
// puts("Configured network interfaces:");
// _gnrc_netif_config(0, NULL);
        sock_tcp_t *sock;
        if (sock_tcp_accept(&queue, &sock,  SOCK_NO_TIMEOUT) < 0) {
            puts("Error accepting new sock");
        }
        else {
            int read_res = 0;
            puts("Reading data");
            while (read_res >= 0) {
                read_res = sock_tcp_read(sock, &buf, sizeof(buf),
                                         SOCK_NO_TIMEOUT);
                if (read_res < 0) {
                    puts("Disconnected");
                    break;
                }
                else {
                    int write_res;
                    printf("Read: \"");
                    for (int i = 0; i < read_res; i++) {
                        printf("%c", buf[i]);
                    }
                    puts("\"");
                    if ((write_res = sock_tcp_write(sock, &buf,
                                                    read_res)) < 0) {
                        puts("Errored on write, finished server loop");
                        break;
                    }
                }
            }
            sock_tcp_disconnect(sock);
        }
    }
    sock_tcp_stop_listen(&queue);
    return 0;
}
---------------------------------------------------- Makefile
---------------------------------------------------------
# name of your application
APPLICATION = tcp-simple-server
# If no BOARD is found in the environment, use this default:
BOARD ?= esp32-wroom-32

# This has to be the absolute path to the RIOT base directory:
RIOTBASE ?= $(CURDIR)/../..

BOARD_INSUFFICIENT_MEMORY := arduino-duemilanove arduino-leonardo \
                             arduino-mega2560 arduino-nano \
                             arduino-uno blackpill bluepill calliope-mini \
                             chronos hifive1 i-nucleo-lrwan1 mega-xplained \
                             microbit msb-430 msb-430h \
                             nucleo-f031k6 nucleo-f042k6 nucleo-f303k8 \
                             nucleo-l031k6 nucleo-f030r8 nucleo-f070rb \
                             nucleo-f072rb nucleo-f103rb nucleo-f302r8 \
                             nucleo-f334r8 nucleo-l053r8 saml10-xpro \
                             saml11-xpro spark-core stm32f0discovery \
                             stm32l0538-disco telosb \
                             waspmote-pro wsn430-v1_3b wsn430-v1_4 z1



# module as used in tests/lwip
USEMODULE += lwip lwip_ipv6_autoconfig lwip_sock_ip lwip_netdev
USEMODULE += lwip_tcp lwip_sock_tcp
USEMODULE += ipv6_addr
USEMODULE += shell
USEMODULE += shell_commands
USEMODULE += ps
USEMODULE += od
USEMODULE += netdev_default

# additional modules for the application
USEMODULE += netstats_l2
USEMODULE += netstats_ipv6
USEMODULE += netstats_rpl
USEMODULE += esp_wifi


include $(RIOTBASE)/Makefile.include
_______________________________________________
users mailing list
[email protected]
https://lists.riot-os.org/mailman/listinfo/users

Reply via email to