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.
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.
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.