🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Problem with sending data with TCS/TSS (data has been received mixed and/or damaged)

#5353
From: Vladimir Stefanovic
Date: Thu, 19 Aug 2004 07:01
129 lines
4911 bytes
Hi, (please don't be discourraged when you see the code :)

I made a simple test program for sending TMemoryStream from
TCS to TSS. The bytes has been sent but the destination copy
is usually mixed and/or damaged in some parts.

I used ctBlocking & stNonBlocking modes. When ctNonBlocking &
stThreadBlocking, the example does not work at all (which is
probably normal).

* What can I do to preserve the data consistency at the
destination?

* What is the common BUFFER_SIZE, and is there any need for it
to be configurable?

* Is there any good example/artcile/web for all these possible
techiques of sending data?


--- CLIENT SIDE ---

__fastcall TclientForm::TclientForm(TComponent* Owner)
        : TForm(Owner)
{
   OutboxDataBuffer = new TMemoryStream;
   OutboxDataBuffer->LoadFromFile( ExtractFilePath( ParamStr(0) ) + "test.txt" );
}

__fastcall TclientForm::~TclientForm()
{
   delete OutboxDataBuffer;
}

const int BUFFER_SIZE = 100;

void __fastcall TclientForm::SendButtonClick(TObject *Sender)
{
    // buffer to accept portions of the memory stream...
    BYTE Buffer [ BUFFER_SIZE ];

    // current position of sent data...
    int CurrentPosition = 0;

    // needed to calculate the size of buffer because the last portion/s is/are usually smaller...
    int Delta;

    // while current sent position hasn't yet achieved the memory stream size...
    while ( ( Delta = OutboxDataBuffer->Size - CurrentPosition ) > 0 )
    {
       // should I use the full buffer size or truncated because of last portion...
       int NumberOfBytesRead = ( Delta < BUFFER_SIZE ? Delta : BUFFER_SIZE );

       // positions memory stream to the last sent position...
       OutboxDataBuffer->Seek( CurrentPosition, soFromBeginning );

       // reads the previously calculated amount of bytes from memory stream...
       OutboxDataBuffer->ReadBuffer( Buffer, NumberOfBytesRead );

       // send the buffer and give me the actually sent bytes
       int NumberOfBytesSent = ClientSocket->Socket->SendBuf( Buffer, NumberOfBytesRead );

       // if the number of sent bytes is less then ZERO it's an error...
       if ( NumberOfBytesSent < 1 )
       {
          return; // Error ...
       }

       // our new position is incresed by number of bytes actually sent...
       CurrentPosition += NumberOfBytesSent;
    }
}


--- SERVER SIDE ---

__fastcall TserverForm::TserverForm(TComponent* Owner)
        : TForm(Owner)
{
   InboxDataBuffer = new TMemoryStream;
}

__fastcall TserverForm::~TserverForm()
{
   delete InboxDataBuffer;
}

const int TOTAL_SIZE = 44800; // Expected file length in bytes

void __fastcall TserverForm::ServerSocketClientRead(TObject *Sender,
      TCustomWinSocket *Socket)
{
    // how many bytes are read in this pass...
    int IncomingSize = Socket->ReceiveLength();

    // check if any data is available to read at all...
    if ( IncomingSize < 1 ) return;

    // read how many bytes have already been stored into buffer...
    int OldSize = InboxDataBuffer->Size;

    // calculate the new size that has to be into buffer after receiving...
    int NewSize = OldSize + IncomingSize;

    // increse the size of the buffer to allow the new storage...
    InboxDataBuffer->Size = NewSize;

    // read the incoming data into the buffer...
    Socket->ReceiveBuf( InboxDataBuffer->Memory, IncomingSize );

    // check if the whole amount of data has been received...
    if ( NewSize == TOTAL_SIZE )
    {
       // increse the size of the buffer to allow the new storage...
       InboxDataBuffer->Size = NewSize;
       
       // resets the stream position to beginning (maybe this is not necessary)...
       InboxDataBuffer->Position = 0;
       
       // unfortunately, receiving finished with some mess into Copy.txt...
       InboxDataBuffer->SaveToFile( ExtractFilePath( ParamStr(0) ) + "Copy.txt" );
    }
}



Best regards,
Vladimir Stefanovic
Technical Details
Message-ID: <41245031$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!proxad.net!gatel-ffm!gatel-ffm!194.25.134.126.MISMATCH!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail

Show Full Headers
Complete Article Headers
X-Google-Thread: 11508e,9d2d5d657ea2710a,start
X-Google-Attributes: gid11508e,public
X-Google-Language: ENGLISH,ASCII-7-bit
From: "Vladimir Stefanovic" <antivari@ptt.yu>
Sender: "Vladimir Stefanovic" <antivari@ptt.yu>
Reply-To: "Vladimir Stefanovic" <antivari@ptt.yu>
Subject: Problem with sending data with TCS/TSS (data has been received mixed and/or damaged)
Newsgroups: borland.public.cppbuilder.internet.socket
X-User-Info: 213.137.116.133     
X-Forwarded-For: 128.128.129.165
NNTP-Posting-Host: 207.105.83.66
Message-ID: <41245031$1@newsgroups.borland.com>
Date: 19 Aug 2004 00:01:05 -0700
X-Trace: newsgroups.borland.com 1092898865 207.105.83.66 (19 Aug 2004 00:01:05 -0700)
Lines: 131
Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!proxad.net!gatel-ffm!gatel-ffm!194.25.134.126.MISMATCH!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail