[riot-notifications] [RIOT-OS/RIOT] start a udp server that listens only to one client and port (#11894)

Thomas Stilwell notifications at github.com
Sat Jul 27 22:38:46 CEST 2019


> 1) Is it possible to force the server listening to only one ipv6 address and its port?

Yes. Populate a `sock_udp_ep_t` for the remote endpoint and pass it to `sock_udp_create()` in place of `NULL`. IIRC both the port and address must be specified and the address may not be `::`. When a datagram is received that doesn't match `remote`, `sock_udp_recv()` will return `-EPROTO`.
```c
    sock_udp_ep_t remote = SOCK_IPV6_EP_ANY;
    remote.port = 9000;
    char remote_ip[IPV6_ADDR_MAX_STR_LEN] = "2001:db8:ffff:ffff::2";
    ipv6_addr_from_str((ipv6_addr_t *)&remote.addr.ipv6, remote_ip);

    int res;
    sock_udp_t sock;
    if ((res = sock_udp_create(&sock, &local, &remote, 0)) < 0) {
        LOG_ERROR("[udp server] error initializing socket (%i)\n", res);
        return res;
    }
```

> 2) How can I get the client's address and port as string using `remote` variable?

You could use `ipv6_addr_to_str()` like this:
```c
    while (1) {
        sock_udp_ep_t remote;
        ssize_t res;
        if ((res = sock_udp_recv(&sock, server_buffer, sizeof(server_buffer),
            SOCK_NO_TIMEOUT, &remote)) < 0)
        {
            LOG_WARNING("sock_udp_recv() returned %li\n", res);
            continue;
        }

        char remote_ip[IPV6_ADDR_MAX_STR_LEN];
        ipv6_addr_to_str(remote_ip, (const ipv6_addr_t *)remote.addr.ipv6,
                         sizeof(remote_ip));
        LOG_DEBUG("received datagram from [%s]:%" PRIu16 "\n",
                  remote_ip, remote.port);
    }
```

-- 
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/11894#issuecomment-515711981
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.riot-os.org/pipermail/notifications/attachments/20190727/c5b22115/attachment.htm>


More information about the notifications mailing list