🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

a IdTCPClient solution

#3694
From: Roberto Zais
Date: Thu, 21 Jul 2005 21:25
157 lines
6315 bytes
This is a client application using IdTCPClient . This application receives a
flow composed of big messages : 10Kbytes each.
I have used some code that was shown in  this  newsgroup . BUT that was a
server application .
Please , can someone say me if the application-use that I did is correct ?
It seems working ok .
[Rarely I notice  errors, They are rare but persistent ..( Connection
refused) It seems to depend of the environment or other problems (?). .]
At the bottom of the mail I report the original suggestions I have read in
this newsgroup (but, for server application)

Perhaps the solution is not the best one as speed or performace : shall I
use streams ?  How?
Are there limits to the size of data and the time related throughput ? (
beyond  the obvious limits of the network...)

Which is the more simple performant and recommendable approach ?
I had a look  at  " please help me..socket file transfer" thread , but I
feel a little confused .
More, I must use Indy client ( I have Indy9 installed OK)
Thank you all for any help or opinion
Roberto

the setup includes

TCPClient->Host = iaddress;
TCPClient->Port = iport;  //  8023
TCPClient->Connect(0);  //  Timeout - millisecondi
----------------------------------------------------------------------------
----------

This  is called cyclically

ContactThread->socket_read_procedure();

----------------------------------------------------------------------------
----------

void __fastcall TContactThread::socket_read_procedure(void)
{
AnsiString Buffer;
int a,Count,size, size_readable;
int bufsize;

TCPClient->ReadTimeout = 100;            //READ_STACK_TIMEOUT;

//  $$$ variante newsgroup
try     {
        if(TCPClient->InputBuffer->Size  == 0 )
                size_readable =
TCPClient->ReadFromStack(true,IdTimeoutDefault,true);
              else
                size_readable = TCPClient->InputBuffer->Size;
        }

       catch(Exception& e){       // Without display
          String sbuf = String(e.ClassName()) + " " + e.Message.c_str();
          MessageText = sbuf;
                                            // possible error :
EIdReadTimeout    .   Other errors ??
          if (String(e.ClassName()) == "EIdReadTimeout")
                  return;                 // lets in waiting read loop
          }
if(!size_readable)
        return;

    if(ser_start <= ser_end)
                                            // circular buffer
         size = Min((ser_end + MAX_BUF - ser_start),size_readable);
            else
                                            // buffer disposto linearmente :
                                            // spazio libero = semplice
differenza puntatori
         size = Min((ser_end - ser_start),size_readable);    // al limite,
zero

                                                         // si adotta size
in modo da leggere dati solo per lo spazio disponibile
    char *buf = new char[size];
    TCPClient->ReadBuffer(buf, size);     // are possible any errors in this
phase  ???

    serial_lock=true;
    for(a=0; a<size; a++)
        {
        mychar[ser_end]= buf[a];     // posizione attuale : posizione libera
di inserz.
        ser_end++;
        char_ready++;
        if(ser_end>(MAX_BUF-1))      // eventuale rotazione circolare buffer
           ser_end=0;
        }
    serial_lock=false;
    delete[] buf;
    return;


----------------------------------------------------------------------------
---------------------------------------------
Server solution provided  14/2/2005

"Jonathan Benedicto" <incorrect@no.server> wrote in message
news:42110245$1@newsgroups.borland.com...

> Eg. If I read data like this :

You should not be looping like that.  The OnExecute event is already looped
by TIdTCPServer.  Your code should only be processing a single iteration of
that internal loop, not running your own loop at all:

    void __fastcall TForm1::IdTCPServer1Connect(TIdPeerThread *AThread)
    {
        AThread->Connection->ReadTimeout = 1000;
    }

    void __fastcall TForm1::IdTCPServer1Execute(TIdPeerThread *AThread)
    {
        char buffer[200];

        if( AThread->Connection->InputBuffer->Size == 0 )
        {
            int read = AThread->Connection->ReadFromStack(true,
IdTimeoutDefault, false);
            if( read == -1 ) // timeout
                return;
        }

        do
        {
            int bufsize = min(AThread->Connection->InputBuffer->Size, 200);
            AThread->Connection->ReadBuffer(buffer, bufsize);
            // use buffer as needed...
        }
        while( bufsize > 0 );
    }

> Then the app can only work with 200 bytes chucks of data. Even if the
> sending app sent a 100 byte chunk and then a 300 byte chunk.

I would strongly recommend that you prefix your packets with the actual size
of the packets.  Then you don't have to limit your buffer size during
reading, and can send small and large packets alike:

    void __fastcall TForm1::IdTCPServer1Execute(TIdPeerThread *AThread)
    {
        int size = AThread->Connection->ReadInteger(true);
        if( size > 0 )
        {
            char *buffer = new char[size];
            AThread->Connection->ReadBuffer(buffer, size);
            // use buffer as needed...
            delete[] buffer;
        }
    }


Gambit
Technical Details
Message-ID: <42e010be$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!newsread.com!newsprint.newsread.com!news-out.newsfeeds.com!propagator3-LAX.newsfeeds.com!news-in.spamkiller.net!news-in.superfeed.net!caladan.landsraad.net!newsgroups.borland.com!not-for-mail

Show Full Headers
Complete Article Headers
X-Google-Thread: 11508e,a84606e83349b98f,start
X-Google-Attributes: gid11508e,public
X-Google-Language: ENGLISH,ASCII-7-bit
From: "Roberto Zais" <rzaiss@tiscali.it>
Newsgroups: borland.public.cppbuilder.internet.socket
Subject: a IdTCPClient solution
Date: Thu, 21 Jul 2005 23:25:54 +0200
Lines: 161
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1506
X-MIMEOLE: Produced By Microsoft MimeOLE V6.00.2800.1506
NNTP-Posting-Host: 212.171.10.178
Message-ID: <42e010be$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1121980606 212.171.10.178 (21 Jul 2005 14:16:46 -0700)
Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!newsread.com!newsprint.newsread.com!news-out.newsfeeds.com!propagator3-LAX.newsfeeds.com!news-in.spamkiller.net!news-in.superfeed.net!caladan.landsraad.net!newsgroups.borland.com!not-for-mail