🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: borland.public.cppbuilder.internet.socket
5 messages
5 total messages Started by Martin Fri, 10 Mar 2006 09:25
Multi-threaded TCP Server with Indy 9
#2667
Author: Martin
Date: Fri, 10 Mar 2006 09:25
10 lines
1217 bytes
Does anybody has a C++ example for a multi-threaded TIdTcpServer using Indy 
9?
I want to create a thread for each TCP connection, to handle different 
communication states.

I did this before with TServerSocket, but had no chance to make it stable.
Now i want to switch to Indy 9 with BDS2006.

Thanks for any help
Martin 
Re: Multi-threaded TCP Server with Indy 9
#2671
Author: Remy Lebeau (TeamB)
Date: Fri, 10 Mar 2006 18:46
24 lines
1676 bytes
"Martin" <martin@styleworx.com> wrote in message
news:441145d6$1@newsgroups.borland.com...

> Does anybody has a C++ example for a multi-threaded
> TIdTcpServer using Indy 9?

What exactly do you want an example of?  Please be more specific.

> I want to create a thread for each TCP connection, to handle
> different communication states.

TIdTCPServer handles that for you automatically.  You don't have to do
anything yourself, other than to assign handers to the desired component
events.

> I did this before with TServerSocket, but had no chance to make it stable.

Then you probably weren't using it properly in the first place.
TServerSocket is very stable component.  I have used it to make numerous
multi-threaded servers in several different versions of BCB.


Gambit
Re: Multi-threaded TCP Server with Indy 9
#2675
Author: Martin
Date: Sat, 11 Mar 2006 13:50
56 lines
2841 bytes
OK, this is my (working) code i am currently using:

//---------------------------------------------------------------------------
class TMyThreadClass
{
public:
        TIdPeerThread*  pAThread;
        int someparameter;
        void __fastcall Process();
protected:
private:
};
//---------------------------------------------------------------------------
void __fastcall TForm1::IdTCPServer1Connect(TIdPeerThread *AThread)
{
   TMyThreadClass* pMyThreadClass;
   pMyThreadClass= new TMyThreadClass();
   pMyThreadClass->someparameter=1;  // initializing parameters
   AThread->Data = (TObject*) pMyThreadClass;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::IdTCPServer1Disconnect(TIdPeerThread *AThread)
{
   TMyThreadClass* pMyThreadClass;
   pMyThreadClass = (TMyThreadClass*) AThread->Data;
   delete pMyThreadClasss;
   AThread->Data = NULL;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::IdTCPServer1Execute(TIdPeerThread *AThread)
{
   TMyThreadClass* pMyThreadClass;
   pMyThreadClass = (TMyThreadClass*) AThread->Data;
   pMyThreadClass->pAThread = (TIdPeerThread*) AThread;

   // read from with AThread->Connection->CurrentReadBuffer()

    if (...) // some conditions
   {
      Process();
   }
}
//---------------------------------------------------------------------------
void __fastcall TDeviceClass::Process()
{
     // Example
     pAThread->Connection->WriteLn("Hello!");
}
//---------------------------------------------------------------------------

This code works fine. But i want to assign MyThreadClass to 
IdTCPServer->ThreadClass.
You wrote in a previous thread, that this would be the better solution.
But i gave it up due some access violations.

Regards, Martin
Re: Multi-threaded TCP Server with Indy 9
#2601
Author: Remy Lebeau (TeamB)
Date: Mon, 13 Mar 2006 21:23
67 lines
2526 bytes
"Martin" <martin@styleworx.com> wrote in message
news:4412d582@newsgroups.borland.com...

> class TMyThreadClass
> {
> public:
>         TIdPeerThread*  pAThread;
>         int someparameter;
>         void __fastcall Process();
> protected:
> private:
> };

A better solution would be to derive a new class from TIdPeerThread itself
and then assign the server's ThreadClass property:

    class TMyThreadClass : public TIdPeerThread
    {
    public:
        __fastcall TMyThreadClass(bool ACreateSuspended = true);
        int someparameter;
        void __fastcall Process();
    };

    __fastcall TMyThreadClass(bool ACreateSuspended)
        : TIdPeerThread(ACreateSuspended), someparameter(1)
    {
    }

    void __fastcall TMyThreadClass::Process()
    {
        // Example
        Connection->WriteLn("Hello!");
    }

    __fastcall TForm1::TForm1(TComponent *Owner)
    {
        IdTCPServer1->ThreadClass = __classid(TMyThreadClass);
    }

    void __fastcall TForm1::IdTCPServer1Execute(TIdPeerThread *AThread)
    {
        TMyThreadClass* pMyThreadClass =
static_cast<TMyThreadClass*>(AThread);

        // read with AThread->Connection->CurrentReadBuffer()

        if( some conditions )
            pMyThreadClass->Process();
    }

> This code works fine. But i want to assign MyThreadClass to
> IdTCPServer->ThreadClass.

See above.

> You wrote in a previous thread, that this would be the better solution.

It is.

> But i gave it up due some access violations.

Then you probably weren't using it properly.


Gambit
Re: Multi-threaded TCP Server with Indy 9
#2605
Author: Martin
Date: Tue, 14 Mar 2006 08:42
9 lines
1291 bytes
Thank you Gambit! It works now!
I only had to correct the syntax like this

__fastcall TMyThreadClass::TMyThreadClass(bool ACreateSuspended)
        : TIdPeerThread(ACreateSuspended)
{
}

Regards, Martin 
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads