🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Reading data from TServerSocket in TClientSocket

#2970
From: Maurice Anderson
Date: Sun, 01 Jan 2006 01:18
211 lines
6980 bytes
Hello,

Thanks to Remy, I was able to send data(text/file) to a TSocket server
program.  This is true for both text and files sent to the server.  (1)
However, when trying to read data from TScoket to the ClientSocket, it does
not work.  Here is the code I put in the ClientSocket->OnRead event:

void __fastcall TForm1::ClientSocket1Read(TObject *Sender,
      TCustomWinSocket *Socket)
{

   const int BytesToReceive = Socket->ReceiveLength();
   if( BytesToReceive <= 0 )
            return;

  Memo1->Lines->Add("Server Transmitting: ");

  TMemoryStream *ServerData = new TMemoryStream;
  char *Buffer[256];
  Socket->ReceiveBuf(Buffer, BytesToReceive);
  ServerData->Write(Buffer, BytesToReceive);
  ReadPacket(ServerpStream); <- ReadPacket takes a TStream object
}

 Here is the relevant code from the server end:

void __fastcall TMyServerThread::ClientExecute(void)
{
   .................
while (!Terminated && ClientSocket->Connected)
{
        if (pStream->WaitForData(6000))
        {
           ReadPacket(pStream);
          TMemoryStream *Strm2 = new TMemoryStream;
          Strm2->Clear();
          FileName = "Thank You";
          BinaryTransfer1->SendTextMessage(Strm2, FileName);
          ClientSocket->SendBuf(Strm2->Memory, Strm2->Size);
        }
}
}

(2) Is ClientSocket->OnRead executed in a seperate thread?  (3) If not, is
it wise to execute a client read in a seperate thread?  (4) And if so, how
do I accomplish that?

These Misc Read/Write functions are defined in both the server and the
client.  I use the same functions for consistencies sake:

//--------------------------------------------------------------------------
-
bool __fastcall TForm1::ReadPacket(TStream *Stream)
{
        char type = 0x00;
        AnsiString FileName;
        if( ReadBuf(Stream, &type, sizeof(char)) )
        {    //ShowMessage(" type " + String(type));
            switch( type )
            {
                case 0x01: // text
                {
                    AnsiString Msg;
                    if( ReadString(Stream, Msg) )
                    {
                        // use msg as needed ...
                        ShowMessage(Msg);
                        return true;
                    }
                    break;
                }

                case 0x02: // file
                {
                    if( ReadFile(Stream, FileName) )
                    {
                        // use FileName as needed...
                        return true;
                    }
                    break;
                }

                default: // unknown type
                {
                    // unknown data received, do something...
                    break;
                }
            }
        }
        return false;
}

bool __fastcall TForm1::ReadBuf(TStream *Stream, void *Buffer, int BufSize)
{
        BYTE *pBuf = (BYTE*) Buffer;
        int NumRead;

        try
        {
            while( BufSize > 0 )
            {
                NumRead = Stream->Read(pBuf, BufSize);
                if( NumRead < 1 )
                    return false;

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

        return true;
}
bool __fastcall TForm1::ReadString(TStream *Stream, AnsiString &S)
{
 bool status = false;
        int length = 0;

        if( ReadBuf(Stream, &length, sizeof(int)) )
        {
            if( length > 0 )
            {
                try
                {
                    S.SetLength(length);
                }
                catch(const Exception &)
                {
                    return false;
                }
                status = ReadBuf(Stream, S.c_str(), length);
                return status;

            }
            else
            {
                S = "";
                return true;
            }
        }

        return false;
}
bool __fastcall TForm1::ReadFile(TStream *Stream, AnsiString &FileName)
{
        AnsiString fn = FileName, fn2;
        if( ReadString(Stream, fn) )
        {
            fn2 = ExtractFileName(fn);
            fn = ("C:\\temp\\" + fn2);

            int size = 0;
            if( ReadBuf(Stream, &size, sizeof(int)) )
            {
                bool error = false;

                try
                {
                    TFileStream *FS = new TFileStream(fn, fmCreate);

                    try
                    {
                        //FS->Size = size;
                        BYTE rbuf[256];
                        int bufsize;
                        bufsize = size;
                        while( size > 0 )
                        {
                           // bufsize = size;
                            if( bufsize > sizeof(rbuf) )
                                bufsize = sizeof(rbuf);

                            if( !ReadBuf(Stream, rbuf, bufsize) )
                            {
                                error = true;
                                break;
                            }

                            if( FS->Write(rbuf, bufsize) < 1 )
                            {
                                error = true;
                                break;
                            }

                            size -= bufsize;
                        }
                    }
                    __finally
                    {
                        delete FS;
                    }
                }
                catch(const Exception &)
                {
                    error = true;
                    DeleteFile(fn);
                }

                if( !error )
                {
                    FileName = fn;
                    return true;
                }
            }
        }

        return false;
}
Technical Details
Message-ID: <43b72c70$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!news3.google.com!news.glorb.com!newsfeed.hal-mli.net!feeder1.hal-mli.net!news-out.newsfeeds.com!propagator2-sterling.newsfeeds.com!in.nntp.be!caladan.landsraad.net!newsgroups.borland.com!not-for-mail

Show Full Headers
Complete Article Headers
X-Google-Thread: 11508e,eeddadf8c21d2f02,start
X-Google-Attributes: gid11508e,public
X-Google-Language: ENGLISH,ASCII-7-bit
From: "Maurice Anderson" <mauriceanderson@hotmail.com>
Newsgroups: borland.public.cppbuilder.internet.socket
Subject: Reading data from TServerSocket in TClientSocket
Date: Sat, 31 Dec 2005 17:18:53 -0800
Lines: 213
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1106
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1106
NNTP-Posting-Host: 64.30.203.223
Message-ID: <43b72c70$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1136077936 64.30.203.223 (31 Dec 2005 17:12:16 -0700)
Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!news3.google.com!news.glorb.com!newsfeed.hal-mli.net!feeder1.hal-mli.net!news-out.newsfeeds.com!propagator2-sterling.newsfeeds.com!in.nntp.be!caladan.landsraad.net!newsgroups.borland.com!not-for-mail