> since 3 weeks I'm trying to send a file with TWSocket  and
> TWSocketServer components..
> my idea was to send records like:
>  pFileBuf = ^TFileBuf;
>  TFileBuf = packed record
>    ID : Integer;
>    bufSize : Integer;
>    buf: array[0..1023] of Byte;
>  end;

That's OK except you should put the bufsize in front of the record.

> so I tried to receive these records in a pointer buffer and then to
> process the buffer  like the ICS FAQ for TWSocket "Receiving high speed
> data
> <http://users.pandora.be/sonal.nv/ics/faq/TWSocket.html#Receivinghighspeeddata228>"
> and write every single record to disk in ProcessData procedure
> sometimes the first record was send correctly but the other records were
> currupt..

You probably forgot that TCP stream doesn't preserve boundaries. Even if you 
send your record in one chunk, you may receive. You _will_ receive it either 
split in several packets, or several small would be merged in a larger 
packet. Packet boundaries will not likely to match record boundaries. You 
must take care of that behaviour when receiving data.

> my WSocket1DataSent looks like this:
> ..
> var
> fbuf : TFileBuf;
> begin
> if sendnow then begin
>  if not Eof(f) then begin
>       BlockRead(f,FBuf.buf,SizeOf(FBuf.buf),numread);
>       FBuf.bufSize:=numread;
>       FBuf.ID:=2;
>      WSocket1.Send(@fbuf, SizeOf(tfilebuf));
>  end;
> end; //sendnow
> end;//proc

This will work but has a side effect which may be undersirable. Because 
TWSocket is async, calling Send is not blocking, your data will be put into 
a buffer for sending in the background. Reading the file will be much much 
faster than sending on the network. The net effect is that you'll have 
almost all data into the file stored in the buffer, taking a lot of valuable 
RAM space.

To avoid this, do not use a loop with any asynchronous, event driven 
component. Use events !
When you send data, you'll get an OnDataSent event when your data has been 
passed to winsock (which has also his own buffer). From that event handler 
read the next record and send it. No loop.

Contribute to the SSL Effort. Visit http://www.overbyte.be/eng/ssl.html

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