Synapse TCP Client issue ? maybe some help lazarus / 38-Networking and Web Programming ---------------------------------------------------------------------- fpc2pas · 2021-05-24 16:27 Hi guys, recently i have a look into this amazing source code https://forum.lazarus.freepascal.org/index.php?topic=48677.0, actually is amazing and well written. currently, I am working to do that client as a console application rather than GUI. but there is some issues I am facing while not receiving anything sent from the server. but I successfully managed to send into server below is sample of code I am using Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i type   { TMyApplication }   TMyApplication = class(TCustomApplication) // cbIP: TComboBox;  protected    procedure DoRun; override;     private    FTCPClient: TTCPClient;    FSettings: TSettings;    FTCPBase: TTCPBase;    FTCPBaseType: TBaseType;    Connection: TConnection;    procedure AddLog(ALogMsg: String);    procedure DoOnRecvMessage(Sender: TObject; AMsg: String; AParams: TStringArray; AConnection: TConnection);    procedure DoOnConnect(Sender: TObject; AConnection: TConnection);   public  procedure execute;    constructor Create(ATCPClient: TTCPClient);    destructor Destroy; override;    procedure WriteHelp; virtual;    procedure connect_tcp; virtual;    procedure sending(ATCPBase: TTCPBase; ATCPBaseType: TBaseType; ASettings: TSettings; AConnection: TConnection);   // public  //  procedure RecvMessage(Sender: TObject;AMsg: String; AParams: TStringArray; {%H-}AConnection: TConnection; AddLog: TAddLog);  end; { TMyApplication } type   TAddLog = procedure(ALogMsg: String) of Object;    Tfrclient = class   private   FTCPBase: TTCPBase;    FTCPBaseType: TBaseType;    FSettings: TSettings;    FConnection: TConnection;  public     procedure RecvMessage(Sender: TObject; AMsg: String; AParams: TStringArray; {%H-}AConnection: TConnection; {%H-}AddLog: TAddLog);  end; procedure TMyApplication.DoRun;var  ErrorMsg: String;begin  // quick check parameters  ErrorMsg:=CheckOptions('h', 'help');  if ErrorMsg<>'' then begin    ShowException(Exception.Create(ErrorMsg));    Terminate;    Exit;  end;   // parse parameters  if HasOption('h', 'help') then begin    WriteHelp;    Terminate;    Exit;  end;   { add your program here }   connect_tcp;   terminate; end; constructor TMyApplication.Create(ATCPClient: TTCPClient);begin // inherited Create(TheOwner);  FTCPClient := ATCPClient; // FSettings := ASettings;  FTCPClient := TTCPClient.Create;  FTCPClient.OnRecvMessage := @DoOnRecvMessage;  FTCPClient.OnConnect:=@DoOnConnect;  StopOnExcep tion:=True; end; destructor TMyApplication.Destroy;begin  inherited Destroy;end; procedure Tfrclient.RecvMessage(Sender: TObject;AMsg: String; AParams: TStringArray; {%H-}AConnection: TConnection;AddLog: TAddLog);varrcv_msg : string;begin  writeln('what');   case AMsg of  'TEXT':       begin          rcv_msg := AParams[0];          writeln('waiting...');          readln(rcv_msg);        end;end; end; procedure Tmyapplication.AddLog(ALogMsg: String);begin  writeln('log added');end; procedure Tmyapplication.DoOnConnect(Sender: TObject; AConnection: TConnection);beginwriteln('connected');end; procedure Tmyapplication.DoOnRecvMessage(Sender: TObject; AMsg: String;  AParams: TStringArray; AConnection: TConnection);type  TAddLog = procedure(ALogMsg: String) of Object;varAddLogCallback: TAddLog;rcv_msg : string;FPCClient : Tfrclient;begin  FPCClient := Tfrclient.Create; FPCClient.RecvMessage(Sender, AMsg, AParams, AConnection, AddLogCallBack);end;  procedure Tmyapplication.sending(ATCPBase: TTCPBase; ATCPBaseType: TBaseType; ASettings: TSettings; AConnection: TConnection);begin   // FTCPBase := TTCPBase.Create;   FTCPBase := ATCPBase;   FTCPBaseType := ATCPBaseType;   FSettings := ASettings;   Connection := AConnection;  trywriteln('sending a msg');FTCPBase.SendMessage('TEXT', ['8989898'], AConnection.FUniqueID);readln; except    writeln('faild');end;end; procedure Tmyapplication.connect_tcp;var  //  Connection: TConnection;    list : Tstringlist;    connection_status : boolean;      MS: TMemoryStream;      bla:string;      chat : Tmyapplication;begin FSettings :=Tsettings.Create; FTCPClient := TTCPClient.Create; if FTCPClient.Connected = false then  // begin   connection_status := false;     r_host := '127.0.0.1';   FSettings.ActiveConnection.FIP := r_host;   FSettings.ActiveConnection.FPort := IntToStr(4433);  FSettings.ActiveConnection.FUser := 'wtf';   FSettings.ActiveConnection.FPass := '';   Connection := TConnection.Create;  FSettings.CopyConnection(FSettings.activeConnection, Connection);    r_host := connection.fip; FTCPClient.Connect(FSettings.ActiveConnection); while FTCPClient.Connected do begin   writeln('ok'); end;  readln;  end;     what I want ? * able to receive command sent from the server-side again it is better to have a look into this sample (https://forum.lazarus.freepascal.org/index.php?topic=48677.0) i am working on ---------------------------------------------------------------------- engkin · 2021-05-24 17:15 Your app needs to call CheckSynchronize in a loop inside DoRun, along these lines: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i     While not Terminated do    begin      Sleep(1);      CheckSynchronize;    end; ---------------------------------------------------------------------- engkin · 2021-05-24 18:06 Disregard my previous reply. I had thought you already converted the code to a console app before I replied. ---------------------------------------------------------------------- rvk · 2021-05-24 21:22 writeln and readln in a thread? Wouldn't that cause lots of trouble? ---------------------------------------------------------------------- fpc2pas · 2021-05-25 03:43 Quote from: engkin on May 24, 2021, 08:06:27 pmDisregard my previous reply. I had thought you already converted the code to a console app before I replied. still not, I have problems on porting the socket events to work probably in console app mode not GUI ---------------------------------------------------------------------- fpc2pas · 2021-05-25 03:45 Quote from: rvk on May 24, 2021, 11:22:41 pmwriteln and readln in a thread? Wouldn't that cause lots of trouble? this is still testing, first i have to get a workable version that can receive strings from server and interact on events like Dononconnect or DoonRecv ---------------------------------------------------------------------- fpc2pas · 2021-05-25 04:35 @engkin thanks i think the following function CheckSynchronize; helped me to have the first simple working version ---------------------------------------------------------------------- fpc2pas · 2021-05-30 08:48 Quote from: engkin on May 24, 2021, 07:15:37 pmYour app needs to call CheckSynchronize in a loop inside DoRun, along these lines: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i     While not Terminated do    begin      Sleep(1);      CheckSynchronize;    end; I am just having access violation while using CheckSynchronize, do you have an idea why that's happening? especially when sending a message from the server into client multiple times, it shows access violation ---------------------------------------------------------------------- engkin · 2021-05-30 12:46 If you post the code you already have, it would make it easier to pinpoint the problem. Don't forget to mention your OS, FPC version..etc ---------------------------------------------------------------------- fpc2pas · 2021-05-30 14:04 Quote from: engkin on May 30, 2021, 02:46:09 pmIf you post the code you already have, it would make it easier to pinpoint the problem. Don't forget to mention your OS, FPC version..etc I figured out whats the problem, it is not about CheckSynchronize, it is about the TstringArray problem on @Recvmessage event, so for example from server-side if you send the following send message command HELP  Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i  FTCPBase.SendMessage('TEXT', [edText.Text], FConnection.FUniqueID); while your onRecmessage function is like this Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure Tfrclient.RecvMessage(Sender: TObject;AMsg: String; AParams: Tstringarray; {%H-}AConnection: TConnection);begin    case Aparams[0] of    'HELP':       begin         write_help;        end;                                                                                           you will get an access violation if you send it multiple time, while if you send a msg like this format from server-side it works with no violation HELP,, I think it is a bug, but still not figure out how to resolve it using windows 10  FPC 3.2.0