Your OnExecute event handler has code in it that is not thread-safe (the Memo logic), and your Download() procedure is using write buffering when it should not be. Either of those could lead to unwanted exceptions being raised that will terminate the connection.
Try this code instead:
uses
..., IdSync;
type
TMemoNotify = class(TIdNotify)
protected
fMsg: String;
procedure DoNotify; override;
public
class procedure AddToMemo(const AMsg: String);
end;
class procedure TMemoNotify.AddToMemo(const AMsg: String);
begin
with Create do begin
fMsg := AMsg;
Notify;
end;
end;
procedure TMemoNotify.DoNotify;
begin
Form1.Memo_server.Lines.Add(fMsg);
end;
procedure TForm1.IdTCPServer1Execute(AContext: TIdContext);
var
Command : String;
begin
// FYI, if you switch to TIdCmdTCPServer, you can remove this OnExecute handler
// completely and use the TIdCmdTCPServer.CommandHandlers collection instead,
// which allow you to define per-command OnCommand handlers at design-time...
Command := AContext.Connection.IOHandler.ReadLn;
TMemoNotify.AddToMemo('Execute : ' + Command + #13 + DateTimeToStr(Now));
if TextIsSame(Command, 'download') then
Download(AContext)
else if TextIsSame(Command, 'list_of_file_dir') then
list_of_file_dir(AContext);
end;
procedure Download(AContext: TIdContext);
var
s: string;
TheStream : TFileStream;
begin
with AContext.Connection do
begin
IOHandler.WriteLn('file name?');
// warning: this is a potential security risk! Don't use the client's path
// as-is, you should sanitize it first before you then use it. Otherwise,
// a malicious client can pass in a filename that is preceeded with ".."
// and other folders to access files you do not want it to access...
s := Base_Dir + IOHandler.ReadLn;
TheStream := TFileStream.Create(s, fmOpenRead or fmShareDenyWrite);
try
IOHandler.WriteLn(IntToStr(TheStream.Size));
IOHandler.Write(TheStream);
finally
TheStream.Free;
end;
end;
end;
http://forums2.atozed.com/viewtopic....9c9b2f88e8f44f