nRF91 as UDP server

Hi,

I'm try to set up a UDP server on a nRF9151dk, and send data from a remote pc to the devkit. I have tested my sim and networking works correctly, I am able to use coap_client on the board to make requests to a coap server.

void udp_thread(void)
{
    printk("Starting UDP server\n");
    int fd = socket(AF_INET, SOCK_DGRAM, 0);

    if (fd < 0) {
        printk("socket");
    }

    struct sockaddr_in sin, cin;
    socklen_t cin_len = sizeof(cin);

    inet_pton(AF_INET, "100.xx.xx.1", &sin.sin_addr); // my sim static ip, also tried INADDR_ANY
    sin.sin_family = AF_INET;
    sin.sin_port = htons(4242);

    if (bind(fd, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
        printk("bind");
    }

    printk("Listening on UDP port %d\n", 4242);

    while (true) {
        ssize_t len = recvfrom(fd, buf, BUF_SIZE, 0, &cin, &cin_len);

        if (len < 0) {
            printk("recv");
        }

        printk("Received %ld bytes\n", (long)len);
    }
}

int main(void) {
    modem_init();
    k_sleep(K_SECONDS(15));
    udp_thread();
}


This function is straight from one of the examples, on the client side I use python to send a message over UDP.

Something like this:

from socket import *
s = socket(AF_INET, SOCK_DGRAM)
s.sendto(b'hello', ("100.xx.xx.1", 4242))


Any ideas what the issue could be? The relevant config:

# Main stack and heap size
CONFIG_MAIN_STACK_SIZE=4096
CONFIG_HEAP_MEM_POOL_SIZE=30000

# RTT based logging
CONFIG_LOG=y
CONFIG_LOG_MODE_IMMEDIATE=y 
CONFIG_USE_SEGGER_RTT=y
CONFIG_SEGGER_RTT_BUFFER_SIZE_UP=4096
CONFIG_SEGGER_RTT_BUFFER_SIZE_DOWN=360

# LTE, modem & soft-SIM configuration
CONFIG_NRF_MODEM_LIB=y
CONFIG_MODEM_INFO=y
CONFIG_MODEM_KEY_MGMT=y
CONFIG_LTE_LINK_CONTROL=y
CONFIG_LTE_LC_PSM_MODULE=y
CONFIG_LTE_LC_EDRX_MODULE=y
CONFIG_SOFTSIM=y
CONFIG_SOFTSIM_LOG_LEVEL_INF=y

# Networking
CONFIG_NETWORKING=y
CONFIG_NET_NATIVE=n
CONFIG_NET_SOCKETS=y
CONFIG_NET_SOCKETS_OFFLOAD=y
CONFIG_NET_SOCKETS_POSIX_NAMES=y
CONFIG_COAP=y

Probably something dumb on my end, but I can't seem to spot it...

Edit: Forgot to mention that my issue is I do not receive any data on the devkit (acting as UDP server). I do not use use any encryption either.

Thanks!

Parents
  • Any ideas what the issue could be?

    You didn't mention the issue.

    Using cellular IP is commonly using something as an "IP breakout", where the cellular IP-traffic is feed into the public IP.

    That comes usually with a NAT (Network Address Translation). It works, if the device initiates the traffic, that allocates an mapping in the NAT and from that on your server may send something back. If no messages are exchanged, the NAT will remove the mapping after an timeout. See my article NAT .

    Therefore you have two options:

    Permanently exchange messages, e.g. every 30s

    Use a "private network", that may come as VPN or IPsec. In some cases it comes with extra costs at your provider.

    By the way, if you don't want to keep the device in IDLE (energy consumption), you will need eDRX.

  • Thanks for your reply. I forgot to mention that my issue is I receive no data on the nrf91 devkit.

    Coap+DTLS where the nrf91 is the client works perfectly fine. I need a web application to be able to initiate some action on the devkit, so I was trying to setup a UDP server on the devkit - here I receive no data

  • As I explained, there is usually a NAT between the cellular ip-network and the public ip-network.

    Pretty similar to a DSL-internet-router. There it's also easy to use clients, but if you want to run an server in your home network, then you need "port forwarding" (for cellular that's not the way). 

    You will need either "always traffic" (energy + data volume) or something as a VPN or ip-tunel or whatever spans then a private ip-network between your device and server. 

Reply
  • As I explained, there is usually a NAT between the cellular ip-network and the public ip-network.

    Pretty similar to a DSL-internet-router. There it's also easy to use clients, but if you want to run an server in your home network, then you need "port forwarding" (for cellular that's not the way). 

    You will need either "always traffic" (energy + data volume) or something as a VPN or ip-tunel or whatever spans then a private ip-network between your device and server. 

Children
No Data
Related