> //send the file (client) > while ... > BlockRead(f, FileBuf.buf, SizeOf(FileBuf.buf), FileBuf.bufSize); > FBuf.ID:=1; > WSocket1.Send(@FileBuf, sizeof(FileBuf)); > end;
With such code, then the file is almost read in memory before sending anything. You forgot that the component is asynchronous. Calling Send put data into an internal buffer and try to send as much as possible without blocking. Then data is left in the buffer and will be send later when winsock send a message telling it can accept more data for sending. You should probably remove your loop and send only one data chunk at a time, sending the next one from OnDataSent event. > //ondata aviable... Here again, it looks like you forgot that the component is non blocking and TCP is a stream protocol which doesn't preserve datagram boundaries. In short, what you've sent in a single block can be either split in several smaller blocks or merged into a larger one depending on hardware and connection speed. All the data will be sent/received once, in correct order and without error but you don't know how it will be split into packets on the network. You can't assume you'll receive a TFileBuf into a single call to Receive ! You must receive data into a buffer, appending to the buffer until you have received enough data (maybe more) before processing it. Once processed, you remove the data from your buffer, without throwing away what you may already have received ! -- Contribute to the SSL Effort. Visit http://www.overbyte.be/eng/ssl.html -- [EMAIL PROTECTED] Author of ICS (Internet Component Suite, freeware) Author of MidWare (Multi-tier framework, freeware) http://www.overbyte.be ----- Original Message ----- From: "Selahattin Erkoc" <[EMAIL PROTECTED]> To: <[email protected]> Sent: Thursday, November 17, 2005 9:50 PM Subject: [twsocket] TWsocket/TWsocketServer sending/receiving Files > hi, > I have used both compenets to send/receive text but now I'm trying to > use it for sending files.. > so I tried just simple to open the file binary, read it and send it to > the Server > > TCommand = record > ID : Integer; > end; > > TFileBuf = record > ID : Integer; > bufSize : Integer; > buf: array[0..1024] of Byte; > end; > > //send the file (client) > while ... > BlockRead(f, FileBuf.buf, SizeOf(FileBuf.buf), FileBuf.bufSize); > FBuf.ID:=1; > WSocket1.Send(@FileBuf, sizeof(FileBuf)); > end; > > before sending the file, the server gets some information about it > (size, name, ...) - this works fine but if I try to send a file the > server doesn't get any new record from the client: > > //ondata aviable... > var > buf : array[Word] of Byte; > fData : TMemoryStream; > > Filebuf : ^TFileBuf; > Command : TCommand; > FileInfo : TFileInfo; > OldPos : Integer; > > begin > fData:=TMemoryStream.Create; > GetMem(Filebuf,SizeOf(TFileBuf)); > with sender as TWSocketClient do begin > nRead := Receive(@buf, High(Buf)- Low(buf)); > OldPos := fdata.Position; > fData.WriteBuffer(buf, nRead); > try > fData.Position := OldPos; > fdata.ReadBuffer(Command, SizeOf(Command)); > if Command.ID = 1 then begin > fData.Position := OldPos; > fdata.ReadBuffer(Filebuf, SizeOf(Filebuf)); > if CanWriteToFile then begin //write data to file > BlockWrite(f, Filebuf.buf, SizeOf(FileBuf.bufSize)); > currspeed:= currspeed + FileBuf.bufSize; > Numwritten:=Numwritten+FileBuf.bufSize; > end; > end else > //... code for getting Fileinfo (works) > > some strange thing I recognized was that the client was sending/reading > data in full speed while I was debugging and executing my code step by > step :( > > has anybody an idea to solve this problem or another way to send a file > except ftp or http (it shoud be recordbased)? > > ps sorry for the huge code in my mail > > thx Sela > -- > 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 -- 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
