Thread and TCP

This is the forum for miscellaneous technical/programming questions.

Moderator: 2ffat

Re: Thread and TCP

Postby ingalime » Thu Nov 30, 2017 3:08 am

Thank you very much for your help and patience. The new code works as expected and if the server is turned ON and OFF. All perfectly! Please check the new version:
Main:
Code: Select all
TFormMenu *FormMenu;
TMyConnect * MyConnect = NULL;
extern String ip;
extern int port;
int chek = 0;
//---------------------------------------------------------------------------
__fastcall TFormMenu::TFormMenu(TComponent* Owner)
   : TForm(Owner)
{
}
//---------------------------------------------------------------------------
//This button if user want just close form
void __fastcall TFormMenu::Button1Click(TObject *Sender)
{
 chek = 1;
 Close();
}
//---------------------------------------------------------------------------
void __fastcall TFormMenu::ThreadTerminated(TObject *Sender)
{
   TMyConnect *Thread = static_cast<TMyConnect*>(Sender);
   if(Thread->FatalException)//if setver OFF=Connect timed out.
    {
     chek = 1;
     //user should not see messages in English
     //in the future in my language:
     ShowMessage(L"Sorry there is no connection to the server");
    }

   Button2->Enabled = true;
   AniIndicator1->Visible = false;
   Close();
}
//---------------------------------------------------------------------------
void __fastcall TFormMenu::Button2Click(TObject *Sender)
{
  if(Memo1->Lines->Count > 0)
   {
   Button2->Enabled = false;
   AniIndicator1->Visible = true;
   MyConnect = new TMyConnect(ip, port, Memo1->Lines);
    MyConnect->OnTerminate = &ThreadTerminated;
   MyConnect->Start();
  }
}
//---------------------------------------------------------------------------

void __fastcall TFormMenu::FormShow(TObject *Sender)
{
 chek = 0;
}
//---------------------------------------------------------------------------

void __fastcall TFormMenu::FormClose(TObject *Sender, TCloseAction &Action)
{
  if(chek == 0)
      {
      //in the future in my language:
      ShowMessage(L"Thank you. All good.");
       
      }
}
//---------------------------------------------------------------------------


h. TThread
Code: Select all
class TMyConnect : public TThread
{
   private:
      String FHost;
      Word FPort;
      TStringList *FLines;
   protected:
      void __fastcall Execute();
   public:
      __fastcall TMyConnect(String AHost, Word APort, TStrings *ALines);
      __fastcall ~TMyConnect();
};


cpp. TThread
Code: Select all
__fastcall TMyConnect::TMyConnect(String AHost, Word APort, TStrings *ALines)
    : TThread(true), FHost(AHost), FPort(APort)
{
   FreeOnTerminate = true;
   FLines = new TStringList;
   FLines->Assign(ALines);
}
//---------------------------------------------------------------------------

__fastcall TMyConnect::~TMyConnect()
{
  delete FLines;
}
//---------------------------------------------------------------------------
void __fastcall TMyConnect::Execute()
{
   std::unique_ptr<TIdTCPClient> Client(new TIdTCPClient);

   Client->Host = FHost;
   Client->Port = FPort;
   Client->ConnectTimeout = 5000;

   Client->Connect();
   Client->IOHandler->Write(FLines, true, IndyTextEncoding_UTF8());
   Client->Disconnect();
}


I love Indy!
ingalime
Active Poster
Active Poster
 
Posts: 18
Joined: Wed Apr 12, 2017 3:22 am

Previous

Return to Technical

Who is online

Users browsing this forum: No registered users and 18 guests