Question

I need to implement a TCP Server connection with TIdTCPServer component. I already did it with GUI (drag & drop) and its working. But I need to Split form and TCP implementations. Example codes I've seen so far always uses TIdTCPServer as the member of TForm class.(result of drag and drop).

How I Call TCPConnection class that I've implemented from the TForm

TCPConnection CConnection = new TCPConnection(Owner, this);

Here is how I try to create a TCP Server Connection.

TCPConnection::TCPConnection(TComponent* Owner, TForm4* TSuperForm){
    IdTCPServer1 = new TIdTCPServer(Owner);
    IdTCPServer1->Bindings->Clear();
    //IdTCPServer1->Bindings->Add()->SetBinding("10.10.2.103", 774);

    IdTCPServer1->OnConnect = (TIdServerThreadEvent)(&OnConnect);
    IdTCPServer1->OnExecute = (TIdServerThreadEvent)&OnExecute;
    IdTCPServer1->OnDisconnect = (TIdServerThreadEvent)&OnConnect;
    IdTCPServer1->OnException =  (TIdServerThreadExceptionEvent)&OnException;
     IdTCPServer1->DefaultPort = 774;
    IdTCPServer1->Bindings->Add();
    IdTCPServer1->Bindings->Items[0]->IP="10.10.2.103";
    IdTCPServer1->Bindings->Items[0]->Port=774; 


    IdTCPServer1->ListenQueue = 15;
    IdTCPServer1->MaxConnections = 15;
    IdTCPServer1->TerminateWaitTime = 5000;
    IdTCPServer1->Active = true;



    this->TSuperForm = TSuperForm;

}

So far that codes work. But when I try to reach the context, connection is lost and throws an exception

void TCPConnection::OnConnect(TIdContext *AContext){
    String IP = AContext->Binding()->PeerIP;
}

void TCPConnection::OnException(TIdContext *AContext, Exception *AException)

{
    ShowMessage("Error:" + AException->ToString());    
}

Error says TIdTaskThreadWork (I'll edit error, might be wrong) If I don't try to reach AContext, connection stays without problem.

Might be something about thread, locking list, ...

Any Suggestions?

Was it helpful?

Solution

Those function casts look very smelly. Are you sure you've defined the functions as __fastcall, as the function casts should not be required at all.

This should be all you need, if you've defined them correctly.

IdTCPServer1->OnConnect = &OnConnect;
// ... etc...

OTHER TIPS

This is what I do:

TIdTCPServer *TCPServer = new TIdTCPServer( this );
TCPServer->Active = false;
TCPServer->OnExecute = MyExecute;
TCPServer->DefaultPort = XXX;
TCPServer->Active = true;

Then my MyExecute call is defined like this:

void __fastcall MyExecute( TIdContext* AContext );

Other callbacks are handled the same way, don't forget __fastcall and it should work.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top