🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: TWinSocketStream and threading

#3015
From: Remy Lebeau (TeamB)
Date: Tue, 20 Dec 2005 00:29
83 lines
4203 bytes
"Maurice Anderson" <mauriceanderson@hotmail.com> wrote in message
news:43a738ea$1@newsgroups.borland.com...

> how can I modify the server code below to use
> pStream->ReadBuffer(Buffer, x) instead of
> pStream->Read(Buffer, 10).

For what purpose exactly?  What exactly do you want the code to do?  Please
be more specific.

> If I want to send binary data such as a file, can I still use
> Read or must I use ReadBuffer?

The stream's ReadBuffer() method calls the Read() method internally.
Everything ultimately goes through Read().  The only thing ReadBuffer() does
extra is to throw an exception if the amount of data received is not the
same as the amount that was requested.  If you continue to use Read()
directly, then you have to check the return value to know how much data was
actually received.

> How can I modify it to send data to a client?

Simply call the stream's Write() or WriteBuffer() method when desired.

> Once the data is read on the server, how can I send it over to the
> main thread for processing

That is up to you.  There are many techniques available for inter-thread
communication - window/thread messages, shared memory protected with a
synchronization object, etc.  What you use is up to you based on your chosen
code design.

> I know I should use sycnhronize some way, but how? Is that even wise?

Using the TThread::Synchronize() method is one possible way to do it, yes.

> Is it wise or necesarry to use a seperate thread on the client side to
> send and receive data as well?

That depends on the client's particular design, but it is not a requirement.
The server has no concept of how the client accesses its local endpoint for
the socket, and vice versa.  The decision whether or not to use threads has
nothing to do with the underlying communication over the socket, only in how
the socket is managed from a higher-level application perspective.

>   while (!Terminated && ClientSocket->Connected)
>   {
>     try
>     {
>       pStream = new TWinSocketStream(ClientSocket, 60000);

I would suggest moving the creation of the stream outside of the while()
block so that the stream instance can be re-used over and over for the
lifetime of the thread.  There's no point in creating and destroying
resources repeatedly.

>           test = sizeof(pStream);

Why are you doing that?  You are retreiving the compiled size of a memory
pointer, not the run-time size of the stream contents.  A memory pointer is
always 4 bytes on a 32-bit system.

>           // now process the request  ...
>            Form1->Memo1->Lines->Add((char *)Buffer);

That is not thread-safe.  You need to use TThread::Synchronize() in order to
access the GUI safely.  Either directly, or via the TIdSync class of Indy.

>     catch (Exception &E)

You should always catch exceptions by 'const' references:

    catch (const Exception &E)

>       HandleException();

I do not recommend using that.  All it basically does is displays a popup
message for the exception.  Not only does that block the worker thread but
the main thread as well.  You should handle the exception in your own code.


Gambit
Technical Details
Message-ID: <43a75019$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!border1.nntp.dca.giganews.com!border2.nntp.dca.giganews.com!nntp.giganews.com!meganewsservers.com!feeder2.on.meganewsservers.com!news-out.newsfeeds.com!propagator2-sterling.newsfeeds.com!in.nntp.be!caladan.landsraad.net!newsgroups.borland.com!not-for-mail
References: <43a738ea$1@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 11508e,75fde898af9e336
X-Google-Attributes: gid11508e,public
X-Google-Language: ENGLISH,ASCII-7-bit
From: "Remy Lebeau \(TeamB\)" <no.spam@no.spam.com>
Newsgroups: borland.public.cppbuilder.internet.socket
References: <43a738ea$1@newsgroups.borland.com>
Subject: Re: TWinSocketStream and threading
Date: Mon, 19 Dec 2005 16:29:27 -0800
Lines: 85
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1409
X-MIMEOLE: Produced By Microsoft MimeOLE V6.00.2800.1409
NNTP-Posting-Host: 68.69.17.41
Message-ID: <43a75019$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1135038489 68.69.17.41 (19 Dec 2005 16:28:09 -0700)
Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!border1.nntp.dca.giganews.com!border2.nntp.dca.giganews.com!nntp.giganews.com!meganewsservers.com!feeder2.on.meganewsservers.com!news-out.newsfeeds.com!propagator2-sterling.newsfeeds.com!in.nntp.be!caladan.landsraad.net!newsgroups.borland.com!not-for-mail