🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Using ReceiveBuff() from a client socket app?

#1570
From: Chris
Date: Tue, 02 Jan 2007 16:58
256 lines
8673 bytes
Before the Holidays I had asked a question in the "ide" group 
about my method of sending jpegs through a socket connection. 
The response to my questions were good except for one problem I 
can't seem to fix. Just to refresh, below is the code that was 
sent to me on the server side and seems to work well.

***************************************************************
***************************************************************
#3 and #4 are good reasons NEVER to use SendStream() at all.  Use SendBuf()
instead, and pay attention to the return value.

Try this instead:

    void __fastcall SendBuf(TCustomWinSocket *Socket, void *Buffer, int
BufSize)
    {
        LPBYTE pBuf = (LPBYTE) Buffer;

        try
        {
            while( BufSize > 0 )
            {
                int NumSent = Socket->SendBuf(pBuf, BufSize);
                if( NumSent < 1 )
                {
                    if( (NumSent == -1) && (WSAGetLastError() ==
WSAEWOULDBLOCK) )
                    {
                        Application->ProcessMessages();
                        continue;
                    }
                    return false;
                }

                pBuf += NumSent;
                BufSize -= NumSent;
            }
        }
        catch(const Exception &)
        {
            return false;
        }
        return true;
    }

    bool __fastcall SendStream(TCustomWinSocket *Socket, TStream *Stream)
    {
        BYTE buffer[1024];
        int Size = Stream->Size;

        int NumToSend = (Size - Stream->Position);

        if( !SendBuf(Socket, &NumToSend, sizeof(NumToSend)) )
            return false;

        try
        {
            while( NumToSend > 0 )
            {
                int NumRead = Stream->Read(buffer, min(NumToSend,
sizeof(buffer)));
                if( NumRead < 1 )
                    return false;

                if( !SendBuf(Socket, buffer, NumRead) )
                    return false;

                NumToSend -= NumRead;
            }
        }
        catch(const Exception &)
        {
            return false;
        }

        return true;
    }


    while( !stop_images )
    {
        try
        {
            Application->ProcessMessages();
            if( stop_images )
                return;

            // set begin size of image as zero
            strm.Size(0);

            client.Image(strm, imFULL);// imHALF and imFULL

            strm.Position(0);
            strm.Save(strFileName.c_str());

            if( ServerSocket1->Socket->ActiveConnections > 0 )
            {
                TCustomWinSocket *socket =
ServerSocket1->Socket->Connections[0]; // use an appropriate index

                TStream* ftream = new TFileStream(strFileName.c_str(),
fmOpenRead | fmShareDenyWrite);
                try
                {
                    SendStream(socket, fstream);
                }
                __finally {
                    delete fstream;
                }
            }
        }
        catch(const Exception &)
        {
        }
    }

On the client side, you can read the first 4 bytes to get the stream size,
and then use ReceiveBuf() to keep reading until the specified number of
bytes have been recieved before then processing the stream data as needed.

***************************************************************
***************************************************************


The problem that I am having now is on the client side. I do 
recieve the the first 4 bytes using ReceiveBuff() to get the 
correct size that I am trying to receive. However, when I use 
ReceiveBuff() for the second time to start receiving the 1st 
packet, I get an error, "Asynchronous socket error 10053."

For the OnRead Event of the Client Socket I call a function 
called DrawImage(). Below is my code:

//------------------------------------------------------------------
void __fastcall TForm1::ClientSocket1Read(TObject *Sender,
      TCustomWinSocket *Socket)
{
         if(connected && !client_reading){
                CurrentBufferPosition++;

                if(LastBufferPosition == CurrentBufferPosition - 1){
                        client_reading = true;
                        DrawImage(Socket);
                        client_reading = false;
                }
         }
}
//---------------------------------------------------------------------------

bool TForm1::DrawImage(TCustomWinSocket *socket)
{
        TStream* fstream;

        try{
                if(CurrentBufferPosition == 1){
                        int buffSize = GetStreamSize(socket);
                        bufferSize = buffSize;
                }
                else if(bufferSize > 0){                    
                        ReceiveStream(socket,bufferSize, ftream);
                }
        }
        catch(const Exception &)
        {
            return false;
        }

        return true;
}
//------------------------------------------------------------------------------
bool __fastcall TForm1::ReceiveStream(TCustomWinSocket *socket, int Size,  TStream *Stream)
{
        void *buffer;

        try{
                
             
                while( Size > 0 )
                {
                        Application->ProcessMessages();
                    

                        int NumToReceive = socket->ReceiveLength();   
                        int NumReceived = socket->ReceiveBuf(buffer, NumToReceive);


                        if( NumReceived < 1 )
                        {
                                if( (NumReceived == -1) && (WSAGetLastError() == WSAEWOULDBLOCK) )
                                {
                                        
                                        Application->ProcessMessages();
                                        continue;
                                }
                                return false;
                        }

                        if(NumToReceive == NumReceived)
                        {
                                Stream->Write(buffer,Stream->Position);
                                //buffStream += buffer;
                                bufferSize -= NumReceived;
                                LastBufferPosition = CurrentBufferPosition;

                                if(bufferSize == 0){
                                        LastBufferPosition = 0;
                                        CurrentBufferPosition = 0;
                                }

                               

                        }
                        else
                                return false;
                    //}
                }
             
        }
        catch(const Exception &)
        {
            return false;
        }


        return true;
}
//------------------------------------------------------------------------------
int TForm1::GetStreamSize(TCustomWinSocket *socket)
{
        int Size = 0;
        void *buffSize = &Size;

        try{
                int NumToReceive = socket->ReceiveLength();                  int NumReceived = socket->ReceiveBuf(buffSize, NumToReceive); 
                
                if(NumToReceive == NumReceived){
                        LastBufferPosition = CurrentBufferPosition;
                        return Size;
                }
                else
                        return 0;
        }
        catch(const Exception &)
        {
            return 0;
        }
}
//------------------------------------------------------------------------------

I have tried using different ports as well. I appreciate any 
help on my problem.

Thanks,

Chris
Technical Details
Message-ID: <459a8f2a$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news2.google.com!news3.google.com!border1.nntp.dca.giganews.com!nntp.giganews.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,41e3cddf77758900,start
X-Google-Attributes: gid11508e,public
X-Google-Language: ENGLISH,ASCII-7-bit
From: "Chris" <nospam@help.com>
Sender: "Chris" <nospam@help.com>
Reply-To: "Chris" <nospam@help.com>
Subject: Using ReceiveBuff() from a client socket app?
Newsgroups: borland.public.cppbuilder.internet.socket
X-User-Info: 70.46.79.254     
NNTP-Posting-Host: 207.105.83.66
Message-ID: <459a8f2a$1@newsgroups.borland.com>
Date: 2 Jan 2007 08:58:18 -0800
X-Trace: newsgroups.borland.com 1167757098 207.105.83.66 (2 Jan 2007 08:58:18 -0800)
Lines: 256
Path: arc1-ca.pugleaf.net!g2news2.google.com!news3.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail