Search code examples
firemonkeyc++builderrad-studio

Change error message in TIdTCPClient.Connect


I'm using TIdTCPClient. It seems to be a simple thing to do, but I can't find a way to change the error message "Already connected" when I execute tcpClient->Connect();

So I set the Host, Port, and ConnectTimeout, and after all this I call the Connect().

I tried to change that error message using WriteLn(), maybe is that the solution, but I could be using that in a wrong way.

I only want to show to the client (maybe in a ShowMessage() or in a TLabel), not necessarily to the server.


Solution

  • You can't call TIdTCPClient::Connect() if the client is already connected to a server (TIdTCPClient::Connected() returns true), you have to call TIdTCPClient::Disconnect() first.

    If you are asking how to change the error message text itself, it is a localized string (RSAlreadyConnected in the IdResourceStringsCore unit), so just localize your app as needed.

    Otherwise, you can simply use a try..catch to catch the EIdAlreadyConnected exception that is raised, and then display whatever text you want, however you want, eg:

    try {
      tcpClient->Connect();;
    }
    const (const EIdAlreadyConnected &) {
        ShowMessage("Already connected! Please disconnect first.");
    }
    catch (const Exception &) {
        ShowMessage("Error Connecting!");
    }