Article View: borland.public.cppbuilder.internet.socket
Article #3236Re: i need ideas/comments with tclientsocket
From: Hans Galema
Date: Mon, 31 Oct 2005 16:22
Date: Mon, 31 Oct 2005 16:22
55 lines
3210 bytes
3210 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... ) Please wrap your lines manually. Now it is difficult to reply selective. Not only for the server. Also for a client. In this respect it's better to speak of sender and receiver. > 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. That looks like the length in ascii. Better declare an int size. int size = the_size_of_the_message; ->SendBuf ( &size, sizeof(size); ->SendBuf ( message, size ); check the return values. > 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 > gain 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. As Johan showed you: int size = 0; Socket->ReceiveBuf(&size,4); Using a TMemoryStream is fine. What you have to consider first is that if OnRead triggers that you /have/ to read all bytes indicated by ReceiveLength(). That can be very easily handled by just reading /all/ into a TMemoryStream. (Adjust Size first, And Position after). Now after having put all in the memorystream check the first four bytes. They indicate the size of the first message that is in it. int size = *(int*)MemoryStream->Memory; You should then make a loop in which you check if there is a complete message in the stream. If so handle it. If there are more (partial) in the stream then the first message then move the following messages to the start of the stream. Hans.
Technical Details
Message-ID:
<43664511@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!news-xfer.newsread.com!yellow.newsread.com!netaxs.com!newsread.com!newsfeed00.sul.t-online.de!t-online.de!195.92.193.196.MISMATCH!nntp.theplanet.net!inewsm1.nntp.theplanet.net!news-peer-test!btnet!newsfeed.bt.es!caladan.landsraad.net!newsgroups.borland.com!not-for-mailReferences:
<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 Date: Mon, 31 Oct 2005 17:22:35 +0100 From: Hans Galema <notused@notused.nl> User-Agent: Mozilla Thunderbird 1.0 (Windows/20041206) X-Accept-Language: en-us, en MIME-Version: 1.0 Newsgroups: borland.public.cppbuilder.internet.socket Subject: Re: i need ideas/comments with tclientsocket References: <43662baf$1@newsgroups.borland.com> In-Reply-To: <43662baf$1@newsgroups.borland.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit NNTP-Posting-Host: 145.98.201.248 Message-ID: <43664511@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1130775825 145.98.201.248 (31 Oct 2005 08:23:45 -0700) Lines: 55 Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!news-xfer.newsread.com!yellow.newsread.com!netaxs.com!newsread.com!newsfeed00.sul.t-online.de!t-online.de!195.92.193.196.MISMATCH!nntp.theplanet.net!inewsm1.nntp.theplanet.net!news-peer-test!btnet!newsfeed.bt.es!caladan.landsraad.net!newsgroups.borland.com!not-for-mail