🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: Hangs on socket read, despite timeout & antifreeze

#2657
From: Remy Lebeau (TeamB)
Date: Mon, 06 Mar 2006 21:44
62 lines
3350 bytes
"DWilkinson" <dennis.c.wilkinson@boeing.com> wrote in message
news:440c9e1f$1@newsgroups.borland.com...

> The reason I was using Recv() rather than ReadBuffer() is
> that I get no response other than a time-out when I do.

The only way that can happen is if you are trying to read more data than is
actually being sent.  Like most of the other reading methods, ReadBuffer()
waits for the entire requested data block to be available before it exits.
If you ask it to read 100 bytes, for example, then it will not return until
at 100 bytes are available (or until a timeout or error occurs).

> However, in my application, I don't know size of the incoming
> message beforehand, other than the maximums.

Why is that?  That is not a very good design.

If you have control over the design of the protocol, then the best choice is
to send a fixed-length value specifying the length of the following data,
before then sending the actual data.  That way, the receiver can read the
length first, since it always knows how many bytes to read for that value,
and then it can use that value to finish reading the remaining data that
follows it.  You can use the Read/WriteInteger() methods for that, for
example:

    IdTCPClient1->WriteInteger(size of my data);
    IdTCPClient1->WriteBuffer(my data, size of my data);
    ...
    int BufSize = IdTCPClient1->ReadInteger();
    // allocate a memory buffer as needed, then ...
    IdTCPClient1->ReadBuffer(Buffer, BufSize);

If you don't have control over the design, and must read whatever is
available regardless of the length, then use the ReadFromStack() method.  It
returns how many bytes were actually received, or -1 on timeout.

    int NumRead = IdTCPClient1->ReadFromStack(true, IdTimeoutDefault,
false);
    if( NumRead > 0 )
    {
        // allocate a buffer as needed, then...
        IdTCPClient1->ReadBuffer(Buffer, NumRead);
        // use buffer as needed...
    }
    else if( NumRead == 0 )
    {
        // disconnected, the ClosedGracefully property will be set
        // to true, and any further reading/writing operations on the socket
        // will throw an EIdConnClosedGracefully exception
        //
        // do something else ...
    }
    else
    {
        // reading timeout, no data received yet
        //
        // do something else...
    }


Gambit
Technical Details
Message-ID: <440cad0f$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!news4.google.com!newshub.sdsu.edu!logbridge.uoregon.edu!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail
References: <4408c710$1@newsgroups.borland.com> <4408f04d$3@newsgroups.borland.com> <440c9e1f$1@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 11508e,78f66e549d65ce51
X-Google-Attributes: gid11508e,public
X-Google-Language: ENGLISH,ASCII-7-bit
From: "Remy Lebeau \(TeamB\)" <no.spam@no.spam.com>
Newsgroups: borland.public.cppbuilder.internet.socket
References: <4408c710$1@newsgroups.borland.com> <4408f04d$3@newsgroups.borland.com> <440c9e1f$1@newsgroups.borland.com>
Subject: Re: Hangs on socket read, despite timeout & antifreeze
Date: Mon, 6 Mar 2006 13:44:56 -0800
Lines: 64
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: 68.69.17.41
Message-ID: <440cad0f$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1141681423 68.69.17.41 (6 Mar 2006 13:43:43 -0700)
Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!news4.google.com!newshub.sdsu.edu!logbridge.uoregon.edu!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail