Link to home
Start Free TrialLog in
Avatar of Torus
Torus

asked on

Send Record via Winsock

I have defined a record and want to transfer to a custom server using Delphi 5 built-in winsock VCL. The record
size is 5000 bytes or more. In local lan, it should be no problem. The server can receive the record completely in
triggering the ServerSocket1ClientRead event(TServerSocket) once time. However, if in the slow network, the ServerSocket1ClientRead event need triggering 2 or 3 times before I can get the complete record.(use receivebuf(TCustomWInSock) to get data)

In this situation, how can I know I get the data completely and concat the data getting in several time from the event so that I can assign back the data to my
defined record variable??  




Avatar of DragonSlayer
DragonSlayer
Flag of Malaysia image

One way is to add some special ID to identify the beginning and the end of your record... perhaps something like this:

type
  TMyRecord = packed record
    HeaderID: array [0 .. 1] of Char;
    // other structures
    TailID: array [0 .. 1] of Char;
  end;

set HeaderID and TailID to something unique, that won't exist as part of your normal data... e.g. HeaderID = '##' and TailID = '%%'

so in your ReceiveBuf, when you see that you have reached %% it is time to put the next byte(s) into a new record... and if you did not reach %% but you see ##, that means the previous record was corrupt and you need to re-transfer.
ASKER CERTIFIED SOLUTION
Avatar of rondi
rondi

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Torus
Torus

ASKER

rondi, the idea works. But your pseudo-code has problem.
The move statement doesn't work.
But I can still give you mark .
Try using pointer addresses instead:
  Move(@buf[1],@brec,SizeOf(TMyBigRecord));