Page 1 of 1

Multi-Threading with idTCPServer and Unidac

Posted: Fri 03 Jan 2014 14:58
by jhuffmanmvf
I'm using the Unidac with MySQL in a Delphi 7 Application to test out using Multithreading with idTCPServer and idTCPClient. I'll explain the program a little bit.

Server Program creates a UniConnection and a UniTable on the ServerConnect Property. I then have two CommandHandlers, the first one does a locate on the table, the second one replies with a string field from the UniTable of the located record.

Code: Select all

procedure TForm1.TcpServerConnect(AThread: TIdPeerThread);
begin
  Memo1.Lines.Add('Connected from: ' +
    AThread.Connection.Socket.Binding.PeerIP);
  ClientIP := AThread.Connection.Socket.Binding.PeerIP;
  SockUniConn:=TUniConnection.Create(Nil);
  SockUniConn.ProviderName := 'MySql';
  SockUniConn.Server := 'Vision';
  SockUniConn.Username := 'Vision';
  SockUniConn.Password := '*******';
  SockUniConn.Port := 3306;
  SockUniConn.LoginPrompt := False;
  SockUniConn.Database := 'vision';
  SockUniConn.Connect;
  TempProdTbl:=TUniTable.Create(Nil);
  TempProdTbl.Connection := SockUniConn;
  TempProdTbl.TableName := 'products';
  TempProdTbl.Open;
end;

procedure TForm1.TcpServerStartHandlerCommand(ASender: TIdCommand);
begin
  TempProdTbl.Locate('Prod',ASender.Thread.Connection.ReadLn(),[]);
end;

procedure TForm1.TcpServerExecHandlerCommand(ASender: TIdCommand);
begin
  ASender.Thread.Connection.WriteLn(TempProdTbl.FieldValues['Description']);
end;

This works for one user from the created TCPClient Application, this however does not work when I do the following:

On Computer A I connect the TCPClient to the TCPServer, I then locate the record and get the string field in response, on Computer B I connect and repeat the same steps, then going back to Computer A I ask for the string field and it replies with the string field located for Computer B instead of the one for Computer A.

At this point I am quite a bit lost on how to handle this. I have never written a multi-threaded application before and the tutorials online seem to beat around the bush so to speak on how to build a program using databases. If anyone would have any insight in to how to handle this it would be greatly appreciated. I am quite sure that whatever I am doing wrong is probably quite simple. Thank you very much.