🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Client: ctNonBlocking -> Server: stThreadBlocking

#5248
From: Vladimir Stefanovic
Date: Mon, 27 Sep 2004 18:08
113 lines
3863 bytes
   Hi,

How can I send data with TClientSocket in ctNonBlocking
to Server. I have the sending code which only works when
the mode is ctBlocking. What can I do to modify the code
to work in ctNonBlocking mode?


const int BUFFER_SIZE = 100;

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

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

void __fastcall TcliForm::ConnectButtonClick(TObject *Sender)
{
   ClientSocket->Open();
}

void __fastcall TcliForm::DisconnectButtonClick(TObject *Sender)
{
   ClientSocket->Close();
}

void __fastcall TcliForm::ClientSocketConnect(TObject *Sender,
      TCustomWinSocket *Socket)
{
   StatusBar->SimpleText = "Connected";
}

void __fastcall TcliForm::ClientSocketDisconnect(TObject *Sender,
      TCustomWinSocket *Socket)
{
   StatusBar->SimpleText = "Disconnected";
}

void __fastcall TcliForm::SendButtonClick(TObject *Sender)
{
    if ( ( ! ClientSocket->Active ) || ( !
ClientSocket->Socket->Connected ) )
       return;

    // 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 );

       int NumberOfBytesSentZ = ClientSocket->Socket->SendBuf
 &NumberOfBytesRead, sizeof(int) );

       // if the number of sent bytes is less then ZERO it's an
error...
       if ( NumberOfBytesSentZ < 1 )
       {
          ClientSocket->Close();
          ShowMessage( "Error!" );
          return; // Error ...
       }

       // 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 )
       {
          ClientSocket->Close();
          ShowMessage( "Error!" );
          return; // Error ...
       }

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



Best regards,
Vladimir Stefanovic
Technical Details
Message-ID: <415856d5@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!news.glorb.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail

Show Full Headers
Complete Article Headers
X-Google-Thread: 11508e,b3058d6528d1daa1,start
X-Google-Attributes: gid11508e,public
X-Google-Language: ENGLISH,ASCII-7-bit
From: "Vladimir Stefanovic" <spam@not.needed>
Newsgroups: borland.public.cppbuilder.internet.socket
Subject: Client: ctNonBlocking -> Server: stThreadBlocking
Date: Mon, 27 Sep 2004 20:08:38 +0200
Lines: 116
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 5.00.2314.1300
X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2314.1300
NNTP-Posting-Host: 213.137.116.140
Message-ID: <415856d5@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1096308437 213.137.116.140 (27 Sep 2004 11:07:17 -0700)
Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!news.glorb.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail