0

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.

Craig
  • 548
  • 9
  • 24
  • 3
    In debug mode, you're still going to see these exceptions, otherwise without debug, you won't see them. Try running the EXE its self without the Delphi IDE. – Jerry Dodge Feb 21 '14 at 20:47
  • Well you learn something new every day right :p Thank you :) – Craig Feb 21 '14 at 20:52
  • 1
    You might find [this answer](http://stackoverflow.com/a/20386385/62576) to a similar problem useful. – Ken White Feb 22 '14 at 00:28

1 Answers1

1

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;
Jerry Dodge
  • 26,858
  • 31
  • 155
  • 327