Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

TServerSocket and TClientSocket OnWrite event

407 views
Skip to first unread message

Dan English

unread,
Dec 13, 1999, 3:00:00 AM12/13/99
to
When sending data between sockets, is it necessary to only send data in the
OnWrite event? I don't understand what this event is for? Any examples
using it?

Thanks,

Dan English
Realsoft Development


Paul Gertzen

unread,
Dec 13, 1999, 3:00:00 AM12/13/99
to
Sorry, slight typo in my previous post, should have been
SendBuffer[BytesSent] and not Buffer[BytesSent]
and SizeOfDataToSend should be SizeOf(THeader)

Paul

Paul Gertzen <pger...@livetechnology.com> wrote in message
news:8348hg$fq...@forums.borland.com...
> As I said in my previous post, the onclientread and onclientwrite events
are
> for a non-blocking TServerSocket. The example I sent you uses a
> thread-blocking server (servertype=stthreadblocking). I suggest you use
the
> same method as my example. to send data back to the client within the
> onClientExecute method, do the following:
>
> procedure TClientThread.ClientExecute;
> Type THeader = record
> datasize : integer;
> message : shortstring; // you CANNOT use string as it is
> dynamic!!!!
> End;
> var SendBuffer : PChar;
> MyHeader : THeader;
> begin
> while not Terminated and ClientSocket.Connected do
> try
> ... read the data as per my example
> ... once all data has been read and processed, write back to the
client
> as follows
> ... assume you want to send a record back (THeader)
> GetMem(SendBuffer, SizeOf(THeader));
> MyHeader.DataSize := 0; // there is no data appended to the
header
> at this stage
> MyHeader.Message := 'This is a message from the server!!';
> // Move the header into the buffer
> Move(MyHeader, SendBuffer, SizeOf(THeader));
> BytesSent := 0;
> While (ClientSocket.Socket.Connected) And (BytesSent <>
> SizeOfDataToSend) Do
> Begin
> BytesSent := BytesSent +
> ClientSocket.Socket.SendBuf(Buffer[BytesSent],
SizeOfDataToSend-BytesSent);
> End;
> ClientSocket.Close;
> except
> end;
> end;
>
> The above is pseudo-code, not everything is there but what you need.
>
> Paul
>
> Dan English <d...@realsoftdev.com> wrote in message
> news:833qb5$c5...@forums.borland.com...

Paul Gertzen

unread,
Dec 13, 1999, 3:00:00 AM12/13/99
to

Paul Gertzen

unread,
Dec 13, 1999, 3:00:00 AM12/13/99
to
More description on threadblocking vs nonblocking TServerSocket:

All the OnClientXXX events are invoked by winsock when an event occurs, this
is the nature of nonblocking sockets, they are event driven, here is the
sequence of the events:

(eg onClientConnect), from within that event, you need to read data into a
buffer for the client (receivebuf), the OnClientRead event will be invoked
until all the data has been read (calling receivebuf each time). you then
need to send to the client (sendbuf) once you are happy you have received
all the data, the onclientwrite event will be invoked if not all the data
was sent (where you will need to call sendbuf again).

I suggest you use the threadblocking servertype as this is easier to code,
easier to control, connection timeouts can be set (using TWinSocketStream)
unlike nonblocking sockets which cannot, you have no way of knowing what the
client is going to do next, making it difficult to free resources. I'm sure
the winshoes guys would agree with me.

Paul

Paul Gertzen <pger...@livetechnology.com> wrote in message
news:8348hg$fq...@forums.borland.com...

Dan English

unread,
Dec 13, 1999, 3:00:00 AM12/13/99
to
Paul: Thanks, however my app is still "non-blocking" and I was hoping to
get it working without changing too much yet.

--
Dan English
Realsoft Development
d...@realsoftdev.com

0 new messages