Why is nsapi_size_or_error_t always -3001 and becomes never > 0, even when I get a message?
Using a STM32-NucleoF767ZI board with meeds 6.8 and Mbed Studio
UDP is connectionless, it doesn‘t need the connect.
receivefrom works also without bind, a SocketAddress with the listening Port will work also. As in the UDP snippet.
UDP is connectionless, it doesn‘t need the connect.
This right, but connect() allows to use send() instead of sendTo()
I tried with a SocketAddress only with the listing port as you mentioned but this does not work.
The updated example which does not work without binding:
And when you set receive SocketAddress to IP 0.0.0.0? Documentation say it is initially unspecified. In the example, the IP is set to the server address with gethostbyname.
yes, I tried just the same with the same result. Thats a little bit strange, because the example does the same execept using sendto() before.
I have initialised the socket also with full IP information.
The initial problem was that udp.connect(send); blocks the correct output of nsapi_size_or_error_t size = udp.recv(buffer, sizeof(buffer)); and gives always -3001 when receiving data.
I think you get the error -3001 (NSAPI_ERROR_WOULD_BLOCK) because the socket is non-blocking and you request more data than available. This brings -3001, and the documentation says:
Note
recv() is allowed write to data buffer even if error occurs.
This is of course strange, I don’t know if this is Posix standard behaviour.
And you are setting also the eth interface to non-blocking, but without event handler:
It is not guaranteed that the interface is set up completely when you acces the netif directly after connect(). It may work now, but when dhcp is enabled, this will defintely fail.
It is definitely a bug. When replace udp.connect(send); and I use udp.sendto() instead udp.send()the udp.recv() function works as expected.
Can you give examples to use event handler for sending and receiving?
I lost a bunch of packets, however.
In the meantime, I have worked also on my UDP connection. I’ve read your issue, the answers seem reasonable.
I’ve tested first a version with a non-blocking UDP Socket and used sigio(). But that was more inefficient:
sigio can run in interrupt context, so you cannot directly send a datagram after receiving
in signal handler, I do receiving the packets until I get error would_block. For the incoming packets, I used a memory pool and a queue for storing the pointer to data, remote and packet size.
I have a 2nd thread that handles the queue of received packets
Now I have removed this stuff and do a blocking recvfrom() in an own thread. After receiving, I call a message handler that can also respond directly with a sendto(remote,…);
I’ve seen also that the memory setting for lwip is pretty stingy, increasing the lwip memory pool in your mbed_app.json should perform better: