Article View: borland.public.cppbuilder.internet.socket
Article #5081Re: How TServerSocket sends back data to TClientSocket... ?
From: Hans Galema
Date: Thu, 14 Oct 2004 15:46
Date: Thu, 14 Oct 2004 15:46
130 lines
4739 bytes
4739 bytes
Vladimir Stefanovic wrote:
> 1) Should I send data immediatelly, while I'm still in
> ClientExecute()?
Yes.
> 2) If yes, how can I address the client? Maybe the 'Stream->'
> pointer (I think that Mr Hans Galema said that once to me)?
Indeed I told you already how to do that.
<old reply>
Date: Mon, 27 Sep 2004 21:53:51 +0200
From: Hans Galema <dontusethis@dontusethis.nl>
Newsgroups: borland.public.cppbuilder.internet.socket
Subject: Re: TClientSocket/TServerSocket data exchange...
References: <41585538@newsgroups.borland.com>
In-Reply-To: <41585538@newsgroups.borland.com>
Vladimir Stefanovic wrote:
> I want my Client to send a request to Server to get
> some file. Imaginary protocol:
>
> GET filename.txt
Allow rigth for binary files too:
GET filename.pdf
> Then, I want the Server to receive the request and to
> send back the Client the desired file contents. Imaginary
> protocol:
>
> FIL this_is_the_pure_ascii_contents_of_the_filename.txt
Better:
size FIL this_is_the_pure_contents_of_filename.doc
or
FIL size this_is_the_pure_contents_of_the_filename.doc
The last will do if the client only expects 3 char commands.
> The Server receives data through it's own thread
> stThreadBlocking & ServerSocketGetThread().
>
> How I'm supposed to SEND BACK the Client the desired
> file? Where I have to place the sending code? Should I
> have the alternative TClientSocket on Server to use for
> sending? There are some examples of how to send a row
> of text back ( SendText() ) but my file can be several KB
> so I cannot send the file back from only one pass...
Nothing of that all.
You got data from a client using something like
pStream->Read()
in your Threads Execute() method. In the same method you can use
pStream->Write()
to send data to the client.
</old reply>
> 3) Should I use the same concept of sening data like I did
> from the client side (piece by piece)?
Mostly yes.
> 4) On the clients side I suppose that I should use on OnRead(),
> what else? And here comes the main of my concerns:
If it is non-blocking yes. Sending/Receiving with Client/Server
is the same. There is only difference with blocking/non-blocking.
> 5) AFAIK, clients OnRead() is the common passage for all
> reads (it's not thread), so I have to be able to:
> - start receiving
> - extract 'sizeof_stream' when the received ammount of data
> exceeds sizeof(int)
> - continue reading while TOTAL_RECEIVED == sizeof_stream,
> then prepare for new reads
> - Clear-up if some error occure and prepare for new read
>
> Does anyone have some similar code I should look at?
Such code or atleast part of such code has often been posted here.
> Sometimes I think that it's easier to have alternative
> TClientSocket on server for sending and TServerSocket
> on client for receiving...
No. Keep on. You will do it the normal way.
> InboxDataBuffer = new TMemoryStream;
For readibility: do not call that a databuffer.
It is an instance of s TMemoryStream, so better:
InboxMemoryStream = new TMemoryStream;
> if ( NewSize == expected_size )
That will not do. The server can send several messages.
If the server sends two messages:
The client receives just pieces. Once piece can consist
of the last bytes of the first message and the first bytes
of the last message.
So for determining the size ? It is not at byte 0 of your stream
for the second message now.
Better:
if ( NewSize >= expected_size )
But yet better. Fill the memorystream just for one message,
handle every messaga separately, and then continue reading with
a clered stream.
Hans.
Technical Details
Message-ID:
<416e9e72@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-mailReferences:
<416e8327@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Thread: 11508e,5efe1ef2e1363462 X-Google-Attributes: gid11508e,public X-Google-Language: ENGLISH,ASCII-7-bit Date: Thu, 14 Oct 2004 17:46:16 +0200 From: Hans Galema <dontusethis@dontusethis.nl> User-Agent: Mozilla Thunderbird 0.7 (Windows/20040616) X-Accept-Language: en-us, en MIME-Version: 1.0 Newsgroups: borland.public.cppbuilder.internet.socket Subject: Re: How TServerSocket sends back data to TClientSocket... ? References: <416e8327@newsgroups.borland.com> In-Reply-To: <416e8327@newsgroups.borland.com> Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit NNTP-Posting-Host: 129.125.201.248 Message-ID: <416e9e72@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1097768562 129.125.201.248 (14 Oct 2004 08:42:42 -0700) Lines: 130 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