🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: TClientSocket ReceiveLength() returns large value

#4271
From: Gary Johnson
Date: Tue, 15 Mar 2005 22:09
137 lines
6082 bytes
on two different runs, the buffer size was 2760 and 5840 bytes.
i believe that the data i received in the huge buffer was valid.
code follows:

//--------------------------------------------------------------------------
-
#define ETHERNET_RCV_BUF_LENGTH 2048
#define ETHERNET_RCV_BUF_LENGTH_HUGE 8192

void __fastcall TEthernetForm::ClientSocket1Read(TObject *Sender,
      TCustomWinSocket *Socket)
{
unsigned short bufDescIndex = this->nextAvailToReadBufDescriptorIndex;
int bufferLengthInBytes = 0;
int bytesReceived = -1;


    try
    {
        //get new ethernet frame length from Socket
        bufferLengthInBytes = Socket->ReceiveLength();
        while(bufferLengthInBytes < Socket->ReceiveLength())
            bufferLengthInBytes = Socket->ReceiveLength();
        if (
                ( 0 < bufferLengthInBytes )
                && ( bufferLengthInBytes < ETHERNET_RCV_BUF_LENGTH_HUGE)
            )
        {
            //copy the new frame into our buffer ring
            if ( bufferLengthInBytes < ETHERNET_RCV_BUF_LENGTH )
            {
                bytesReceived = Socket->ReceiveBuf(
                                this->bufDescriptors[bufDescIndex].buffer,
                                bufferLengthInBytes);
            }
            else//must use larger buffer
            {
                bytesReceived = Socket->ReceiveBuf(
                                this->emergencyBuffer,
                                bufferLengthInBytes);
                this->Memo1->Lines->Add("Warning - HUGE BUFFER used for "
                                            +
IntToStr(bufferLengthInBytes));
                this->Memo1->Lines->Add("Bytes copied from HUGE BUFFER = "
                                            + IntToStr(bytesReceived));
                AnsiString activeStatus = (ClientSocket1->Active)?"Is
Active":"NOT Active";
                this->Memo1->Lines->Add( "Client - " + activeStatus );
                //the connection THINKs that it is still active but it is no
                //longer communicating with the SPU.
            }
            //we did not get the entire frame, for now skip the frame.
            //  -1 == bytesReceived indicates error.
            if (bytesReceived != bufferLengthInBytes)
            {
                this->Memo1->Lines->Add("Error - ReceiveBuf() did not write
full packet.");
                this->BuffersLostLabel->Caption = "Total Buffers Lost: "
                                        + IntToStr(++this->buffersLost);
                //count errors
                //this->NumErrorsLabel->Caption =
                //    "Total Number of Errors: " +
IntToStr(++this->totalNumErrors);
            }
            //must skip buffer since we have overlapped the buffer
descriptors
            else if (false == this->bufDescriptors[bufDescIndex].readyToRcv)
            {
                this->Memo1->Lines->Add("Error - Receive Buffer
overlapped.");
                this->BuffersLostLabel->Caption = "Total Buffers Lost: "
                                        + IntToStr(++this->buffersLost);
            }
            //update buffer descriptor parameters
            else
            {
                this->bufDescriptors[bufDescIndex].readyToRcv = false;
                this->bufDescriptors[bufDescIndex].bytesInBuffer =
bytesReceived;
                this->nextAvailToReadBufDescriptorIndex++;
                //update index if we are at the end of the buffer ring
                if(this->nextAvailToReadBufDescriptorIndex
                    > this->endBufDescriptorIndex)
                    this->nextAvailToReadBufDescriptorIndex =
this->startBufDescriptorIndex;
            }
        }
        else
        {
                this->Memo1->Lines->Add("Error - ReceiveLength() Out of
Bounds = "
                                        + IntToStr(bufferLengthInBytes) );
                this->BuffersLostLabel->Caption = "Total Buffers Lost: "
                                        + IntToStr(++this->buffersLost);
        }
    }
    catch(...)
    {
        //ShowMessage("Error Reading Socket.");
        this->Memo1->Lines->Add("Exception Thrown While Reading Socket.");
    }
}
//--------------------------------------------------------------------------
-





"Remy Lebeau (TeamB)" <no.spam@no.spam.com> wrote in message
news:4235f764$2@newsgroups.borland.com...
>
> "Gary Johnson" <gjohnson@cinele.com> wrote in message
> news:4235f30d@newsgroups.borland.com...
>
> > I use Socket->ReceiveLength() before Socket->ReceiveBuf() and
> > everything works great until i get a large value returned by
> ReceiveLength()
> > over 2000.
>
> What is the actual value?
>
> > The subsequent call to ReceiveBuf() returns a legitimate value
>
> In other words, you did receive valid data that was as large as it
claimed?
>
> > but then all communication stops even though the Active property is
still
> "true."
>
> Please show your actual code.
>
>
> Gambit
>
>
Technical Details
Message-ID: <42375d21$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!news.glorb.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail
References: <4235f30d@newsgroups.borland.com> <4235f764$2@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 11508e,d813a4525d839272
X-Google-Attributes: gid11508e,public
X-Google-Language: ENGLISH,ASCII-7-bit
Reply-To: "Gary Johnson" <gjohnson@cinele.com>
From: "Gary Johnson" <gjohnson@cinele.com>
Newsgroups: borland.public.cppbuilder.internet.socket
References: <4235f30d@newsgroups.borland.com> <4235f764$2@newsgroups.borland.com>
Subject: Re: TClientSocket  ReceiveLength() returns large value
Date: Tue, 15 Mar 2005 17:09:38 -0500
Lines: 139
Organization: Cincinnati Electronics
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1437
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1441
NNTP-Posting-Host: 66.117.195.80
Message-ID: <42375d21$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1110924577 66.117.195.80 (15 Mar 2005 14:09:37 -0800)
Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!news.glorb.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail