[riot-notifications] [RIOT-OS/RIOT] TCP client cannot send read only data (#16541)

kex2017 notifications at github.com
Thu Jun 10 03:53:36 CEST 2021


I use my board to test that UDP can send read-only data successfully, but if the same data is sent by TCP, it will fail.
Blocking when sending TCP data.
My code refers to the following file:
https://github.com/RIOT-OS/RIOT/blob/master/sys/include/net/sock/tcp.h#L191
https://github.com/RIOT-OS/RIOT/blob/master/tests/lwip_sock_tcp/main.c#L493


`**main.c**:
/*-----------------------------------------------------------------------------------------------*/
#include <stdio.h>

#include "tcp_udp_test.h"

int main(void)
{
    update_dev_net_info_to_eth();

    test_tcp_client();
    // test_udp_client();
    // test_tcp_server();

    return 0;
}
/*-----------------------------------------------------------------------------------------------*/

**tcp_udp_test.c**
/*-----------------------------------------------------------------------------------------------*/
#include "xtimer.h"
#include <stdio.h>

#include "lwip/ip4_addr.h"
#include "lwip/netif.h"

#include "net/sock/udp.h"

#include "net/sock/tcp.h"

#define DEV_IP_ADDR ("192.168.12.206")
#define DEV_NETMASK ("255.255.255.0")
#define DEV_GATEWAY ("192.168.12.1")

#define SERVER_IP_ADDR ("192.168.12.89")
#define SERVER_PORT (12345)

const char sendbuf[] = "Hello!";

/**********************************************************************/
void update_dev_net_info_to_eth(void)
{
    struct netif *netif = netif_get_by_index(netif_name_to_index("ET")); //or:netif_find("ET");
    ip4_addr_t ipaddr = {0}, netmask = {0}, gw = {0};

    ipaddr.addr = ipaddr_addr(DEV_IP_ADDR);
    netmask.addr = ipaddr_addr(DEV_NETMASK);
    gw.addr = ipaddr_addr(DEV_GATEWAY);

    netif_set_addr(netif, &ipaddr, &netmask, &gw);
}

/***********************test udp client***********************/
int test_udp_client(void)
{
    int ret = 0;
    sock_udp_ep_t local = SOCK_IPV4_EP_ANY;
    sock_udp_t sock;
    local.port = 0xabcd;

    if (sock_udp_create(&sock, &local, NULL, 0) < 0)
    {
        puts("Error creating UDP sock");
        return 1;
    }
    sock_udp_ep_t remote = {.family = AF_INET, .port = SERVER_PORT};
    ipv4_addr_from_str((ipv4_addr_t *)&remote.addr, SERVER_IP_ADDR);
    xtimer_sleep(2);

    while (1)
    {
        if ((ret = sock_udp_send(&sock, sendbuf, sizeof(sendbuf), &remote)) < 0)
        {
            printf("ret is %d\r\n", ret);
            puts("Error sending message");
            sock_udp_close(&sock);
            return 1;
        }
        xtimer_sleep(2);
    }
    return 0;
}

/***********************test tcp client***********************/
int test_tcp_client(void)
{
    int res = 0;
    sock_tcp_t sock;
    sock_tcp_ep_t remote = SOCK_IPV4_EP_ANY;

    remote.port = SERVER_PORT;

    ipv4_addr_from_str((ipv4_addr_t *)&remote.addr, SERVER_IP_ADDR);
    xtimer_sleep(2);

    if (sock_tcp_connect(&sock, &remote, 0, 0) < 0)
    {
        puts("Error connecting sock");
        return 1;
    }

    while (1)
    {
        puts("Sending \"Hello!\"");
        if ((res = sock_tcp_write(&sock, sendbuf, sizeof(sendbuf))) < 0)
        {
            puts("Errored on write");
        }
        puts("send over...");
    }

    return 0;
}

/***********************test tcp server***********************/
#define SOCK_QUEUE_LEN (1U)
sock_tcp_t sock_queue[SOCK_QUEUE_LEN];
uint8_t buf[128] = {0};

int test_tcp_server(void)
{
    sock_tcp_ep_t local = SOCK_IPV4_EP_ANY;
    sock_tcp_queue_t queue;

    local.port = SERVER_PORT;

    if (sock_tcp_listen(&queue, &local, sock_queue, SOCK_QUEUE_LEN, 0) < 0)
    {
        puts("Error creating listening queue");
        return 1;
    }
    puts("Listening on port 12345");
    while (1)
    {
        sock_tcp_t *sock;

        if (sock_tcp_accept(&queue, &sock, 0) < 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;
}
/*-----------------------------------------------------------------------------------------------*/
`
@maribu

-- 
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/issues/16541#issuecomment-858218917
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.riot-os.org/pipermail/notifications/attachments/20210609/cf56c90b/attachment.htm>


More information about the notifications mailing list