This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

nRF9160 TCP connection, sending over LTE-M

Hello,

I'm trying to setup a TCP connection to a remote TCP echo server (echo.u-blox.com : 195.34.89.241, port 7), for testing.

The socket is being opened, and I receive a welcome message. However, it seems as if the connection is closed afterwards:

Sent: AT+CFUN=1
Received: OK

tcp_socket: 2
connect to 195.34.89.241 err: 0
Received: U-Blox AG TCP/UDP test service
 [31]
Sent: Hello U-Blox [Bytes: 13] [Error: 0]
Receive failed: [Bytes: 0] [Error: 0]
Send failed: 54
Receive failed: [Bytes: -1] [Error: 54]


With UDP, everything is working just fine. What am I missing?

The proj.conf:
#
# Copyright (c) 2019 Nordic Semiconductor ASA
#
# SPDX-License-Identifier: LicenseRef-BSD-5-Clause-Nordic
#
# General config
CONFIG_ASSERT=y

# Network
CONFIG_NETWORKING=y
CONFIG_NET_NATIVE=n
CONFIG_NET_SOCKETS=y
CONFIG_NET_SOCKETS_OFFLOAD=y
CONFIG_NET_SOCKETS_POSIX_NAMES=y

# BSD library
CONFIG_BSD_LIBRARY=y

# AT host library
CONFIG_AT_HOST_LIBRARY=y
CONFIG_UART_INTERRUPT_DRIVEN=y

# Stacks and heaps
CONFIG_MAIN_STACK_SIZE=4096
CONFIG_HEAP_MEM_POOL_SIZE=32768

# Testing changes
CONFIG_GPIO=y

The code to set it up is as follows:

  printk("Starting socket\n");

  int at_socket_fd = socket(AF_LTE, SOCK_DGRAM, NPROTO_AT);

  if (at_socket_fd < 0)
  {
    printk("Socket err: %d, errno: %d\r\n", at_socket_fd, errno);
  }

  char *initCmds[1] = { "AT+CFUN=1" };
  send(at_socket_fd, initCmds[0], strlen(initCmds[0]), 0);
  printk("Sent: %s\n", initCmds[0]);

  recv(at_socket_fd, recv_buf, sizeof(recv_buf), 0);
  printk("Received: %s\n", recv_buf);

  k_msleep(15000);
  
  int tcp_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); // SOCK_DGRAM, IPPROTO_UDP
  printk("tcp_socket: %i\n", tcp_socket);

  struct sockaddr_in remote_addr;
  remote_addr.sin_family = AF_INET;
  remote_addr.sin_port = htons(TCP_PORT);
  inet_pton(AF_INET, TCP_HOST, &(remote_addr.sin_addr));
  int err = connect(tcp_socket, (struct sockaddr *) &remote_addr, sizeof(remote_addr));
  printk("connect to %s err: %i\n", TCP_HOST, err);

  int bytes_received = recv(tcp_socket, recv_buf, sizeof(recv_buf), 0);
  if (bytes_received > 0)
  {
    printk("Received: %s [%i]\n", recv_buf, bytes_received);
  }
  else
  {
    printk("Receive failed: %i\n", errno);
  }

  for (;;)
  {
    memset(recv_buf, 0, sizeof(recv_buf));

    uint8_t cmd[] = "Hello U-Blox";
    
    int bytes_sent = send(tcp_socket, cmd, sizeof(cmd), 0);
    if (bytes_sent < 0)
    {
      printk("Send failed: %i\n", errno);
    }
    else
    {
      printk("Sent: %s [Bytes: %i] [Error: %i]\n", cmd, bytes_sent, errno);
    }

    bytes_received = recv(tcp_socket, recv_buf, sizeof(recv_buf), 0);
    if (bytes_received > 0)
    {
      printk("Received: %s [Bytes: %i]\n", recv_buf, bytes_received);
    }
    else
    {
      printk("Receive failed: [Bytes: %i] [Error: %i]\n", bytes_received, errno);
    }

    k_msleep(5000);
  }

Parents Reply Children
  • Hello, 

    My apologies for the late reply. Difficult to see what you have altered in the main post, it's better to add changes as a comment to the thread. Can you please add prj.conf and main.c ?

    newUser said:
    ECONNRESET "Connection reset by peer":

    A connection reset by peer message means that the site you are connected to has reset the connection. This is usually caused by a high amount of traffic on the site, but may be caused by a server error as well.

    What other servers did you try?

  • Hi, no problem! Thank you for getting back to me.

    The current status is that with UDP, everything works fine. With TCP, the server's welcome message is received, but after that no communication is working anymore. The send() function still returns 0 (success) and the expected number of written bytes (13), but the recv() fails (we would expected the 'Hello U-Blox' message to be echoed):

    Sent: AT+CFUN=1
    Received: OK
    
    tcp_socket: 2
    connect to 195.34.89.241 err: 0
    Received: U-Blox AG TCP/UDP test service
     [31]
    Sent: Hello U-Blox [Bytes: 13] [Error: 0]
    Receive failed: [Bytes: 0] [Error: 0]
    Send failed: 54
    Receive failed: [Bytes: -1] [Error: 54]

    When I test this server with other embedded devices, it is working just fine for both UDP and TCP.
    Our own remote management server is also working just fine with these other devices, but we observe the above behavior when connecting from the nRF9160 board.

    The code producing this output:

    #include <zephyr.h>
    #include <device.h>
    #include <drivers/gpio.h>
    
    #include <stdint.h>
    
    #include <net/socket.h>
    
    #include "test.h"
    
    #define TEST_THREAD_PRIORITY 7
    
    #define TCP_HOST "195.34.89.241"
    #define TCP_PORT 7
    #define RECV_BUF_SIZE 128
    
    static uint8_t recv_buf[RECV_BUF_SIZE];
    
    K_THREAD_STACK_DEFINE(testThreadStack, 2048);
    struct k_thread testThread;
    
    static void TestThreadFxn (void);
    
    void TestCreateThread (void)
    {
      k_thread_create(&testThread, testThreadStack,
                      K_THREAD_STACK_SIZEOF(testThreadStack),
                      (k_thread_entry_t) TestThreadFxn,
                      NULL, NULL, NULL,
                      TEST_THREAD_PRIORITY, 0, K_NO_WAIT);
    }
    
    static void TestThreadFxn (void)
    {
      printk("Starting socket\n");
    
      int at_socket_fd = socket(AF_LTE, SOCK_DGRAM, NPROTO_AT);
    
      if (at_socket_fd < 0)
      {
        printk("Socket err: %d, errno: %d\r\n", at_socket_fd, errno);
      }
    
      char *initCmds[1] = { "AT+CFUN=1" };
      send(at_socket_fd, initCmds[0], strlen(initCmds[0]), 0);
      printk("Sent: %s\n", initCmds[0]);
    
      recv(at_socket_fd, recv_buf, sizeof(recv_buf), 0);
      printk("Received: %s\n", recv_buf);
    
      k_msleep(20000);
    
      int tcp_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); // SOCK_DGRAM, IPPROTO_UDP
      printk("tcp_socket: %i\n", tcp_socket);
    
      struct sockaddr_in remote_addr;
      remote_addr.sin_family = AF_INET;
      remote_addr.sin_port = htons(TCP_PORT);
      inet_pton(AF_INET, TCP_HOST, &(remote_addr.sin_addr));
    
      int err = connect(tcp_socket, (struct sockaddr *) &remote_addr, sizeof(remote_addr));
      printk("connect to %s err: %i\n", TCP_HOST, err);
    
      int bytes_received = recv(tcp_socket, recv_buf, sizeof(recv_buf), 0);
      if (bytes_received > 0)
      {
        printk("Received: %s [%i]\n", recv_buf, bytes_received);
      }
      else
      {
        printk("Receive failed: %i\n", errno);
      }
    
      for (;;)
      {
        memset(recv_buf, 0, sizeof(recv_buf));
    
        uint8_t cmd[] = "Hello U-Blox";
    
        int bytes_sent = send(tcp_socket, cmd, sizeof(cmd), 0);
        if (bytes_sent < 0)
        {
          printk("Send failed: %i\n", errno);
        }
        else
        {
          printk("Sent: %s [Bytes: %i] [Error: %i]\n", cmd, bytes_sent, errno);
        }
    
        int bytes_received = recv(tcp_socket, recv_buf, sizeof(recv_buf), 0);
        if (bytes_received > 0)
        {
          printk("Received: %s [Bytes: %i]\n", recv_buf, bytes_received);
        }
        else
        {
          printk("Receive failed: [Bytes: %i] [Error: %i]\n", bytes_received, errno);
        }
    
        k_msleep(5000);
      }
    }

    proj.conf:

    #
    # Copyright (c) 2019 Nordic Semiconductor ASA
    #
    # SPDX-License-Identifier: LicenseRef-BSD-5-Clause-Nordic
    #
    # General config
    CONFIG_ASSERT=y
    
    # Network
    CONFIG_NETWORKING=y
    CONFIG_NET_NATIVE=n
    CONFIG_NET_SOCKETS=y
    CONFIG_NET_SOCKETS_OFFLOAD=y
    CONFIG_NET_SOCKETS_POSIX_NAMES=y
    
    # BSD library
    CONFIG_BSD_LIBRARY=y
    
    # AT host library
    CONFIG_AT_HOST_LIBRARY=y
    CONFIG_UART_INTERRUPT_DRIVEN=y
    
    # Stacks and heaps
    CONFIG_MAIN_STACK_SIZE=4096
    CONFIG_HEAP_MEM_POOL_SIZE=32768
    
    # Testing changes
    CONFIG_GPIO=y

  • Thank you guys. I can also confirm that the U-Blox test server is no longer working according to their spec.

    I will setup our own server again and report back the latest results, perhaps the code is already working.

  • We are now successfully communicating with our own server. Thank you for your support and patience.

Related