I am not able to create socket for TCP communication

Hello evebody,

                       I have problem with socket creation. I am trying to copy example of this tutorial:

https://academy.nordicsemi.com/courses/cellular-iot-fundamentals/lessons/lesson-3-cellular-fundamentals/topic/lesson-3-exercise-1/

What is my idea?

My idea is create a socket for TCP communication, and connected to a TCP server (my nrf9161-DK will work as Client). In another project, i tried to connect my client to server, but it didn't work cause of socket creation failure. If is not able to create socket in fact i am not able to connect to server. I tried to copy some examples, but they are not compatible with my device or not working. 

Now cause of some troubles with libraries i am not able to compile my project.

If I add to prj.conf:

CONFIG_NET_NATIVE=n
CONFIG_NET_SOCKETS_OFFLOAD=y
I am not able to compile my project. Zephyr missing some references (for some net_pkt.h functions maybe :-/ but this library is already included in project) in libraries, but all of them are part of SDK.
I am using SDK v2.5.1 (same as toolchain).
main.c:
#include <zephyr/kernel.h>
#include <zephyr/net/socket.h>
#include <zephyr/net/net_if.h>
#include <zephyr/net/net_ip.h>
#include <zephyr/net/net_pkt.h>
#include <zephyr/net/ppp.h>

int main(void)
{

   int sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_TCP);
   if (sock < 0) 
   {
     printk("SCOKET was not created\n");
   }
   else
   {
      printk("SCOKET created :-)\n");  
   }
   return 0;


}
prj.conf:

CONFIG_NETWORKING=y
CONFIG_NET_NATIVE=n
CONFIG_NET_DRIVERS=y
CONFIG_NET_SOCKETS=y
CONFIG_NET_SOCKETS_OFFLOAD=y

CONFIG_NET_IPV4=y
CONFIG_NET_L2_PPP=y  # Povolení PPP jako síťové vrstvy
CONFIG_NET_DHCPV4=y  # Automatická konfigurace IP adresy
CONFIG_NET_LOG=y     # Debugging síťového stack
CONFIG_NRF_MODEM_LIB=y
CONFIG_MODEM=y
CONFIG_NET_IPV6=y
CONFIG_NET_TCP=y
CONFIG_NET_UDP=y
CONFIG_NET_DHCPV4=y

Devicetree:

/*
 * Copyright (c) 2023 Nordic Semiconductor ASA
 *
 * SPDX-License-Identifier: Apache-2.0
 */

/dts-v1/;
#include <nordic/nrf9161ns_laca.dtsi>
#include "nrf9161dk_nrf9161_common.dtsi"

/ {
	chosen {
		zephyr,flash = &flash0;
		zephyr,sram = &sram0_ns;
		zephyr,code-partition = &slot0_ns_partition;
		zephyr,console = &uart0;
		zephyr,shell-uart = &uart0;
	};
};

&uart0 {

	status = "okay";
	current-speed = <115200>;
};
&uart1 {

	status = "okay";
	current-speed = <115200>;
};

&rtc0 {

	status = "okay";
};
&timer0 {

    status = "okay";          
    label = "TIMER_0";        
    interrupts = <1 0>;       
};

Could please somebody tell me what my prj.conf could include or what library could i add to my project? Is my devicetree ok?

Regards

Jaroslav Havel

Parents
  • Hi, 

    Removing CONFIG_NET_L2_PPP=y and CONFIG_NET_DHCPV4=y should make your code compile. It is not needed to use the modem library and the nRF modem. If you need these for other reasons you can enable CONFIG_NET_NATIVE, which is disabled in your prj.conf.
    The following should for instance be enough in many cases:

    # Network
    CONFIG_NETWORKING=y
    CONFIG_NET_NATIVE=n
    CONFIG_NET_SOCKETS=y
    CONFIG_NET_SOCKETS_OFFLOAD=y
    CONFIG_NET_IPV4=y
    CONFIG_NET_IPV6=y
    
    # Modem library
    CONFIG_NRF_MODEM_LIB=y

    I don't see any immediate issues with the device tree provided. The default device tree in zephyr should also be ok. Note that if you want to add changes to the nRF916DK device tree for your specific application, you can add a device tree overlay with what you need in your application folder, in "<your_app_path>/boards/nrf9161dk_nrf9161_ns.overlay". This is done in some of the cellular samples in NCS, for instance the modem trace flash sample. With this name the file is picked up automatically by the build system.

  • Thank you for reply, now I am able to create socket Blush.

    But I have another problem, if i try to connect my client to TCP server (I am able to establish connection with TCP server running on local IP using NAT, when I use Serial Console, AT command mode), using function connect I am not able to establish communication.

    struct sockaddr_in server_addr;
    server_addr.sin_family = AF_INET;
    server_addr.sin_port = htons(64999);  // Port serveru
    inet_pton(AF_INET, "98.58.85.213", &server_addr.sin_addr);
    int sock;
    sock= socket(server_addr.sin_family, SOCK_STREAM, IPPROTO_TCP);
    	           if (sock < 0) 
                 {
    		            printk("Failed to create TCP socket, error: %d\n", errno);
    
    	           }
                 else 
                 {
                    printk("SOCKET created, fine\n");
                    char ip_str[INET_ADDRSTRLEN];  
                    inet_ntop(AF_INET, &server_addr.sin_addr, ip_str, sizeof(ip_str));
                    printk("IP adress of server is: %s\n", ip_str);
                    vytvoreno=true;
     
    
                 } 
              // after creating socket with some delay
              int ret=connect(sock, (struct sockaddr *)&server_addr, sizeof(server_addr));
              if(ret==0)
              {
                 printf("Podarilo se pripojit\n");
                 pripojeno=true;
    
              }
              else
              {
                  printf("Nepodarilo se pripojit: %d\n", ret);
                  printf("Chyba pri vytvareni socketu: %d (%s)\n", errno, strerror(errno));
    
              }

    I get error: 114 (Network is unreachable), but i know that is reachable, i tested this with AT commands.

    Some tip what to do?

  • Hi, have you made sure that the modem is connected to the network? 

    You can use the LTE link controller library for this, or to do it manually with use of the AT monitor library (CONFIG_AT_MONITOR=y): 

    K_SEM_DEFINE(cereg_sem, 0, 1);
    AT_MONITOR(at_mon_cereg, "+CEREG", at_handler_cereg);
    
    static void at_handler_cereg(const char *notif)
    {
    	int err;
    
    	printk("+CEREG notification: %.*s\n", strlen(notif) - strlen("\r\n"), notif);
    
    	if (!strncmp(notif, "+CEREG: 1", strlen("+CEREG: 1")) ||
    	    !strncmp(notif, "+CEREG: 5", strlen("+CEREG: 5"))) {
    	    /* Registered to home network or roaming */
    		k_sem_give(&cereg_sem);
    	}
    }
    
    static int network_cfun(unsigned int mode)
    {
    	int err;
    
    	err = nrf_modem_at_printf("AT+CFUN=%u", mode);
    	if (err) {
    		printk("nrf_modem_at_printf failed, err %d\n", err);
    		return err;
    	}
    
    	if (mode == 1 || mode == 21) {
    		err = k_sem_take(&cereg_sem, K_MSEC(4000));
    		if (err) {
    			printk("Failed to register to network, err %d\n", err);
    		}
    	}
    }
    
    In main: 
    
    /* Subscribe to network registration status notifications */
    err = nrf_modem_at_printf("AT+CEREG=1");
    if (err) {
    	printk("Failed to register to network status notifications, err %d\n", err);
    	return err;
    }
    
    /* Connect to network */
    err = network_cfun(1);
    if (err) {
    	printk("Functional mode change failed, err %d\n", err);
    	return err;
    }
    
    /* Create socket and connect */ 
    ...

  • Hi,

    Could you provide complete application log which shows the "Network is unreachable" error?

    Best regards,
    Dejan

Reply Children
Related