Question

I need to connect to a server from a client but if the client is unable to connect to the server because the server is offline I want it to display a message saying that there was a error connecting to the server.

The code I have tried is:

try
 form1.IdTCPClient1.Host := 'localhost';
 form1.IdTCPClient1.Port := 55555;
 form1.IdTCPClient1.Connect;
except
 ShowMessage('Connection Unsuccessful');
end;

But when I run the program it still gives me socket error #10061 error message.

Thank you for your help.

Was it helpful?

Solution

When you're running your application in Debug mode, all exceptions will still appear to you (as long as you don't explicitly tell the IDE to ignore certain exception types). However, when you run your application by its self (without debugging), you will not see these exceptions which are handled.

You should also handle exception types...

try
  form1.IdTCPClient1.Host := 'localhost';
  form1.IdTCPClient1.Port := 55555;
  form1.IdTCPClient1.Connect;
except
  on E: EIdSocketError do begin
    ShowMessage('Connection Unsuccessful: '+E.Message);
  end;
end;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top