• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 956
  • Last Modified:

Delphi Indy tcp server

Hello.

I try to create an application
that takes all  data from a printout and save to a uniqe filename (or insert into a database)

it seems to work  as long as I don't try to print two documents at the same time.
How can I solve this...

My code::


 private
 
    sl : TstringList;
    MyDataStr : string;



.....


procedure TfForm1.IdTCPServer1Connect(AContext: TIdContext);
begin
   sl :=TstringList.Create;
  MyDataStr:= '';

end;

procedure TForm1.IdTCPServer1Disconnect(AContext: TIdContext);
begin
  sl.Text:= MyDataStr;
  sl.SaveToFile(MyDir+ \GetUniqeFileName);
  sl.Free;
  end;
0
pucko73
Asked:
pucko73
1 Solution
 
Sinisa VukCommented:
You need create string list by context (create separate string list by each client connected)

procedure TfForm1.IdTCPServer1Connect(AContext: TIdContext);
begin
   AContext.Data := TstringList.Create;
end;

procedure TForm1.IdTCPServer1Disconnect(AContext: TIdContext);
var
  CliContext: TStringList;
begin
    CliContext:=TStringList(AContext.Data);
    CliContext.SaveToFile(MyDir+ \GetUniqeFileName); 
    AContext.Data.Free;
    AContext.Data := nil;
end; 

procedure TfForm1.IdTCPServer1Execute(AContext: TIdContext);
begin
...
    if not AContext.Connection.IOHandler.InputBufferIsEmpty then
      ...read bytes here and store in string list as 
      ...TStringList(AContext.Data).Add(....) 
end;

Open in new window


sl  and MyDataStr is no longer needed ...
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now