Reputation: 15
When I receive data from the target, E.G.:
function TSlave.RecvInteger:integer;
var
p:pointer;
len, i, sent:integer;
begin
result := -1;
len := 4;
p := @result;
sent := 0;
repeat
Sleep(10);
i := Recv(Sk, p^, len, 0);
if i <= 0 then begin
Result := -1;
break;
end;
inc(sent, i);
dec(len, i);
p :=ptr(integer(@result) + sent);
until len = 0;
end;
I have to do that just to get a perfect result, why is there sometimes packet loss when you call recv?
Upvotes: 0
Views: 558
Reputation: 182761
There's no packet loss, the packets just haven't been received yet. If you haven't gotten all the data you want or expect, just call receive again. Protocols like TCP (which I presume is what you are using) have no way to "glue bytes together" nor do they provide application-level messages. If you want to do that, you need to write code to do it.
If receive never returned fewer than the bytes you asked for, it would be almost impossible to use if you didn't know exactly how many bytes you were expecting. That would be pretty awful.
If you do know exactly how many bytes you are expecting, just keep calling the receive function until you receive that many. That sounds like your situation, so just write your own receive function that keeps calling the low-level receive function until it has accumulated the specified number of bytes. (That's not the best way to do it, but that will work.)
Upvotes: 6