🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: i need ideas/comments with tclientsocket

#3230
From: Jonathan Benedicto
Date: Mon, 31 Oct 2005 15:04
75 lines
3224 bytes
Alfred wrote:
> I am making a client/server application with
> TClientSocket/TServerSocket and i have a little problem.
> - when the server read I can have only a part of the full package
> (for lag, fragmentation... ) so my idea is to make my package I send
> from client to server like it : "1234xxxxxxxxx" so 1234 is the full
> lenght of the data the server need to riceve and xxxxx are the data.
> Here is the problem , for example :
> - server get only half package, if i read the "1234"
> (Socket->ReceiveBuf(buf,4);)  and i check Socket->ReceiveLength() I
> can see the package is not complete, but If I wait till next
> ClientSocketRead for a full package I cannot read again the length
> for another check before process it. This can not be a problem with a
> single client becouse i can save the lenght into a string somewhere
> but it is a problem with a high traffic server i think. I dont like
> indy, i like to make all by myself..
> Any idea ?

How about this:

In the header file, add this:
struct TDataStruct
{
    int Length;
    TMemoryStream *DataStream;
};

In your OnClientConnect write this:
{
    TDataStruct *Data = new TDataStruct;
    Data->Length = -1;
    Data->DataStream = new TMemoryStream;
    Socket->Data = Data;
}

Then in your OnClientRead
{
    int DataLength;
    TDataStruct *Data = static_cast<TDataStruct>( Socket->Data );

    if( Data->Length == -1 )
        Socket->ReceiveBuf( &(Data->Length), sizeof( int ) );

    DataLength = min( Socket->ReceiveLength(), Data->Length );
    if( DataLength > 0 )
    {
        char *Buffer = new char[ DataLength ];

        Socket->ReceiveBuf( Buffer, DataLength );
        Data->DataStream->Write( Buffer, DataLength );

        delete[] Buffer;
    }

    Data->Length -= DataLength;
    if( Data->Length == 0 )
    {
        // process your command here.
        // it is stored in Data->DataStream.
        // the stream is cleared once you've finished.

        Data->Length = -1;
        DataStream->Clear();
    }
}

In your OnClientDisconnect
{
    TDataStruct *Data = static_cast<TDataStruct>( Socket->Data );
    delete Data;

    Socket->Data = NULL;
}

Jonathan
Technical Details
Message-ID: <43663272$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!nx02.iad01.newshosting.com!newshosting.com!newsfeed.kamp.net!news.germany.com!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail
References: <43662baf$1@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 11508e,5c33b8c6a9ca7fdb
X-Google-Attributes: gid11508e,public
X-Google-Language: ENGLISH,ASCII-7-bit
From: "Jonathan Benedicto" <incorrect@no.server>
Newsgroups: borland.public.cppbuilder.internet.socket
References: <43662baf$1@newsgroups.borland.com>
Subject: Re: i need ideas/comments  with tclientsocket
Date: Mon, 31 Oct 2005 10:04:25 -0500
Lines: 77
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2900.2670
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2670
X-RFC2646: Format=Flowed; Original
NNTP-Posting-Host: 69.158.116.150
Message-ID: <43663272$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1130771058 69.158.116.150 (31 Oct 2005 07:04:18 -0700)
Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!nx02.iad01.newshosting.com!newshosting.com!newsfeed.kamp.net!news.germany.com!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail