Hello Michael,

TCP will not loose messages, but does not respect packet boundaries. So
if you send 2 strings then possible a part of second string is received
in the last receive chunck of the first string etc... So you have to let
the receiver know when data ends. You can do this by using a terminating
character (LineMode / LineEnd), or by sending the length first.

Also as Francois already mentioned if you receive the return value can
be 0 or -1. Then you just exit the OnDataAvailalbe event of course.

---
Rgds, Wilfried [TeamICS]
http://www.overbyte.be/eng/overbyte/teamics.html
http://www.mestdagh.biz

Wednesday, June 14, 2006, 13:57, Thomasius, Michael wrote:

> Hallo,
> I am using the ICS now for a long time, but there is one problem which I 
> wasn't able to solve.
> Now I hope there is somebody outside, who can give me
> "illumination" ;-). I try to explain this very detailled, sorry for
> the large mail.

> I send many messages via SendStr. They can be very large (about 100KB each).

> At first my implementation:

> ===========================================================================
> I use a method called SendNext:

> procedure Tnvc_AsyncBufferedClientSocket.SendNext;
> begin
> IF (osl_SendMessages.Count > oi_nextsend) THEN BEGIN
>    IF NOT ob_sending THEN BEGIN
>       ob_sending := TRUE;
>       oSock_Client.SendStr (osl_SendMessages.Strings[oi_nextsend]);
>       inc (oi_nextsend);
>    END {IF};
> END ELSE
>    ob_sending := FALSE;
> {END {IF};
> end;

> In the event OnDataSent I do the following:

> procedure Tnvc_AsyncBufferedClientSocket.Socket_OnDataSent(Sender: TObject; 
> ErrCode: Word);
> begin
> ob_sending := FALSE;
> SendNext;
> end;

> On the receiving side I use the DataAvailable event looking like this

> procedure
> tnvc_DBGatewayServerClientConnection.ConnectionDataAvailable(Sender:
> TObject; Error : Word);
> VAR   Buffer : array [0..4096] of char;
>       ls_receive, ls_msg:String;
>       li_rec:Integer;
>       li_msglen:Integer;
> begin
> li_rec                := Receive (@Buffer, SizeOf(Buffer));
> os_newMessage         := copy (buffer,1, li_rec);

> oi_len := oi_len + Length (os_newMessage);
> os_Message := os_Message + os_newMessage;

> //Process os_message

> end;
> ===========================================================================

> Now the problem:
> If I am using this in a local network everything works fine. That
> means all data which was sent will be received.
> If I use this over the internet I lose many packets. And I don't know why.
> As I thought the SendStr-procedure works with an internal buffer,
> which guarantees that all messages which were sent will be received
> right on the other side?

> In an other implementation I do a "SendBlock". that means, I divide
> my Message in tcp-Blocks (around 1400 Bytes per Block) and wait for an
> OnDataSent after each block. This seems to work without losses.
> The problem is, that I need to have the sending asyncron, means the
> application has to handle incoming messages during sending, so I must
> implement my own thread handling - this I would like to avoid.

> I know, the description is a little bit confused, but I hope you
> understand my problem and maybe you have own experiences how to
> implement a tcp-send/receive where it is guaranteed that all messages
> which are sent will be received.

> Thanks for any hints,

> M;chael

-- 
To unsubscribe or change your settings for TWSocket mailing list
please goto http://www.elists.org/mailman/listinfo/twsocket
Visit our website at http://www.overbyte.be

Reply via email to