Jump to content
Skullcode

Twsocket Tcp Client miss some packets

Recommended Posts

i am using Twsocket as a TcpClient to communicate with a tcpserver 

 

the server sends a packet in loop that should be received by the connected Client

 

but Twsocket doesnt read all packets that sent by the server

 

it reads one and miss another and read again and miss one again  . 

 

is it a normal behavior by Twsocket ? i tested with TidtcpClient and it reads all packets normally no matter what

Share this post


Link to post

A properly written ICS application will never lose any data with TCP.  Try receiving the data with the OverbyteIcsIpStmLogTst.dpr sample,

 

Angus

 

Share this post


Link to post

Because of its async nature, reading from TWSocket is somewhat more complex than "while Read > 0 do". Just check provided demos for the sample

Share this post


Link to post
15 hours ago, Skullcode said:

Twsocket doesnt read all packets that sent by the server

it reads one and miss another and read again and miss one again

I think since TWSocket exists for 24 years, if this bug exists, we should have already seen it. I think is is likely the code you wrote which is wrong. But I can tell...

 

1) Could you please show relevant code

2) How do you know that it is no a sever issue

3) Before writing you own code, check with one of the samples

 

Share this post


Link to post
On 8/24/2020 at 3:30 PM, FPiette said:

I think since TWSocket exists for 24 years, if this bug exists, we should have already seen it. I think is is likely the code you wrote which is wrong. But I can tell...

 

1) Could you please show relevant code

2) How do you know that it is no a sever issue

3) Before writing you own code, check with one of the samples

 

The server is built with Java, i dont think that the issue within the server sense i already have another client in java works properly.

also tested in Delphi Tidtcpclient and it works Fine without missing packets. 

 

in Twsocket i am not doing anything special  , i have just placed Twsocket then ondataAvailable Event i read the data

 

procedure TForm1.asockDataAvailable(Sender: TObject; ErrCode: Word);
var
datastr : string;
begin
datastr := asock.ReceiveStrW(CP_UTF8);
Memo1.lines.add(datastr);
end;

The server is using CRLF for each packet maybe this is an issue ? 

Share this post


Link to post

If you look at any ICS components or samples that receive data, you see that you need to loop within the event to receive data with that function, you may get six bytes first time, 29K next time, loop until nothing more is returned.  If you receive a string, it may be a partial line, so you need to buffer it until the a full line is available.  ICS also has line mode that makes sure you receive full lines, but you may lose data if the last line does include CRLF.  

 

Also, you can not read UTF8 data reliably like this, because you may get a partial character with the first read.  You need to wait until you have a line, then convert from ANSI to Unicode.

 

If you used the OverbyteIcsIpStmLogTst.dpr sample I suggested, none of this would be an issue, it's all done for you.

 

Angus

Edited by Angus Robertson
  • Like 1

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×