Newsgroups : Borland : borland.public.delphi.internet.winsock : 2005 May : Reading Data from Delphi TServerSocket (OnClientRead)
| Subject: | Reading Data from Delphi TServerSocket (OnClientRead) |
| Posted by: | "Graham Powell" (grah..@deephaven.co.uk) |
| Date: | Wed, 18 May 2005 12:29:37 |
After using this component for years and years, one client has just started
sending me TCP/IP data in which packets are sometimes being lost. I have put
a packet sniffer on the network and all the data is being transmitted.
My OnClientRead event consists basically of:
var
t_str : string;
begin
t_str := Socket.ReceiveText; //Read once per event
Now do something with t_str;
end;
So then I tried this for the OnClientRead event:
var
ab : array [0..2048] of byte;
n : integer;
i : integer;
begin
repeat
n := Socket.ReceiveBuf (ab, 2048);
i := 0
while i < n do
begin
t_str := t_str + Chr (ab[i]);
i := i + 1;
end;
until n <= 0;
Now do something with t_str;
end;
And this works, no packets lost. Alternatively, multiple calls to
Socket.ReceiveText within the read event also fixes the problem.
So is this the correct approach. Within the OnClientRead event should I keep
checking for more data and only exit the routine when there is no more.
I would have expected that a single call to Socket.ReceiveText would be
sufficient. Then if more data is available another OnClientRead event should
be generated as soon as I exit.
Is it something at the client end that could be causing the problem. Trying
to send data too fast or in too larger packets maybe. I would think that the
TCP/IP protocol would handle all such issues.
This server socket is configured as non-blocking.
Any comments much appreciated
Graham