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.

有帮助吗?

解决方案

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;
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top