-
Notifications
You must be signed in to change notification settings - Fork 3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Failure sending data over UDP blocking socket #2389
Comments
Hi Jenia, thanks for reporting this issue. The NSAPI_ERROR_PARAMETER is actually the expected behavior. The recvfrom function modifies the SocketAddress in place, if a recvfrom call fails the SocketAddress is set to null (represented as “0.0.0.0”). The following sendto call fails with NSAPI_ERROR_PARAMETER because the address “0.0.0.0” is an invalid IP address. The address argument to the recvfrom function is useful for UDP servers, but usually unnecessary for applications where the server address is known beforehand. In this case, a NULL address can be used instead since it is safely ignored: //some message is sent on UDP socket
sentBytes = _socket->sendto(*_socketAddress, messageOut, messageSize);
// message is received on UDP socket
recievedBytes = _socket->recvfrom(NULL, messageIn, messageBufferSize);
if (recievedBytes == NSAPI_ERROR_WOULD_BLOCK){
//some message is sent on UDP socket
sentBytes = _socket->sendto(*_socketAddress, messageOut, messageSize);
} Let me know if you find an issue with this. |
@jenia81 I think I have found the culprit here. There is nothing wrong with APIs. I itested with the code provided by you with a simple python UDP server running on my home machine. The only issue I found was that the ARM machine (office laptop) firewall was blocking my traffic. So I ran the UDP server on my personal machine and made sure that the socket was opened. I would also suggest you to use some other port than 5683 (which is a coap port). Use something like 6005. Also, for this test call sendto() first and then call recvfrom() so as to get a response from our dummy server. I had added some code to enable traces , you can remove it if you wish. I tried with both removing the CLIENT from mbed_app.json and keeping it, reponse is just the same. So I am pretty sure that the issue is with your setup rather than mbed-os/client or any other networking APIs. How to reproduce what I did:
|
@geky @hasnainvirk the issue was that if recvfrom() fails because there is no data received (the error would be then NSAPI_ERROR_WOULD_BLOCK), then next sendto fails too. |
Glad it worked for you. Unfortunately, in previous versions of mbed-os the state of the address after a failed recvfrom was undefined. Most network interfaces didn’t bother to modify the address until an actual packet had arrived, so the code would usually work. Imagine what would happen if I sent a random packet to the port you happened to be listening on, the original address would be lost. The recent changes made the behavior of a failed recvfrom more consistent. As for checking that checking that the received packet is from the correct source, your best bet is to create a temporary SocketAddress: //some message is sent on UDP socket
sentBytes = _socket->sendto(*_socketAddress, messageOut, messageSize);
// message is received on UDP socket
SocketAddress sourceAddress;
recievedBytes = _socket->recvfrom(&sourceAddress, messageIn, messageBufferSize);
if (recievedBytes >= 0) {
// successfully received a message
if (strcmp(_socketAddress.get_ip_address(), &sourceAddress.get_ip_address()) == 0) {
// _socketAddress and sourceAddress are the same
sentBytes = _socket->sendto(*_socketAddress, messageOut, messageSize);
}
} You were right that the recvfrom address parameter is how you get the packet’s source address, however there needs to be a temporary to avoid overwriting the _socketAddress. |
@jenia81 Yup I understood that. That's why if you notice, I changed the pattern of calls in your code and mentioned it in the comment that this is an example of showing you that it works. |
@jenia81 Can you please close this issue then if you are happy with the discussion ? |
@geky I've added the code you suggested and it works now. |
…..48609ae 48609ae Merge branch 'release_internal' into release_external 62d8586 Ignore ns_monitor when receiving Ack (ARMmbed#2417) 3323f36 Add support for Ethernet RA dns configuration d8e7d40 Iotthd 4239 (ARMmbed#2414) b46f3c6 add empty function for ws_stack_info_get fc97980 Changed RADIUS shared secret length to 16-bit value f827ffc Added information API to Wi-SUN and border router 8f1f9d5 EDFE error handling update 51bf94e Fix adaptation interface unit tests (ARMmbed#2409) 0860b57 FHSS_WS: Fixed reading unicast remaining slots (ARMmbed#2408) 4d8c03b Border Router RADIUS client basic authentication functionality (ARMmbed#2406) fbfada9 Adaptation IF: Allocate fragmentation buffer only if needed (ARMmbed#2407) 66f1bff Added storing of PAN version to NVM on BR 89826ce Iotthd 4224 (ARMmbed#2403) 3fc1ae2 BR EUI-64 is now selected for 4WH using PMKID on 4WH Message 1 af8438e Timing tool traces (ARMmbed#2401) 7938795 Fixed new PD data request for check if EDFE exchange is active. 85ab8fd Extented Frame exchange support 86b1f27 Merge pull request ARMmbed#2399 from ARMmbed/IOTTHD-4220 fed69e0 Add missing test method to ws_empty_functions 6b58e26 Add EDFE mode to Socket API setsockopt 1283077 Test API to adjust 6LoWPAN fragmentation MTU size (ARMmbed#2398) e787874 Init MAC MTU size based on driver MTU size (ARMmbed#2397) bf8e89e Ignore neighbors using unsupported channel function (ARMmbed#2395) 1c263fd FHSS exclude channel usage from mask and Brazilian Domain support 841dcbe MAC: Configurable data whitening (ARMmbed#2393) 9a10d66 Fix global address detection (ARMmbed#2392) f27fe86 Corrected network name and PAN ID change on auth start bcce0ed Clarified border router routing table API description e4630a4 Wi-SUN interface now informs address changes as interface events 2174374 Fix error found by coverity (ARMmbed#2389) 843254a MPL: traces for transmit and receive message (ARMmbed#2387) git-subtree-dir: features/nanostack/sal-stack-nanostack git-subtree-split: 48609ae
…..48609ae 48609ae Merge branch 'release_internal' into release_external 62d8586 Ignore ns_monitor when receiving Ack (ARMmbed#2417) 3323f36 Add support for Ethernet RA dns configuration d8e7d40 Iotthd 4239 (ARMmbed#2414) b46f3c6 add empty function for ws_stack_info_get fc97980 Changed RADIUS shared secret length to 16-bit value f827ffc Added information API to Wi-SUN and border router 8f1f9d5 EDFE error handling update 51bf94e Fix adaptation interface unit tests (ARMmbed#2409) 0860b57 FHSS_WS: Fixed reading unicast remaining slots (ARMmbed#2408) 4d8c03b Border Router RADIUS client basic authentication functionality (ARMmbed#2406) fbfada9 Adaptation IF: Allocate fragmentation buffer only if needed (ARMmbed#2407) 66f1bff Added storing of PAN version to NVM on BR 89826ce Iotthd 4224 (ARMmbed#2403) 3fc1ae2 BR EUI-64 is now selected for 4WH using PMKID on 4WH Message 1 af8438e Timing tool traces (ARMmbed#2401) 7938795 Fixed new PD data request for check if EDFE exchange is active. 85ab8fd Extented Frame exchange support 86b1f27 Merge pull request ARMmbed#2399 from ARMmbed/IOTTHD-4220 fed69e0 Add missing test method to ws_empty_functions 6b58e26 Add EDFE mode to Socket API setsockopt 1283077 Test API to adjust 6LoWPAN fragmentation MTU size (ARMmbed#2398) e787874 Init MAC MTU size based on driver MTU size (ARMmbed#2397) bf8e89e Ignore neighbors using unsupported channel function (ARMmbed#2395) 1c263fd FHSS exclude channel usage from mask and Brazilian Domain support 841dcbe MAC: Configurable data whitening (ARMmbed#2393) 9a10d66 Fix global address detection (ARMmbed#2392) f27fe86 Corrected network name and PAN ID change on auth start bcce0ed Clarified border router routing table API description e4630a4 Wi-SUN interface now informs address changes as interface events 2174374 Fix error found by coverity (ARMmbed#2389) 843254a MPL: traces for transmit and receive message (ARMmbed#2387) git-subtree-dir: features/nanostack/sal-stack-nanostack git-subtree-split: 48609ae
…3fe574..48609ae 48609ae Merge branch 'release_internal' into release_external 62d8586 Ignore ns_monitor when receiving Ack (ARMmbed#2417) 3323f36 Add support for Ethernet RA dns configuration d8e7d40 Iotthd 4239 (ARMmbed#2414) b46f3c6 add empty function for ws_stack_info_get fc97980 Changed RADIUS shared secret length to 16-bit value f827ffc Added information API to Wi-SUN and border router 8f1f9d5 EDFE error handling update 51bf94e Fix adaptation interface unit tests (ARMmbed#2409) 0860b57 FHSS_WS: Fixed reading unicast remaining slots (ARMmbed#2408) 4d8c03b Border Router RADIUS client basic authentication functionality (ARMmbed#2406) fbfada9 Adaptation IF: Allocate fragmentation buffer only if needed (ARMmbed#2407) 66f1bff Added storing of PAN version to NVM on BR 89826ce Iotthd 4224 (ARMmbed#2403) 3fc1ae2 BR EUI-64 is now selected for 4WH using PMKID on 4WH Message 1 af8438e Timing tool traces (ARMmbed#2401) 7938795 Fixed new PD data request for check if EDFE exchange is active. 85ab8fd Extented Frame exchange support 86b1f27 Merge pull request ARMmbed#2399 from ARMmbed/IOTTHD-4220 fed69e0 Add missing test method to ws_empty_functions 6b58e26 Add EDFE mode to Socket API setsockopt 1283077 Test API to adjust 6LoWPAN fragmentation MTU size (ARMmbed#2398) e787874 Init MAC MTU size based on driver MTU size (ARMmbed#2397) bf8e89e Ignore neighbors using unsupported channel function (ARMmbed#2395) 1c263fd FHSS exclude channel usage from mask and Brazilian Domain support 841dcbe MAC: Configurable data whitening (ARMmbed#2393) 9a10d66 Fix global address detection (ARMmbed#2392) f27fe86 Corrected network name and PAN ID change on auth start bcce0ed Clarified border router routing table API description e4630a4 Wi-SUN interface now informs address changes as interface events 2174374 Fix error found by coverity (ARMmbed#2389) 843254a MPL: traces for transmit and receive message (ARMmbed#2387) git-subtree-dir: connectivity/nanostack/sal-stack-nanostack git-subtree-split: 48609ae
The following scenario fails:
Attached is sample app that recreates this problem (rename it to .cpp)
udp_socket_example.txt
Should be compiled with following command:
"mbed compile -t GCC_ARM -m K64F --source ."
The text was updated successfully, but these errors were encountered: