(The file was too large)
You can usually omit the .res file. The publish function also doesn't copy that file.
I have some small suggestions (if I may).
*) The Sleep(2000) in the Execute isn't needed anymore. Because the server has a separate socket, and will listen (and wait) until that socket is connected, the sleep can be removed.
*) Also... the repeat until loop is not needed. The Listen function waits until the connection is made. You can do one CanRead on that socket to make sure the connection is there. So no need for the loop.
*) You had a final Form1.Progress = 100% in the Execute. It would be easier (and safer) to call ShowProgress at the end of the sending and receiving. Because the pointers ALWAYS are on the end of the file, the 100% progress is automatically reported.
*) One extra thing. Code is easier to read if you do proper code indentation. You can do that automatically. In Package > Install Package you can install jcfidelazarus 2.0. If installed (and IDE restarted) you can press Ctrl+D to automatically indent your code correctly.
The only setting I usually change is Tools > Options > JCF Format Settings > Clarify > Line Breaking to Never.
(I like to break my lines manually if needed)
You get something like this.
procedure TCustomThread.Execute;
begin
if connected then
begin
if mode = 'filesend' then
begin
FileServer := TTCPBlockSocket.Create;
FileServer.CreateSocket;
FileServer.Bind(Form1.IpEdit.Text, IntToStr(StrToInt(Form1.PortEdit.Text) + 1));
FileServer.Listen; // will wait for connection
if FileServer.CanRead(60000) then // is connection made?
begin
FileConnection := TTCPBlockSocket.Create;
FileConnection.OnMonitor := @MonitorHook;
FileConnection.CreateSocket;
FileConnection.Socket := FileServer.Accept;
MainStream := TFileStream.Create(Form1.OpenFileDialog.FileName, fmOpenRead);
FileConnection.SendStream(MainStream);
Synchronize(@ShowProgress);
StatString := '>:Transfer Done!';
Synchronize(@ShowStat);
end;
end;
if mode = 'fileaccept' then
begin
FileConnection := TTCPBlockSocket.Create;
FileConnection.OnMonitor := @MonitorHook;
FileConnection.CreateSocket;
FileConnection.Connect(Form1.IpEdit.Text, IntToStr(StrToInt(Form1.PortEdit.Text) + 1));
if FileConnection.LastError = 0 then
begin
MainStream := TFileStream.Create(infile_name, fmCreate);
FileConnection.RecvStream(MainStream, 60000);
Synchronize(@ShowProgress);
if MainStream.Size = StrToInt(infile_size) then
StatString := '>:Transfer Done!'
else
StatString := '>:Transfer FAILED!';
Synchronize(@ShowStat);
end;
end;
mode := 'none';
MainStream.Free;
FileServer.Free;
FileConnection.Free;
end;
end;