I'm making some tests with the socket client component to catch an solve network and connections exceptions.
The test i'm doing now is closing the socket server application suddenly and see the socket client behavior, but the client start a loop with this exception:
Even if i reopen the server application, the client doesn't connect again with the server.
Any idea how i deal with this exception and force a new connection with the server?
Can you provide a minimal example for this (both server and client)? It's hard to tell what is going on without any context. Just by connecting to the server then closing the server does not raise this exception.
note that both socket server and client are created dynamically. This is the only configuration i do on the components i used to have a TTimer for make the Ping calls but i disable it for a while during the tests.
No. i send no messages in the test, only connect the client with the server, wait some seconds and then close the server application. After this the exception is raised.
Are you sure that is all? With the given information we are only able to reproduce this if the server instance is not destroyed when the application stops. Given you are creating this dynamically, where are you calling FSocketServer.Free?
Yes, i'm calling the .Free on the instance, but i was calling on another place, not when the application stops, i changed the place that was calling the .Free for the OnDestroy of the form:
procedure TMainForm.FormDestroy(Sender: TObject);
begin
if Assigned(FSocketServer) then
FreeAndNil(FSocketServer);
end;
doing this way, no exception on the client. i'll continue making the tests on the component and start a new connection. Thanks for the help
It should be enough to wrap the Connect call with a try..except block, we don't do that so it can be raised at application level for you to handle.
procedure TForm1.Button1Click(Sender: TObject);
begin
try
TMSFNCWebsocketClient1.Connect;
except
on E: Exception do
//Handle exception
end;
end;
Depending on your IDE settings, the debugger might intercept this exception before it has a chance to reach the exception handling. In that case just click Continue. If the application is running outside of the IDE you won't encounter that behavior.