I am writing a C++ application using Embarcadero RAD studio and TServerSocket
component in blocking mode. I have overridden the OnGetThread
handler for the socket to create a custom class derived from TServerClientThread
which overrides the default ClientExecute()
method. Within this function I use a TWinSocketStream
and make calls to WaitForData()
, Read()
, and Write()
to receive and send data. According to everything I have read this is an acceptable way to go about things (although please correct me if this is wrong).
From this answer What events are fired for a blocking socket? I am lead to believe that using an OnClientError
handler in blocking mode is OK since the events will fire. In my event handler I set the error code to zero every time so that the exception does not get thrown.
In addition, every time I call Read()
or Write()
from within my ClientExecute()
function I wrap it in a try-catch block and catch ESocketError
exceptions.
My question is this: Which is the best approach:
- Use a socket error event handler to deal with everything (this is nice as I can get the socket error code to display for debugging purposes)
- Use the try-catch statements to prevent my application from throwing exceptions
- Use both (although not set the error code to zero in the handler otherwise the exception will not get thrown making 2. above pointless)
This is an old component and has been deprecated but I have to use this but as I have been unable to find a cast iron guide to using it correctly I have pieced together an approach from many sources. It has been working well for quite some time, but every now and again I get a silent error which prevents the server from accepting any further client connections - but I get no output from OnClientError
and no ESocketErrors
are thrown. This application is running on an embedded device so the only way it is detected is when it becomes non-responsive.
If anyone could give me advice on which of the 3 approaches above is best (or suggest an alternative) I would be very grateful.