LNet file sending (Again) lazarus / 38-Networking and Web Programming ---------------------------------------------------------------------- dimonar · 2011-04-10 17:56 Hello. I know, that it's a frequently asked question. But I have a problem. So. I have a "client", that want to send a file (in general it can be any file, but for the beginning I want to send a short txt file). I have a form, button and TLTCPComponent. On FormCreate client tries to connect to "server". Button click causes an CanSend event. Code: [Select]procedure TForm1.TCPCanSend(aSocket: TLSocket); var   i,n : integer;   sendfile : file;   Buff : array of char; begin If sending then //check if the button clicked begin   i := 0;   AssignFile(sendfile, 'D:/jj.html'); //example file   Reset(sendfile,1);     Repeat       begin         Repeat // I try to read portion of data. May be 65k bytes, may be less.           SetLength(Buff,Length(Buff)+1); //increase buffer           BlockRead(sendfile, Buff[i], 1); //read part of the file into buffer           inc(i,1);         until (i=65000) or EoF(sendfile);         n := TCP.Send(Buff[0], Length(Buff)); //sending       end;     until Eof(sendfile) or (n = 0) or (FSendIndex > Length(Buff));     CloseFile(sendfile); end; end; On the other side "server" is trying to construct a file from received parts. Code: [Select]procedure TForm1.TCPReceive(aSocket: TLSocket); Var   filename : string;   savefile : file;   letter : Array of Char; begin  SetLength(letter,151); // For example length is 151. Like a base file length  If aSocket.Get(letter,151) > 0 then  begin   filename := 'jjj.html';    AssignFile(savefile,filename);     if not FileExists(filename) then     begin       Rewrite(savefile);     end;     Reset(savefile, SizeOf(letter));     BlockWrite(savefile,letter,1);     CloseFile(savefile);  end; end; As a result I've got an "SIGSEGV" error on the "server" side while receive. So I do not understand how to send a file correctly. Could you, please, help me to solve this problem. And sorry for my english and bad coding habit. ---------------------------------------------------------------------- Almindor · 2011-04-10 20:17 There are multiple things wrong I see here. 1. In client/oncansend you need to do .Send(buffer) either until the whole file is sent, or Send = 0 (which means "wait" and another OnCanSend will get automagically called out when you can continue) for more info read: http://lnet.wordpress.com/usage/sockets-protocols-and- sending 2. In server you open the file on each receive, you should only open it once, and then write to it (make it a field of the form for example) until the whole thing is received, or something bad happens (OnDisconnect or OnError should close the file, or even delete it as failed). It's simplest to do with a TFileStream IMO. Hope this helps.