🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: a IdTCPClient solution

#3701
From: Remy Lebeau (TeamB)
Date: Fri, 22 Jul 2005 17:14
89 lines
3824 bytes
"Roberto Zais" <rzaiss@tiscali.it> wrote in message
news:42e0c5b2@newsgroups.borland.com...
> First of all , please,  I have a question :  is  TCPClient->Disconnect();
> innocuous  even if already disconnected , too ??
> I didn' t find this information in the help.

> so, I have modified the code in this way :

You did not pay attention to what the earlier code was doing.  Remove the
try..catch, and size_readable will be -1 on timeout so (!size_readable) will
not work.  Use (size_readable < 1) instead.

> I remember at the beginning, I set the try-catch because it seemed
> that in presence of a bad condition ( typically read timeout)  the size
> returned  by TCPClient->ReadFromStack was bad and I got blockings
> and bad results .

That is because you are not checking the size properly.

> Is size_readable really  reliable ?

Yes.  It returns how many bytes the socket actuall returned and Indy then
buffered for later reading.

> Yes, the only reason is that I fear disalignment . But ,really ,there
> is not a difference concerning disalignment .

What are you talking about?

> My packets begin with 0x16 and end with 0x4

That is fine, though including the packet size insode each packet as well
would be better.

With that said, since your packets are already framed, I would suggest not
calling ReadBuffer() until you have first validated that all of the packet
data is actually available.  For example (untested):

    TCPClient->Host = iaddress;
    TCPClient->Port = iport;
    TCPClient->ReadTimeout = 100;
    TCPClient->Connect(0);

    void __fastcall TContactThread::socket_read_procedure(void)
    {
        int size_readable = TCPClient->ReadFromStack(true, IdTimeoutDefault,
false);
        if( size_readable == -1 )
            return;

        do
        {
            char *ptr = (char*) TCPClient->InputBuffer->Memory();

            int start = MemoryPos("\x16", ptr,
TCPClient->InputBuffer->Size);
            if( start < 1 )
            {
                TCPClient->InputBuffer->Clear();
                break;
            }

            int end = MemoryPos("\x4", ptr+start,
TCPClient->InputBuffer->Size - start);
            if( end < 1 )
                break;

            if( start > 1 )
                TCPClient->InputBuffer->Remove(start-1);

            int size = ((end-start)+1);
            char *buf = new char[size];

            TCPClient->ReadBuffer(buf, size);

            // use buffer as needed...
            delete[] buf;
        }
        while( TCPClient->Connected() );
    }

> When the first byte is lost

TCP/IP guarantees data integrity.  If data is corrupted, then something is
wrong with the socket in general and it should be reporting errors anyway.


Gambit
Technical Details
Message-ID: <42e12b6b$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!news.glorb.com!newscon02.news.prodigy.com!prodigy.net!nx01.iad01.newshosting.com!newshosting.com!news-peer0-test!news-peer-test!btnet!newsfeed.bt.es!caladan.landsraad.net!newsgroups.borland.com!not-for-mail
References: <42e010be$1@newsgroups.borland.com> <42e02116@newsgroups.borland.com> <42e0c5b2@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 11508e,a84606e83349b98f
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: <42e010be$1@newsgroups.borland.com> <42e02116@newsgroups.borland.com> <42e0c5b2@newsgroups.borland.com>
Subject: Re: a IdTCPClient solution
Date: Fri, 22 Jul 2005 10:14:35 -0700
Lines: 91
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: <42e12b6b$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1122052971 68.69.17.41 (22 Jul 2005 10:22:51 -0700)
Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!news.glorb.com!newscon02.news.prodigy.com!prodigy.net!nx01.iad01.newshosting.com!newshosting.com!news-peer0-test!news-peer-test!btnet!newsfeed.bt.es!caladan.landsraad.net!newsgroups.borland.com!not-for-mail