Forum > Networking and Web Programming
Catching Indy IdUDPClient.SendBuffer error
Dzandaa:
Hi,
Using Lazarus 2.2.4 and Indy on Windows and Linux.
I have an ESP8266 server that respond to an UDP command and send back Data.
I have an Indy IdUDPClient that send command and receive the data.
All works good except if I close the server or if I connect when the IP and port are wrong, IdUDPClient.SendBuffer crash.
I want to catch the error and close correctly.
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} --- MyUDPClient.Host := IP; MyUDPClient.Port := Port; MyUDPClient.Connect; // No error even if the IP or Port doesn't exist ... MyUDPClient.SendBuffer(DataSend); // I want to catch the error
Any ide?
Thank you.
Warfley:
It's UDP, so you can't know if the message reached it's destination.
Example:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---var sock: TSocket; Res: SizeInt;begin sock := UDPSocket(stIPv4); Res := SendStrTo(sock, '153.2.111.10', 5917, 'Test'); // Made up IP and port WriteLn(Res); // Result: 4 = Length('Test') CloseSocket(sock);end.As you can see, when sending data to this made up ip and port, where there is no server behind, it still works as expected and tells me that 4 bytes have been sent, even though they never reached any destination.
Dzandaa:
Hi,
Yes, I know that you can't rely on UDP and send data to an unknown IP,
All I want to know if it is possible to catch the IdUDPClient.SendBuffer error with try, except finally.
Thank you.
Warfley:
If an exception is thrown, you you probably can just use try-except. But you should note that lazarus breaks on exceptions anyway, if they are enclosed in try-except or not. This is just a debugging functionality and will not occur in production code running without a debugger. You can disable handling of certain exception types to avoid this. Otherwise, just click the continue button when the exception message pops up.
Remy Lebeau:
--- Quote from: Warfley on March 09, 2023, 05:00:24 pm ---It's UDP, so you can't know if the message reached it's destination.
--- End quote ---
Actually, you can, but not immediately. When you send a packet to a non-existent IP/port, the network is likely to send back an ICMP packet explaining that the host/port was unreachable. If you Connect() a UDP socket to a peer, and the socket internally receives such an ICMP packet after a send to that peer, then subsequent sends on that same peer will fail with an error that you can catch, as long as the socket remains "connected" to that peer.
--- Quote from: Dzandaa on March 09, 2023, 05:16:56 pm ---All I want to know if it is possible to catch the IdUDPClient.SendBuffer error with try, except finally.
--- End quote ---
Of course it is possible. It is just a normal 'raise'd exception, like any other. Why do you think it is not possible to catch it?
Navigation
[0] Message Index
[#] Next page