Recent

Author Topic: Synapse TCP Client issue ? maybe some help  (Read 4898 times)

fpc2pas

  • New Member
  • *
  • Posts: 34
Synapse TCP Client issue ? maybe some help
« on: May 24, 2021, 06:27:26 pm »
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][+][-]
  1. type
  2.  
  3.   { TMyApplication }
  4.  
  5.   TMyApplication = class(TCustomApplication)
  6.  // cbIP: TComboBox;
  7.   protected
  8.     procedure DoRun; override;
  9.  
  10.     private
  11.     FTCPClient: TTCPClient;
  12.     FSettings: TSettings;
  13.     FTCPBase: TTCPBase;
  14.     FTCPBaseType: TBaseType;
  15.     Connection: TConnection;
  16.     procedure AddLog(ALogMsg: String);
  17.     procedure DoOnRecvMessage(Sender: TObject; AMsg: String; AParams: TStringArray; AConnection: TConnection);
  18.     procedure DoOnConnect(Sender: TObject; AConnection: TConnection);
  19.  
  20.   public
  21.   procedure execute;
  22.     constructor Create(ATCPClient: TTCPClient);
  23.     destructor Destroy; override;
  24.     procedure WriteHelp; virtual;
  25.     procedure connect_tcp; virtual;
  26.     procedure sending(ATCPBase: TTCPBase; ATCPBaseType: TBaseType; ASettings: TSettings; AConnection: TConnection);
  27.    // public
  28.   //  procedure RecvMessage(Sender: TObject;AMsg: String; AParams: TStringArray; {%H-}AConnection: TConnection; AddLog: TAddLog);
  29.   end;
  30.  
  31. { TMyApplication }
  32.  
  33. type
  34.    TAddLog = procedure(ALogMsg: String) of Object;
  35.     Tfrclient = class
  36.  
  37.   private
  38.    FTCPBase: TTCPBase;
  39.     FTCPBaseType: TBaseType;
  40.     FSettings: TSettings;
  41.     FConnection: TConnection;
  42.   public
  43.      procedure RecvMessage(Sender: TObject; AMsg: String; AParams: TStringArray; {%H-}AConnection: TConnection; {%H-}AddLog: TAddLog);
  44.   end;
  45.  
  46. procedure TMyApplication.DoRun;
  47. var
  48.   ErrorMsg: String;
  49. begin
  50.   // quick check parameters
  51.   ErrorMsg:=CheckOptions('h', 'help');
  52.   if ErrorMsg<>'' then begin
  53.     ShowException(Exception.Create(ErrorMsg));
  54.     Terminate;
  55.     Exit;
  56.   end;
  57.  
  58.   // parse parameters
  59.   if HasOption('h', 'help') then begin
  60.     WriteHelp;
  61.     Terminate;
  62.     Exit;
  63.   end;
  64.  
  65.   { add your program here }
  66.      connect_tcp;
  67.  
  68.   terminate;
  69.  
  70. end;
  71.  
  72. constructor TMyApplication.Create(ATCPClient: TTCPClient);
  73. begin
  74.  // inherited Create(TheOwner);
  75.   FTCPClient := ATCPClient;
  76.  // FSettings := ASettings;
  77.   FTCPClient := TTCPClient.Create;
  78.   FTCPClient.OnRecvMessage := @DoOnRecvMessage;
  79.   FTCPClient.OnConnect:=@DoOnConnect;
  80.  
  81.  StopOnException:=True;
  82.  
  83. end;
  84.  
  85. destructor TMyApplication.Destroy;
  86. begin
  87.   inherited Destroy;
  88. end;
  89.  
  90. procedure Tfrclient.RecvMessage(Sender: TObject;AMsg: String; AParams: TStringArray; {%H-}AConnection: TConnection;AddLog: TAddLog);
  91. var
  92. rcv_msg : string;
  93. begin
  94.   writeln('what');
  95.    case AMsg of
  96.     'TEXT':
  97.        begin
  98.           rcv_msg := AParams[0];
  99.           writeln('waiting...');
  100.           readln(rcv_msg);
  101.         end;
  102. end;
  103.  
  104. end;
  105.  
  106. procedure Tmyapplication.AddLog(ALogMsg: String);
  107. begin
  108.   writeln('log added');
  109. end;
  110.  
  111. procedure Tmyapplication.DoOnConnect(Sender: TObject; AConnection: TConnection);
  112. begin
  113. writeln('connected');
  114. end;
  115.  
  116. procedure Tmyapplication.DoOnRecvMessage(Sender: TObject; AMsg: String;
  117.   AParams: TStringArray; AConnection: TConnection);
  118. type
  119.   TAddLog = procedure(ALogMsg: String) of Object;
  120. var
  121. AddLogCallback: TAddLog;
  122. rcv_msg : string;
  123. FPCClient : Tfrclient;
  124. begin
  125.   FPCClient := Tfrclient.Create;
  126.  FPCClient.RecvMessage(Sender, AMsg, AParams, AConnection, AddLogCallBack);
  127. end;
  128.  
  129.  
  130. procedure Tmyapplication.sending(ATCPBase: TTCPBase; ATCPBaseType: TBaseType; ASettings: TSettings; AConnection: TConnection);
  131. begin
  132.  
  133.   // FTCPBase := TTCPBase.Create;
  134.    FTCPBase := ATCPBase;
  135.    FTCPBaseType := ATCPBaseType;
  136.    FSettings := ASettings;
  137.    Connection := AConnection;
  138.  
  139.  
  140. try
  141. writeln('sending a msg');
  142. FTCPBase.SendMessage('TEXT', ['8989898'], AConnection.FUniqueID);
  143. readln;
  144.  
  145. except
  146.     writeln('faild');
  147. end;
  148. end;
  149.  
  150. procedure Tmyapplication.connect_tcp;
  151. var
  152.   //  Connection: TConnection;
  153.     list : Tstringlist;
  154.     connection_status : boolean;
  155.       MS: TMemoryStream;
  156.       bla:string;
  157.       chat : Tmyapplication;
  158. begin
  159.  
  160. FSettings :=Tsettings.Create;
  161.  
  162. FTCPClient := TTCPClient.Create;
  163.  
  164. if FTCPClient.Connected = false then
  165.   // begin
  166.    connection_status := false;
  167.  
  168.     r_host := '127.0.0.1';
  169.    FSettings.ActiveConnection.FIP := r_host;
  170.    FSettings.ActiveConnection.FPort := IntToStr(4433);
  171.   FSettings.ActiveConnection.FUser := 'wtf';
  172.    FSettings.ActiveConnection.FPass := '';
  173.  
  174.   Connection := TConnection.Create;
  175.   FSettings.CopyConnection(FSettings.activeConnection, Connection);
  176.     r_host := connection.fip;
  177.  FTCPClient.Connect(FSettings.ActiveConnection);
  178.  
  179. while FTCPClient.Connected do begin
  180.    writeln('ok');
  181.  
  182. end;
  183.  
  184.  readln;
  185.  
  186.  end;        
  187.  

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

  • Hero Member
  • *****
  • Posts: 3112
Re: Synapse TCP Client issue ? maybe some help
« Reply #1 on: May 24, 2021, 07:15:37 pm »
Your app needs to call CheckSynchronize in a loop inside DoRun, along these lines:
Code: Pascal  [Select][+][-]
  1.     While not Terminated do
  2.     begin
  3.       Sleep(1);
  4.       CheckSynchronize;
  5.     end;

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: Synapse TCP Client issue ? maybe some help
« Reply #2 on: May 24, 2021, 08:06:27 pm »
Disregard my previous reply. I had thought you already converted the code to a console app before I replied.

rvk

  • Hero Member
  • *****
  • Posts: 6109
Re: Synapse TCP Client issue ? maybe some help
« Reply #3 on: May 24, 2021, 11:22:41 pm »
writeln and readln in a thread?
Wouldn't that cause lots of trouble?

fpc2pas

  • New Member
  • *
  • Posts: 34
Re: Synapse TCP Client issue ? maybe some help
« Reply #4 on: May 25, 2021, 05:43:59 am »
Disregard 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

  • New Member
  • *
  • Posts: 34
Re: Synapse TCP Client issue ? maybe some help
« Reply #5 on: May 25, 2021, 05:45:09 am »
writeln 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

  • New Member
  • *
  • Posts: 34
Re: Synapse TCP Client issue ? maybe some help
« Reply #6 on: May 25, 2021, 06:35:32 am »
@engkin thanks i think the following function
CheckSynchronize; helped me to have the first simple working version

fpc2pas

  • New Member
  • *
  • Posts: 34
Re: Synapse TCP Client issue ? maybe some help
« Reply #7 on: May 30, 2021, 10:48:20 am »
Your app needs to call CheckSynchronize in a loop inside DoRun, along these lines:
Code: Pascal  [Select][+][-]
  1.     While not Terminated do
  2.     begin
  3.       Sleep(1);
  4.       CheckSynchronize;
  5.     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

  • Hero Member
  • *****
  • Posts: 3112
Re: Synapse TCP Client issue ? maybe some help
« Reply #8 on: May 30, 2021, 02:46:09 pm »
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

  • New Member
  • *
  • Posts: 34
Re: Synapse TCP Client issue ? maybe some help
« Reply #9 on: May 30, 2021, 04:04:11 pm »
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

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][+][-]
  1.  FTCPBase.SendMessage('TEXT', [edText.Text], FConnection.FUniqueID);

while your onRecmessage function is like this
Code: Pascal  [Select][+][-]
  1. procedure Tfrclient.RecvMessage(Sender: TObject;AMsg: String; AParams: Tstringarray; {%H-}AConnection: TConnection);
  2. begin
  3.  
  4.    case Aparams[0] of
  5.     'HELP':
  6.        begin
  7.          write_help;
  8.         end;                                                                                                
  9.  

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



 

TinyPortal © 2005-2018