1

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.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
devs121
  • 49
  • 8

1 Answers1

1

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!");
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • Not working... I tried to use that in the way that you wrote, but no results, tried to get only the **const** but no results, tried to override EIdAlreadyConnected, but it keeps showing the message "already connected". I just need to modify this message... Do you have another example? – devs121 Aug 11 '20 at 18:40
  • No problem, i got this now, i did it with the return of the value of **Connected()**, but thanks for the help Remy! – devs121 Aug 11 '20 at 19:38
  • 1
    You can't modify the text itself without altering Indy's source code, or localizing your app (or, catching the exception and altering its `Message` property). Are you seeing the error popup while running your app inside the debugger? If so, that is perfectly normal behavior, and you can configure the debugger to ignore `EIdAlreadyConnected` if needed. Otherwise, if you are seeing the error appear while running your app outside of the debugger, that would mean the compiler's exception handling is not working properly. That's a known issue on Android and OSX, for instance. – Remy Lebeau Aug 11 '20 at 20:06
  • Ok, thank you so much, I got it, but I had another idea: To disable an "enable" button if clicked and vice-versa to another one. I thank I did a little bit much, with that I could do what I needed. And thanks for the explanation, I didn't know about that. – devs121 Aug 11 '20 at 21:16