🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: Using TIdTCPServer

#561
From: Peter
Date: Thu, 24 Jan 2008 13:51
82 lines
3926 bytes
"Remy Lebeau \(TeamB\)" <no.spam@no.spam.com> wrote:
>
>"Peter" <piotrtrzeciak@yahoo.com> wrote in message 
>news:478e1c34$1@newsgroups.borland.com...
>
>> pStream = new TIdTCPStream(AContext->Connection);
>> msg = pStream->Read(imageData->rcv_buffer, 20);
>
>You don't need to use a TIdTCPStream like that.  In fact, that class is 
>primarily meant for Indy's internal use.  TIdIOHandler has its own reading 
>methods that you should use instead, ie:
>
>    TIdBytes rcv_buffer;
>    AContext->Connection->IOHandler->ReadBytes(rcv_buffer, 20);
>
>> discards it, then reads two bytes to see how long the remaining
>> transmission is:
>> msg = pStream->Read(imageData->rcv_buffer, 2);
>
>Use the IOHandler's ReadSmallInt() method instead:
>
>    calculated_length = AContext->Connection->IOHandler->ReadSmallInt();
>    AContext->Connection->IOHandler->ReadBytes(rcv_buffer, 
>calculated_length);
>
>> Now, the first two reads come back ok, but then the calculated length
>> is different on each connection (and it's the same client, so I assume it
>> should be the same), and the last read never comes back (that length is
>> usually very large, like 40k+)
>
>Sounds like you are not taking endian into account.  Is the client sending 
>the 2-byte length value in host byte order or network byte order?  It makes 
>a big difference.  If you don't process it in the correct byte order, you 
>will end up with the wrong value, potentially telling Indy to expect more 
>data than is actually sent.
>
>By default, ReadSmallInt() (and all of the other numeric reading methods) 
>expects the value to arrive in network byte order and converts it to host 
>byte order before exiting.  You can set the AConvert parameter to false to 
>skip that step if needed.
>
>
>Gambit 
>
>

That's great... Thanks!!!

However, I have lots of problems using the components, mostly due to the lack of examples. Is there a piece of C++ code I could look at, that contains examples of the usage? I have trouble writing the code for the low-level image connection. 
The original code was very simple:

while (imageData->msg > 0) {
    	imageData->msg = pStream->Read(imageData->rcv_buffer, 32000);
        fwrite(imageData->rcv_buffer, imageData->msg, 1, imageData->imgFile);
}

but now, I'm trying this:

AContext->Connection->IOHandler->CheckForDataOnSource(0);

do {

if ( !AContext->Connection->IOHandler->InputBufferIsEmpty() ) {

bufferSize = AContext->Connection->IOHandler->InputBuffer->Size;

AContext->Connection->IOHandler->ReadBytes(imageData->rcv_buffer, -1, false);

BytesToRaw(imageData->rcv_buffer, vbuff, imageData->rcv_buffer.Length);

fwrite(vbuff, imageData->msg, 1, imageData->imgFile);

AContext->Connection->IOHandler->CheckForDataOnSource(0);

}

} while ( !AContext->Connection->IOHandler->InputBufferIsEmpty());

and I get no data from the socket.

Thanks for any help!
Technical Details
Message-ID: <4798a5e1$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!feeder3.cambrium.nl!feeder1.cambrium.nl!feeder6.cambrium.nl!feed.tweaknews.nl!xlned.com!feeder1.xlned.com!feeder.news-service.com!newsfeed.freenet.de!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail
References: <478e1c34$1@newsgroups.borland.com> <478e4760@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 11508e,452e664b18b0b56c
X-Google-Attributes: gid11508e,public,usenet
X-Google-Language: ENGLISH,ASCII-7-bit
From: "Peter" <piotrtrzeciak@yahoo.com>
Sender: "Peter" <piotrtrzeciak@yahoo.com>
Reply-To: "Peter" <piotrtrzeciak@yahoo.com>
Subject: Re: Using TIdTCPServer
Newsgroups: borland.public.cppbuilder.internet.socket
X-User-Info: 207.45.45.41     
References: <478e1c34$1@newsgroups.borland.com> <478e4760@newsgroups.borland.com>
NNTP-Posting-Host: 207.105.83.66
Message-ID: <4798a5e1$1@newsgroups.borland.com>
Date: 24 Jan 2008 06:51:13 -0700
X-Trace: newsgroups.borland.com 1201186273 207.105.83.66 (24 Jan 2008 06:51:13 -0700)
Lines: 82
Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!feeder3.cambrium.nl!feeder1.cambrium.nl!feeder6.cambrium.nl!feed.tweaknews.nl!xlned.com!feeder1.xlned.com!feeder.news-service.com!newsfeed.freenet.de!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail