Recent

Author Topic: Catching Indy IdUDPClient.SendBuffer error  (Read 630 times)

Dzandaa

  • Sr. Member
  • ****
  • Posts: 253
  • From C# to Lazarus
Catching Indy IdUDPClient.SendBuffer error
« on: March 09, 2023, 04:49:45 pm »
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  [Select][+][-]
  1.    MyUDPClient.Host := IP;
  2.    MyUDPClient.Port := Port;
  3.    MyUDPClient.Connect;  // No error even if the IP or Port doesn't exist    
  4. ...
  5.    MyUDPClient.SendBuffer(DataSend); // I want to catch the error
  6.  
  7.  

Any ide?

Thank you.
Dzandaa

Warfley

  • Hero Member
  • *****
  • Posts: 1499
Re: Catching Indy IdUDPClient.SendBuffer error
« Reply #1 on: March 09, 2023, 05:00:24 pm »
It's UDP, so you can't know if the message reached it's destination.

Example:
Code: Pascal  [Select][+][-]
  1. var
  2.   sock: TSocket;
  3.   Res: SizeInt;
  4. begin
  5.   sock := UDPSocket(stIPv4);
  6.   Res := SendStrTo(sock, '153.2.111.10', 5917, 'Test'); // Made up IP and port
  7.   WriteLn(Res); // Result: 4 = Length('Test')
  8.   CloseSocket(sock);
  9. 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.
« Last Edit: March 09, 2023, 05:07:47 pm by Warfley »

Dzandaa

  • Sr. Member
  • ****
  • Posts: 253
  • From C# to Lazarus
Re: Catching Indy IdUDPClient.SendBuffer error
« Reply #2 on: March 09, 2023, 05:16:56 pm »
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.
Dzandaa

Warfley

  • Hero Member
  • *****
  • Posts: 1499
Re: Catching Indy IdUDPClient.SendBuffer error
« Reply #3 on: March 09, 2023, 05:18:36 pm »
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

  • Hero Member
  • *****
  • Posts: 1314
    • Lebeau Software
Re: Catching Indy IdUDPClient.SendBuffer error
« Reply #4 on: March 10, 2023, 03:08:07 am »
It's UDP, so you can't know if the message reached it's destination.

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.

All I want to know if it is possible to catch the IdUDPClient.SendBuffer error with try, except finally.

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?
« Last Edit: March 10, 2023, 03:12:45 am by Remy Lebeau »
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

Dzandaa

  • Sr. Member
  • ****
  • Posts: 253
  • From C# to Lazarus
Re: Catching Indy IdUDPClient.SendBuffer error
« Reply #5 on: March 10, 2023, 10:50:01 am »
Hi,

Thank you Warfley, I forgot that the debugger showed an alert message when using try, except, etc...
Thank you.

B->
Dzandaa

 

TinyPortal © 2005-2018