🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: borland.public.cppbuilder.internet.socket
11 messages
11 total messages Started by Ulrik Nilsson Mon, 06 Sep 2004 18:49
Close down connections from the server side. Indy.
#5409
Author: Ulrik Nilsson
Date: Mon, 06 Sep 2004 18:49
33 lines
1688 bytes
Hi!

I need help with to close down all connections from the server (Indy 9).

This is a part of my code

void __fastcall TForm1::FormCreate(TObject *Sender)
{
    mUserMutex = CreateMutex(NULL, false, NULL);
    Users = 0;
    UpdateUsers();
    TCPServer->Active = true;
}

void __fastcall TForm1::Close_ButtonClick(TObject *Sender)
{
    TCPServer->Active = false;
}

void __fastcall TForm1::TCPServerExecute(TIdPeerThread *AThread)

    AnsiString str = AThread->Connection->ReadLn("\n", 2000, 10);
    //AnsiString str = AThread->Connection->ReadLn();


The problem is that my program stops at one of the above lines.
It doesn't matter which I use when I click the "Close" button.
And, of course, no OnDisconnect is fired in the client.

Any code snippet would be very much appreciated.

Regards Ulrik
Re: Close down connections from the server side. Indy.
#5410
Author: Remy Lebeau (TeamB)
Date: Mon, 06 Sep 2004 19:47
19 lines
1600 bytes
"Ulrik Nilsson" <di2niul@chl.chalmers.se> wrote in message
news:413cb130$1@newsgroups.borland.com...

> void __fastcall TForm1::FormCreate(TObject *Sender)

Do not use the OnCreate event in C++.  It is a Delphi remnant that does not
work properly in C++ as it can be triggered before the constructor, which is
illegal in C++.  Use the actual constructor instead.

> The problem is that my program stops at one of the above
> lines. It doesn't matter which I use when I click the "Close"
> button. And, of course, no OnDisconnect is fired in the client.

Please show all of your OnExecute code.  You are probably blocking the peer
thread from shutting down properly.


Gambit
Re: Close down connections from the server side. Indy.
#5412
Author: Ulrik Nilsson
Date: Mon, 06 Sep 2004 20:06
62 lines
2824 bytes
"Remy Lebeau \(TeamB\)" <no.spam@no.spam.com> wrote:
>
>"Ulrik Nilsson" <di2niul@chl.chalmers.se> wrote in message
>news:413cb130$1@newsgroups.borland.com...
>
>> void __fastcall TForm1::FormCreate(TObject *Sender)
>
>Do not use the OnCreate event in C++.  It is a Delphi remnant that does not
>work properly in C++ as it can be triggered before the constructor, which is
>illegal in C++.  Use the actual constructor instead.
>
>> The problem is that my program stops at one of the above
>> lines. It doesn't matter which I use when I click the "Close"
>> button. And, of course, no OnDisconnect is fired in the client.
>
>Please show all of your OnExecute code.  You are probably blocking the peer
>thread from shutting down properly.
>
>
>Gambit
>


Hi Gambit!

Thanks for the fast reply. The current OnExecute code is this:

void __fastcall TForm1::TCPServerExecute(TIdPeerThread *AThread)
{

    while( ! AThread->Terminated  && AThread->Connection->Connected() )
    {

        AnsiString str = AThread->Connection->ReadLn("\n", 2000, 10);
        //AnsiString str = AThread->Connection->ReadLn();

        if( str == "HELLO" )
            AThread->Connection->WriteLn("Welcome here...");

        else if( str == "QUIT" )
        {
            AThread->Connection->WriteLn("OK");
            AThread->Connection->Disconnect();
        }
        else if( str == "MYIPADR" )
        {
            AnsiString IP = AThread->Connection->Socket->Binding->PeerIP;
            AnsiString Port = IntToStr( AThread->Connection->Socket->Binding->PeerPort );
            AnsiString str = "Your IP-Address is: " + IP + " and portnumber is: " + Port;
            AThread->Connection->WriteLn( str );
        }
        else
            AThread->Connection->CheckForDisconnect();
    }

}

You wrote "Use the actual constructor instead". How and where?

Regards Ulrik
>
Re: Close down connections from the server side. Indy.
#5413
Author: Remy Lebeau (TeamB)
Date: Mon, 06 Sep 2004 21:33
31 lines
1963 bytes
"Ulrik Nilsson" <di2niul@chl.chalmers.se> wrote in message
news:413cc343$1@newsgroups.borland.com...

>     while( ! AThread->Terminated  && AThread->Connection->Connected() )

You should not be looping like that.  The OnExecute event is already looped
for you.  Get rid of the loop altogether.

>             AThread->Connection->Disconnect();

Use DisconnectSocket() instead of Disconnect().

>             AThread->Connection->CheckForDisconnect();

You don't need to call that.  ReadLn() and WriteLn() will do that for you.

> You wrote "Use the actual constructor instead". How and where?

You do know what a constructor is in the first place, don't you?  It is a
class method that has the same name as the class itself.  In this case,
TForm1(), which is automatically generated for you by the IDE for VCL forms:

    __fastcall TForm1::TForm1(TComponent *Owner)
        : TForm(Owner)
    {
        // this is the constructor.  Put your initialization code in here...
    }


Gambit
Re: Close down connections from the server side. Indy.
#5415
Author: Ulrik Nilsson
Date: Tue, 07 Sep 2004 07:25
69 lines
3551 bytes
"Remy Lebeau \(TeamB\)" <no.spam@no.spam.com> wrote:
>
>"Ulrik Nilsson" <di2niul@chl.chalmers.se> wrote in message
>news:413cc343$1@newsgroups.borland.com...
>
>>     while( ! AThread->Terminated  && AThread->Connection->Connected() )
>
>You should not be looping like that.  The OnExecute event is already looped
>for you.  Get rid of the loop altogether.
>
>>             AThread->Connection->Disconnect();
>
>Use DisconnectSocket() instead of Disconnect().
>
>>             AThread->Connection->CheckForDisconnect();
>
>You don't need to call that.  ReadLn() and WriteLn() will do that for you.
>
>> You wrote "Use the actual constructor instead". How and where?
>
>You do know what a constructor is in the first place, don't you?  It is a
>class method that has the same name as the class itself.  In this case,
>TForm1(), which is automatically generated for you by the IDE for VCL forms:
>
>    __fastcall TForm1::TForm1(TComponent *Owner)
>        : TForm(Owner)
>    {
>        // this is the constructor.  Put your initialization code in here...
>    }
>
>
>Gambit
>
>

Hi!

Thanks for the reply.

I've already tried to run the OnExecute method whitout a while(...) {...} loop with no luck. But thanks for the reason why.

As you guessed that didn't help me. Maybe I should have told you in the first place but I still get the "Terminate Thread Timeout" from the server application. At the moment I run 
both the client and server application on the same machine.

Well, let's say that the code in the server application is correct. Then the problem must be in the client application. Is there any special "trick" you should do? 

For example my close function looks like this:

void __fastcall TForm1::QUIT_ButtonClick(TObject *Sender)
{
    if( TCPClient->Connected() )
    {
        TCPClient->WriteLn("QUIT");
        lstMain->Items->Add( TCPClient->ReadLn() );
        TCPClient->Disconnect();
    }
    else
        lstMain->Items->Add("You're not connected...");
}


When I click on this button everything works fine. The threads close as I think they should and the OnDisconnet is fired on both sides.

I have also another question. Is it possible to debug the Indy sourcecode via some setting? I can't see a thing whats going on inside the functions call. It would be very useful...

Yes I do know what a constructor is. But, if you study the examples shipped with BCB6, they also use the OnCreate method to start the server.

Regards Ulrik
Re: Close down connections from the server side. Indy.
#5416
Author: Remy Lebeau (TeamB)
Date: Tue, 07 Sep 2004 08:48
39 lines
2409 bytes
"Ulrik Nilsson" <di2niul@chl.chalmers.se> wrote in message
news:413d624f$1@newsgroups.borland.com...

> As you guessed that didn't help me. Maybe I should have told
> you in the first place but I still get the "Terminate Thread Timeout"
> from the server application.

You never said anything about getting that error before.  The only way that
error can occur is if the peer threads are not terminating in a timely
fashion.  Given the code you have shown so far, taking into account the
changes I mentioned earlier, that should never be happening.

> At the moment I run both the client and server
> application on the same machine.

Makes no difference whether the client is on the same machine or halfay
around the world..

> Well, let's say that the code in the server application is
> correct. Then the problem must be in the client application.
> Is there any special "trick" you should do?

The client has nothing to do with the server's inability to shut down
properly.

> For example my close function looks like this:

There is nothing wrong with that code.

> Yes I do know what a constructor is. But, if you study
> the examples shipped with BCB6, they also use the
> OnCreate method to start the server.

Ignore those examples.  They are C++ translations of Delphi examples and
were translated too literally.


Gambit
Re: Close down connections from the server side. Indy.
#5417
Author: Ulrik Nilsson
Date: Tue, 07 Sep 2004 13:31
66 lines
2759 bytes
"Remy Lebeau \(TeamB\)" <no.spam@no.spam.com> wrote:
>
>"Ulrik Nilsson" <di2niul@chl.chalmers.se> wrote in message
>news:413d624f$1@newsgroups.borland.com...
>
>> As you guessed that didn't help me. Maybe I should have told
>> you in the first place but I still get the "Terminate Thread Timeout"
>> from the server application.
>
>You never said anything about getting that error before.  The only way that
>error can occur is if the peer threads are not terminating in a timely
>fashion.  Given the code you have shown so far, taking into account the
>changes I mentioned earlier, that should never be happening.

Hmmm...


Hi, again!

Well this dosen't look good.

I just can't see whats wrong in my application so,

if you have got the time (or a friend of yours),

could you write the client and server code on your machine and then post it here?

The specification is very simple.

Client:

Make a connetion to a server via a button click.

Close the connection to the server via a button click.

Close the application and the tcp-connection via a button click.

Send a text (command) to the server and recieve a text via a button click.

A information if the client are connected to the server or not.

Server:

Start the server via a button click.

Close the connections to all clients via a button click.

Close the application and all tcp-connections via a button click.

Answer the specified command that the client sent and reply.

A counter that tells how many current connections there is via a label.


Indy version: Indy_9_00_14

Components: TCPCLient and TCPServer
 
Language: C++


If I have missed something, post a reply and ask.


Regards Ulrik
Re: Close down connections from the server side. Indy.
#5418
Author: Remy Lebeau (TeamB)
Date: Tue, 07 Sep 2004 16:50
12 lines
1399 bytes
"Ulrik Nilsson" <di2niul@chl.chalmers.se> wrote in message
news:413db82f$1@newsgroups.borland.com...

> Indy version: Indy_9_00_14

You are using an old version of Indy.  The current version is 9.0.17.  You
should upgrade to the latest snapshot version and see ifthe problem still
persists.


Gambit
Re: Close down connections from the server side. Indy.
#5419
Author: Ulrik Nilsson
Date: Tue, 07 Sep 2004 17:04
21 lines
1623 bytes
"Remy Lebeau \(TeamB\)" <no.spam@no.spam.com> wrote:
>
>"Ulrik Nilsson" <di2niul@chl.chalmers.se> wrote in message
>news:413db82f$1@newsgroups.borland.com...
>
>> Indy version: Indy_9_00_14
>
>You are using an old version of Indy.  The current version is 9.0.17.  You
>should upgrade to the latest snapshot version and see ifthe problem still
>persists.
>
>
>Gambit
>
>

Can you give me the address? I can't see it on this site:
http://www.indyproject.org/download/Files/Indy9.html

Regards Ulrik
Re: Close down connections from the server side. Indy.
#5420
Author: Remy Lebeau (TeamB)
Date: Tue, 07 Sep 2004 17:43
16 lines
1534 bytes
"Ulrik Nilsson" <di2niul@chl.chalmers.se> wrote in message
news:413dea22$1@newsgroups.borland.com...

> Can you give me the address?

You already have it.

> I can't see it on this site:
> http://www.indyproject.org/download/Files/Indy9.html

You need to look again, more closely.  That same page has the link for the
"Current Development Snapshot".


Gambit
Re: Close down connections from the server side. Indy.
#5421
Author: Ulrik Nilsson
Date: Tue, 07 Sep 2004 21:45
25 lines
1848 bytes
"Remy Lebeau \(TeamB\)" <no.spam@no.spam.com> wrote:
>
>"Ulrik Nilsson" <di2niul@chl.chalmers.se> wrote in message
>news:413dea22$1@newsgroups.borland.com...
>
>> Can you give me the address?
>
>You already have it.
>
>> I can't see it on this site:
>> http://www.indyproject.org/download/Files/Indy9.html
>
>You need to look again, more closely.  That same page has the link for the
>"Current Development Snapshot".
>
>
>Gambit
>
>
It didn't help me at all. Looking forward for som code snippets from you ;)

By the way, is it possible to debug the Indy sorce files?

Regards Ulrik
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