🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.internet.socket
Article #795

Re: ServerSocket and Threads

#795
From: Remy Lebeau (TeamB)
Date: Thu, 13 Sep 2007 07:55
61 lines
2695 bytes
"Patryk B." <pbobak@no.spam.o2.pl> wrote in message
news:op.tyk0galowzezly@dune.intranet...

> It hang beacuse i delete:
>
> if (pStream->WaitForData(CLIENTWAITTIME))

You need that line in order to detect a graceful disconnect from the client.
When WaitForData() returns true, and then Read() returns 0, the connection
has been disconnected by the client.

> I want disconnect client only when server is going to shutdown.

Then simply remove the call to Close() when WaitForData() returns false, ie:

    void __fastcall TMyServerThread::ClientExecute(void)
    {
        char buffer[BUFFERSIZE];
        int iRead;

        try
        {
            TWinSocketStream *pStream = new TWinSocketStream(ClientSocket,
CLIENTWAITTIME);
            try
            {
                while( (!Terminated) && (ClientSocket->Connected) )
                {
                    if( !pStream->WaitForData(CLIENTWAITTIME) )
                        continue;

                    iRead = pStream->Read(buffer, sizeof(buffer);
                    if( iRead < 1 )
                    {
                        if( iRead == 0 )
                            // client disconnected...
                        else
                            // socket error...

                        ClientSocket->Close();
                    }
                    else
                    {
                        // process buffer up to iRead bytes...
                    }
                }
            }
            __finally
            {
                delete pStream;
            }
        }
        catch (...)
        {
            // handle exception...
        }
    }


Gambit
Technical Details
Message-ID: <46e8ed65@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news2.google.com!news1.google.com!news2.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail
References: <op.tyjavhtkwzezly@dune.intranet> <46e81ec5$3@newsgroups.borland.com> <op.tyk0galowzezly@dune.intranet>

Show Full Headers
Complete Article Headers
X-Google-Thread: 11508e,f2f18bf54164452d
X-Google-Attributes: gid11508e,public,usenet
X-Google-Language: ENGLISH,ASCII-7-bit
From: "Remy Lebeau \(TeamB\)" <no.spam@no.spam.com>
Newsgroups: borland.public.cppbuilder.internet.socket
References: <op.tyjavhtkwzezly@dune.intranet> <46e81ec5$3@newsgroups.borland.com> <op.tyk0galowzezly@dune.intranet>
Subject: Re: ServerSocket and Threads
Date: Thu, 13 Sep 2007 00:55:44 -0700
Lines: 63
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1409
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1409
NNTP-Posting-Host: 75.83.29.61
Message-ID: <46e8ed65@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1189670245 75.83.29.61 (13 Sep 2007 00:57:25 -0700)
Path: arc1-ca.pugleaf.net!g2news2.google.com!news1.google.com!news2.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail