Recent

Author Topic: LAMW Http-TCP server with Lnet  (Read 8206 times)

benohb

  • Full Member
  • ***
  • Posts: 208
LAMW Http-TCP server with Lnet
« on: July 01, 2021, 12:31:37 am »
This is the minimal code for making a Http / TCP server using the INET library with LAMW ...Did we forget something :-\
Note: The lnet library (lnetvisual.lpk) cannot be built  … Use only the package lnetbase.lpk
You don't need to rebuild Lazarus . Just add a package   lnetbase.lpk to your project
Code: Pascal  [Select][+][-]
  1.  
  2. unit unit1;
  3. {$mode objfpc}{$H+}
  4. interface
  5. uses
  6.   Classes, SysUtils, AndroidWidget, Laz_And_Controls,lNet;
  7. type
  8.   { TAndroidModule1 }
  9.   TAndroidModule1 = class(jForm)
  10.     jButton1: jButton;
  11.     jTimer1: jTimer;
  12.      FCon: TLTCP;
  13.     procedure AndroidModule1Create(Sender: TObject);
  14.     procedure AndroidModule1Destroy(Sender: TObject);
  15.     procedure jButton1Click(Sender: TObject);
  16.     procedure jTimer1Timer(Sender: TObject);
  17.   private
  18.     procedure OnRe(aSocket: TLSocket);
  19.   public
  20.     {public declarations}
  21.   end;
  22. var
  23.   AndroidModule1: TAndroidModule1;
  24.  
  25.  
  26. implementation
  27.  
  28. {$R *.lfm}
  29.  
  30.  
  31. { TAndroidModule1 }
  32. procedure TAndroidModule1.AndroidModule1Create(Sender: TObject);
  33. begin
  34.   FCon := TLTCP.Create(nil); // create new TCP connection
  35.   FCon.OnReceive := @OnRe;
  36.   FCon.Timeout := 100; // responsive enough, but won't hog cpu
  37.   FCon.ReuseAddress := True;
  38.   FCon.Listen(8000);
  39.  
  40.  
  41. end;
  42. procedure TAndroidModule1.AndroidModule1Destroy(Sender: TObject);
  43. begin
  44.   FCon.Free;
  45.   inherited Destroy;
  46. end;
  47. procedure TAndroidModule1.jButton1Click(Sender: TObject);
  48. begin
  49.      jTimer1.Enabled:=true;
  50. end;
  51. procedure TAndroidModule1.jTimer1Timer(Sender: TObject);
  52. begin
  53.      FCon.CallAction;
  54. end;
  55. procedure TAndroidModule1.OnRe(aSocket: TLSocket);
  56. var
  57.   s,OutputDataString: string;
  58.   n: Integer;
  59. begin
  60.   if aSocket.GetMessage(s) > 0 then begin // if we received anything (result is in s)
  61.     FCon.IterReset; // now it points to server socket
  62.     while FCon.IterNext do begin // while we have clients to echo to
  63.  
  64.  
  65.              OutputDataString := '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"' + ' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">' +
  66.         #13#10 + '<html><h1>Teste</h1></html>' + #13#10;
  67.  
  68.  
  69.       FCon.SendMessage('HTTP/1.0 200' + #13#10);
  70.       FCon.SendMessage('Content-type: Text/Html' + #13#10);
  71.       FCon.SendMessage('Content-length: ' + IntToStr(Length(OutputDataString)) + #13#10);
  72.       FCon.SendMessage('Connection: keep-alive' + #13#10);
  73.       FCon.SendMessage('Date: ' +'Thu, 06 Dec 2018 17:37:18 GMT' + #13#10);
  74.       FCon.SendMessage('Server: Lazarus Inet' + #13#10);
  75.       FCon.SendMessage('' + #13#10);
  76.       FCon.SendMessage(OutputDataString);
  77.     end;
  78.   end;
  79. end;
  80. end.  

« Last Edit: July 01, 2021, 12:48:11 am by benohb »

Mongkey

  • Full Member
  • ***
  • Posts: 179
Re: LAMW Http-TCP server with Lnet
« Reply #1 on: July 01, 2021, 01:23:01 am »
 :), Awesome, thank you, but anyway not work, got error on fcon.listen(port).

I tried this one:
https://wiki.freepascal.org/Light_Web_Server

Worked, but freezing.
« Last Edit: July 02, 2021, 01:25:53 pm by Mongkey »

Mongkey

  • Full Member
  • ***
  • Posts: 179
Re: LAMW Http-TCP server with Lnet
« Reply #2 on: July 02, 2021, 06:56:40 am »
Waiting loaded or JMPessoa to solve :D

benohb

  • Full Member
  • ***
  • Posts: 208
Re: LAMW Http-TCP server with Lnet
« Reply #3 on: July 10, 2021, 10:33:32 am »

 :) , Awesome, thank you, but anyway not work, got error on fcon.listen(port).




It has been working well...then a problem appeared
I tried to understand where the problem is  %)
Quote
I tried this one:
https://wiki.freepascal.org/Light_Web_Server

Worked, but freezing.

Yes...I'm avoiding this library
I think this example works fine
Quote
LAMW/lamw4linux/usr/share/fpcsrc/release_3_2_2/packages/fcl-web/examples/httpserver/simplehttpserver.pas


benohb

  • Full Member
  • ***
  • Posts: 208
Re: LAMW Http-TCP server with Lnet
« Reply #4 on: July 10, 2021, 10:36:28 am »

Waiting loaded or JMPessoa to solve :D

I don't understand... please explain  :D



jmpessoa

  • Hero Member
  • *****
  • Posts: 1946
Re: LAMW Http-TCP server with Lnet
« Reply #5 on: July 10, 2021, 04:03:48 pm »

Quote
I don't understand... please explain  :D

Hi, benohb!

Please,  help here!    :D :D
Lamw: Lazarus Android Module Wizard
https://github.com/jmpessoa/lazandroidmodulewizard

benohb

  • Full Member
  • ***
  • Posts: 208
Re: LAMW Http-TCP server with Lnet
« Reply #6 on: July 11, 2021, 02:32:25 am »
hhhhhh lol

I figured "jmpessoa" as class or component  :D


Hello jmpessoa  ::)
What do you think about the matter?

Quote
LAMW/lamw4linux/usr/share/fpcsrc/release_3_2_2/packages/fcl-web/examples/httpserver/simplehttpserver.pas


I have another problem (
linux)"adb install " is executed before building a package
apkbuild.pas/(RunExternalTool(Tool)
) Called twice without sequence of steps

 

Guser979

  • New Member
  • *
  • Posts: 40
Re: LAMW Http-TCP server with Lnet
« Reply #7 on: August 08, 2021, 02:15:59 am »
My best experience with TCP was using this example:

https://forum.lazarus.freepascal.org/index.php/topic,48677.msg397515.html#msg397515....

My initial need was to upload files from my phone to the example desktop server.

In my case, to test, I copied all units (.pas) and *.inc from Synapse 40   to a project's JNI folder (APPTCPClientDemo )
http://www.ararat.cz/synapse/doku.php/download

Then I added in 'uses' : blcksock, uTCPSockets, uTCPCryptoCompression

I just used parts of the Synapse TCP/IP client and server code (by GETMEM ) to  send and receive messages and files.

It worked very well integrating mobile and desktop apps.

Synapse's compatibility with LAWM is excellent.With the GETMEM code practically everything is done.
Just study Getmem's work and do what you want
Good luck.

A fragment to help. With this it is possible to connect to the server, send files and messages. Just improve, including handling errors and more
Code: Pascal  [Select][+][-]
  1. {$mode delphi}
  2. unit unit1;
  3. interface
  4.  
  5. uses
  6.  Classes, SysUtils, And_jni, And_jni_Bridge, Laz_And_Controls,
  7.  Laz_And_Controls_Events, AndroidWidget, opendialog, blcksock,
  8.  uTCPSockets, uTCPCryptoCompression,fileutil;
  9.  
  10. type
  11.  
  12.   { TAndroidModule1 }
  13.  
  14.   TAndroidModule1 = class(jForm)
  15.     jAsyncTask1: jAsyncTask;
  16.     jButton1: jButton;
  17.     jButton2: jButton;
  18.     jButton3: jButton;
  19.     jEditText1: jEditText;
  20.     jEditText2: jEditText;
  21.     jOpenDialog1: jOpenDialog;
  22.     procedure AndroidModule1JNIPrompt(Sender: TObject);
  23.     procedure AndroidModule1RequestPermissionResult(Sender: TObject;
  24.       requestCode: integer; manifestPermission: string;
  25.       grantResult: TManifestPermissionResult);
  26.     procedure jAsyncTask1DoInBackground(Sender: TObject; progress: integer; out
  27.       keepInBackground: boolean);
  28.     procedure jAsyncTask1PostExecute(Sender: TObject; progress: integer);
  29.     procedure jButton1Click(Sender: TObject);
  30.     procedure jButton2Click(Sender: TObject);
  31.     procedure jButton3Click(Sender: TObject);
  32.     procedure jOpenDialog1FileSelected(Sender: TObject; path: string;
  33.       fileName: string);
  34.  
  35.  
  36.  
  37.   private
  38.  
  39.  
  40.   public
  41.     {public declarations}
  42.         function  SendMessage(AMsg: String; AParams: array of String): Boolean;
  43.         function RecvMessage(ATimeOut: Integer; out AMsgTyp: Integer; out AMsg: String; out AParams: TStringArray): Boolean;
  44.     procedure DoError(AErrMsg: String; AErrCode: Integer);
  45.         function DoTask(Progress: integer): boolean;
  46.         function SendFile(AFileName: String): Boolean;
  47.   end;
  48.  var
  49.         AndroidModule1: TAndroidModule1;
  50.         FBlockSocket : TTCPBlockSocket;
  51.         MsgTyp:integer;
  52.         Msg:string;
  53.         Params:tstringarray;
  54. implementation
  55.  
  56. {$R *.lfm}                                                                          
  57.  
  58.  
  59.  
  60.  
  61. //To connect to the GetMem server
  62.  
  63. procedure TAndroidModule1.jButton1Click(Sender: TObject);
  64.  
  65.  
  66. begin
  67.  
  68. FBlockSocket := TTCPBlockSocket.Create;
  69.  
  70. FBlockSocket.Connect ('172.26.61.98', '53771');
  71. //the ip here is a node of a network I created with Zerotier.  
  72. // No password in server . Please
  73.  
  74.  
  75. FBlockSocket.SendString (encrypt('53771','0LOGIN:,User,0,') );
  76.  
  77. repeat
  78. until RecvMessage(500, MsgTyp, Msg, Params) ;
  79.  
  80. jEditText2.Text :=Params[0];
  81. jAsyncTask1.Execute ;
  82. end;              
  83.  
  84.  
  85.  
  86. // send text message  :
  87.  
  88. procedure TAndroidModule1.jButton2Click(Sender: TObject);
  89.  
  90. begin
  91.    SendMessage('0TEXT', [jEditText1.Text]);  
  92.  
  93. End;
  94.  
  95.  
  96. //Choose a file
  97. procedure TAndroidModule1.jButton3Click(Sender: TObject);
  98.  
  99. begin
  100.   jOpenDialog1.Show();
  101. end;
  102.  
  103.  
  104.  
  105. //Send file
  106. procedure TAndroidModule1.jOpenDialog1FileSelected(Sender: TObject;
  107.   path: string; fileName: string);
  108. begin
  109.  
  110.   SendMessage('2FILE', ['FILE',inttostr( fileutil.FileSize(path+'/'+fileName)),fileName]);
  111.   SendFile(path+'/'+fileName);
  112.  
  113. end;      
  114.  
  115. //GetMem Code Functions
  116. function TAndroidModule1.SendFile(AFileName: String): Boolean;
  117. var
  118.   ReadCnt: LongInt;
  119.   Cnt: Int64;
  120.   Buffer: Pointer;
  121.   MS: TMemoryStream;
  122.   MSComp: TMemoryStream;
  123.   FS: TFileStream;
  124.   Msg: String;
  125.   CompRate: Single;
  126. begin
  127.   Result := False;
  128.  
  129.   try
  130.     FS := TFileStream.Create(AFileName, fmOpenRead or fmShareDenyWrite);
  131.     try
  132.       FS.Position := 0;
  133.       GetMem(Buffer, FBlockSocket.SendMaxChunk);
  134.       try
  135.         Cnt := 0;
  136.         MS := TMemoryStream.Create;
  137.         try
  138.           MSComp := TMemoryStream.Create;
  139.           try
  140.             repeat
  141.  
  142.               Msg := FBlockSocket.RecvPacket(50000);
  143.               if (FBlockSocket.LastError > 0) or (Msg = 'DONE') or (Msg <> 'NEXT') then
  144.                 Break;
  145.               ReadCnt := FS.Read(Buffer^, FBlockSocket.SendMaxChunk);
  146.               if (ReadCnt > 0) then
  147.               begin
  148.                 MS.Clear;
  149.                 MS.Write(Buffer^, ReadCnt);
  150.                 MS.Position := 0;
  151.  
  152.                   FBlockSocket.SendStream(MS);
  153.  
  154.           if (FBlockSocket.LastError = 0)  then
  155.                begin
  156.                   Cnt := Cnt + MS.Size;
  157.  
  158.                 end;
  159.               end;
  160.             until (ReadCnt = 0) or (Cnt > FS.Size);
  161.             Result := FBlockSocket.LastError = 0;
  162.            if Result then showmessage(inttostr(FS.Size))
  163.  
  164.             else  showmessage('erro');
  165.  
  166.           finally
  167.             MSComp.Free;
  168.           end;
  169.         finally
  170.           MS.Free;
  171.         end;
  172.       finally
  173.         FreeMem(Buffer);
  174.       end;
  175.     finally
  176.       FS.Free;
  177.     end;
  178.   except
  179.     on E: Exception do
  180.     showmessage('erro');
  181.  
  182.   end
  183. end;  
  184.  
  185.  
  186. function TAndroidModule1.SendMessage(AMsg: String; AParams: array of String): Boolean;
  187. var
  188.   I: Integer;
  189.   Msg: String;
  190. begin
  191.   Result := False;
  192.  
  193.   Msg := AMsg;
  194.   if Length(AParams) > 0 then
  195.   begin
  196.     AMsg := AMsg + ':';
  197.     for I := Low(AParams) to High(AParams) do
  198.       AMsg := AMsg + AParams[I] + ',';
  199.   end;
  200.   AMsg := Encrypt('53771', AMsg);
  201.   FBlockSocket.SendString(AMsg);
  202.   Result := FBlockSocket.LastError = 0;
  203.   Delete(Msg, 1, 1);
  204.  
  205.   if Result then
  206.    showmessage('Send')
  207.   else
  208.     showmessage('error');
  209. end;
  210.  
  211.  
  212.  
  213. function TAndroidModule1.RecvMessage(ATimeOut: Integer; out AMsgTyp: Integer;
  214.   out AMsg: String; out AParams: TStringArray): Boolean;
  215. var
  216.   Str: String;
  217.   P: Integer;
  218.   Len: Integer;   var  m:tmemorystream;
  219. begin
  220.  Result :=true;
  221.   AMsg := '';
  222.   AMsgTyp := -1;
  223.   AParams := nil;
  224.   Str := FBlockSocket.RecvPacket(ATimeOut);
  225.  
  226.   if (FBlockSocket.LastError > 0) then
  227.   begin
  228.   Result := False;
  229.  Exit;
  230.   end;
  231.   if Trim(Str) <> '' then
  232.   begin
  233.  Str := Decrypt('53771', Str);
  234.  
  235.     P := Pos(':', Str);
  236.     if P > 0 then
  237.     begin
  238.       AMsg := Trim(System.Copy(Str, 1, P - 1));
  239.       System.Delete(Str, 1, P);
  240.       repeat
  241.         P := Pos(',', Str);
  242.         if P > 0 then
  243.         begin
  244.           Len := Length(AParams);
  245.           SetLength(AParams, Len + 1);
  246.           AParams[Len] := System.Copy(Str, 1, P - 1);
  247.           System.Delete(Str, 1, P);
  248.         end;
  249.       until (P = 0) or (Str = '') ;
  250.     end
  251.     else
  252.       AMsg := Trim(Str);
  253.     AMsgTyp := StrToIntDef(AMsg[1], -1);
  254.     if (not AMsgTyp in [0..3]) then
  255.     begin
  256.  
  257.      jEditText2.Text :='Error';
  258.       Result := False;
  259.       Exit;
  260.     end;
  261. Delete(AMsg, 1, 1);
  262.   end;
  263. end;                    
  264.  
  265.  
  266. //jAsyncTask
  267. function  TAndroidModule1.DoTask(Progress: integer): boolean;
  268.  
  269.  
  270. begin
  271.   Result:= True;
  272.  
  273.    repeat
  274.    
  275.    until RecvMessage(500, MsgTyp, Msg, Params) ;
  276.  
  277.     Result:= False;
  278. end;
  279.  
  280. procedure TAndroidModule1.jAsyncTask1DoInBackground(Sender: TObject;
  281.   progress: integer; out keepInBackground: boolean);
  282.  
  283.      begin
  284.  
  285.  keepInBackground:= DoTask(progress);
  286.  
  287.       end;
  288.  
  289.  
  290.  
  291. procedure TAndroidModule1.jAsyncTask1PostExecute(Sender: TObject;
  292.   progress: integer);
  293. begin
  294.  
  295. jAsyncTask1.Done;
  296.  jEditText2.Text :=Params[0];
  297. jAsyncTask1.Execute;
  298. end;
  299.  
  300.  
  301. procedure TAndroidModule1.AndroidModule1JNIPrompt(Sender: TObject);
  302. var
  303.    manifestPermissions: TDynArrayOfString;
  304.  
  305.  
  306.  begin
  307.  
  308.   if  IsRuntimePermissionNeed() then   // that is, target API >= 23
  309.     begin
  310.  
  311.         SetLength(manifestPermissions, 3);
  312.  
  313.      
  314.         manifestPermissions[1]:= 'android.permission.WRITE_EXTERNAL_STORAGE'; //from AndroodManifest.xml
  315.        
  316.         Self.RequestRuntimePermission(manifestPermissions, 1001);
  317.  
  318.         SetLength(manifestPermissions, 0);
  319.  
  320.     end;
  321.        
  322.  
  323. end;
  324.  
  325. procedure TAndroidModule1.AndroidModule1RequestPermissionResult(
  326.   Sender: TObject; requestCode: integer; manifestPermission: string;
  327.   grantResult: TManifestPermissionResult);
  328. begin
  329.          case requestCode of
  330.                         1001:begin
  331.               if grantResult = PERMISSION_GRANTED  then
  332.  
  333.              exit;
  334.           end;
  335.   end;
  336.  
  337.  
  338.  
  339. end;                                  
  340.  
  341.  
  342.  
  343.  
  344.  
  345.                                                          
  346.  
  347.  //  insert as the code asks for jButton  jEditText  jAsyncTask
  348.  // And of course, declare variables etc as the code asks for  
  349.  
  350.  // don't forget the necessary permissions.
  351.  
  352.  
  353.  



Note: This was my first test and I used APPTCPClientDemo from LAMW. I suggest using the code above in the demo cited to test. Add necessary permission to read files.

Edit : I just tested this by starting a new project and noticed there were some bugs. Have been fixed.


Edit2 : This code is old. I don't recommend using jAsyncTask because its behavior is unstable. When I used this, it worked. But that was a long time ago..


Edit3 :   

Finally some free time.
  removed JaSynctask and use jTCPSocketClient to send and receive strings.

Note: To send files disconnect jTCPSocketClient and connect TTCPBlockSocket. At the end, connect Tcpclient again.

The "template" is APPTCPClientDemo. The "Stop Client" button sends files.

Again : This code is mainly for anyone who wants to use GETMEM Demo with phone. But it serves to make improvements in LAWN TCPIP. Get to work...

The code needs to get cleaned up. There's a lot of unnecessary stuff. They are experiments. But... it works,







Code: Pascal  [Select][+][-]
  1.  
  2. {Hint: save all files to location: C:\adt32\eclipse\workspace\AppTCPClientDemo1\jni }
  3. unit unit1;
  4. {$mode delphi}
  5. // {$mode objfpc}{$H+}
  6. interface
  7. uses
  8.   Classes, SysUtils, And_jni, And_jni_Bridge, Laz_And_Controls,
  9.   Laz_And_Controls_Events, AndroidWidget, tcpsocketclient, textfilemanager,
  10.   opendialog, blcksock, uTCPSockets, uTCPCryptoCompression, strutils, fileutil,
  11.   uSettings;
  12. type
  13.   { TAndroidModule1 }
  14.   TAndroidModule1 = class(jForm)
  15.     jButton1: jButton;
  16.     jButton2: jButton;
  17.     jButton3: jButton;
  18.     jEditText1: jEditText;
  19.     jEditText2: jEditText;
  20.     jOpenDialog1: jOpenDialog;
  21.     jTCPSocketClient1: jTCPSocketClient;
  22.     jTextView1: jTextView;
  23.     jTextView2: jTextView;
  24.     jTextView3: jTextView;
  25.     procedure AndroidModule1JNIPrompt(Sender: TObject);
  26.     procedure AndroidModule1RequestPermissionResult(Sender: TObject;
  27.       requestCode: integer; manifestPermission: string;
  28.       grantResult: TManifestPermissionResult);
  29.     procedure jButton1Click(Sender: TObject);
  30.     procedure jButton2Click(Sender: TObject);
  31.     procedure jButton3Click(Sender: TObject);
  32.  
  33.     procedure jOpenDialog1FileSelected(Sender: TObject; path: string;
  34.       fileName: string);
  35.     procedure jTCPSocketClient1Connected(Sender: TObject);
  36.     procedure jTCPSocketClient1MessagesReceived(Sender: TObject;
  37.       messageReceived: string);
  38.   private
  39.     {private declarations}
  40.     FConnected: boolean;
  41.    sock,FBlockSocket, c,s :TTCPBlockSocket;
  42.    FNeedToClose: Boolean;
  43.    FTCPClient: TTCPClient;
  44.    FSettings: TSettings;
  45.    FConCap: String;
  46.    FDisCap: String;
  47.    FTCPBase: TTCPBase;
  48.    FTCPBaseType: TBaseType;
  49.    FConnection: TConnection;
  50.     var
  51.   Msg: String;
  52.   MsgTyp: Integer;
  53.   Params: TStringArray;
  54.   Size: Int64;
  55.   MS: TMemoryStream;
  56.  // I: Integer;
  57.   FileName,tim,str,f: String;
  58.   Path: String;
  59.   TmpTaskList: TList;
  60.   FCurPing: QWord;
  61.   res,can:boolean;
  62.   public
  63.     {public declarations}
  64.      property Connection: TConnection read FConnection;
  65.     function SendFile(AFileName: String): Boolean;
  66.   end;
  67.  
  68. var
  69.   AndroidModule1: TAndroidModule1;
  70.  
  71. implementation
  72. {$R *.lfm}
  73.  
  74. { TAndroidModule1 }
  75.  
  76.  function TAndroidModule1.SendFile(AFileName: String): Boolean;
  77. var
  78.   ReadCnt: LongInt;
  79.   Cnt: Int64;
  80.   Buffer: Pointer;
  81. //  MS: TMemoryStream;
  82.   MSComp: TMemoryStream;
  83.   FS: TFileStream; fi:boolean;
  84.   Mi,si: String;
  85.   CompRate: Single;
  86. begin
  87.   Result := False;
  88.   si:= inttostr(fileutil.FileSize(afilename));
  89.   mi:= '2FILE:FILE,'+si+','+extractfilename(afilename)+',';
  90.  mi:= Encrypt('53771', mi);FBlockSocket.SendString( mi );
  91.   try
  92.     FS := TFileStream.Create(AFileName, fmOpenRead or fmShareDenyWrite);
  93.     try
  94.       FS.Position := 0;
  95.       GetMem(Buffer, FBlockSocket.SendMaxChunk);
  96.       try
  97.         Cnt := 0;
  98.         MS := TMemoryStream.Create;
  99.         try
  100.           MSComp := TMemoryStream.Create;
  101.           try
  102.             repeat
  103.               repeat
  104.               Msg := FBlockSocket.RecvPacket(50000);
  105.               until (Msg = 'DONE') or (Msg = 'NEXT');
  106.               if (FBlockSocket.LastError > 0) or (Msg = 'DONE') or (Msg <> 'NEXT') then
  107.                begin   showmessage(Msg); Break;  end;
  108.               ReadCnt := FS.Read(Buffer^, FBlockSocket.SendMaxChunk);
  109.               if (ReadCnt > 0) then
  110.               begin
  111.                 MS.Clear;
  112.                 MS.Write(Buffer^, ReadCnt);
  113.                 MS.Position := 0;
  114.                   FBlockSocket.SendStream(MS);
  115.           if (FBlockSocket.LastError = 0)  then
  116.                begin
  117.                   Cnt := Cnt + MS.Size;
  118.                 end;
  119.               end;
  120.             until (ReadCnt = 0) or (Cnt > FS.Size);
  121.             Result := FBlockSocket.LastError = 0;
  122.            if Result then begin    FBlockSocket.CloseSocket; jButton1Click(self)     end
  123.             else  showmessage('erro');
  124.           finally
  125.             MSComp.Free;
  126.           end;
  127.         finally
  128.           MS.Free;
  129.         end;
  130.       finally
  131.         FreeMem(Buffer);
  132.       end;
  133.     finally
  134.       FS.Free;
  135.     end;
  136.   except
  137.     on E: Exception do
  138.     showmessage('erro');
  139.   end
  140. end;
  141.  
  142. procedure TAndroidModule1.jButton1Click(Sender: TObject);
  143. begin
  144.   jEditText2.Clear;
  145.   jTCPSocketClient1.ConnectAsync('172.26.61.98',53771);
  146. end;
  147.  
  148. procedure TAndroidModule1.jButton2Click(Sender: TObject);
  149. begin
  150.  jTCPSocketClient1.SendMessage(Encrypt('53771','0TEXT:'+jEditText1.Text+',') );
  151. end;
  152.  
  153. procedure TAndroidModule1.jButton3Click(Sender: TObject);
  154. begin
  155.      jTCPSocketClient1.CloseConnection();
  156.   FBlockSocket.Connect ('172.26.61.98', '53771');
  157. // eua32  = YOUR USERNAME
  158.    FBlockSocket.SendString (encrypt('53771','0LOGIN:,eua32,0,') );// 'hello'#13#10);
  159. repeat
  160.     until FBlockSocket.RecvPacket(5000) <> '';    
  161.     jOpenDialog1.Show();
  162. end;
  163.  
  164.  
  165.  
  166. procedure TAndroidModule1.jOpenDialog1FileSelected(Sender: TObject;
  167.   path: string; fileName: string);
  168. begin
  169.   f:= getEnvironmentDirectoryPath(TEnvDirectory.dirDownloads)+'/'+fileName;
  170.    SendFile(f);
  171. end;
  172.  
  173. procedure TAndroidModule1.jTCPSocketClient1Connected(Sender: TObject);
  174. begin
  175. // eua32  = YOUR USERNAME
  176.    jTCPSocketClient1.SendMessage(encrypt('53771','0LOGIN:,eua32,0,'));
  177. end;
  178.  
  179. procedure TAndroidModule1.jTCPSocketClient1MessagesReceived(Sender: TObject;
  180.   messageReceived: string);
  181. begin
  182.  messageReceived:=decrypt('53771', messageReceived);  jEditText2.Text :=timetostr(now)+':'+replacestr(extractword(2,messageReceived,[':']),',','');str:='';
  183. end;
  184.  
  185. procedure TAndroidModule1.AndroidModule1JNIPrompt(Sender: TObject);
  186.  var
  187.    manifestPermissions: TDynArrayOfString;
  188.      var myMenu: jObjectRef;
  189.  begin
  190.        self.SetStatusColor(androidmodule1.BackgroundColor );
  191.    if  IsRuntimePermissionNeed() then   // that is, target API >= 23
  192.     begin
  193.         SetLength(manifestPermissions, 3);
  194.         manifestPermissions[0]:= 'android.permission.CAMERA';  //from AndroodManifest.´
  195.         manifestPermissions[1]:= 'android.permission.WRITE_EXTERNAL_STORAGE'; //from AndroodManifest.xml
  196.         manifestPermissions[2]:='android.permission.RECORD_AUDIO';
  197.         Self.RequestRuntimePermission(manifestPermissions, 1001);
  198.         SetLength(manifestPermissions, 0);
  199.     end;
  200.   if IsRuntimePermissionNeed() then   //support for "target API >= 23"
  201.    begin
  202.       Self.RequestRuntimePermission('android.permission.RECORD_AUDIO', 3111);
  203.    end ;
  204. can:=false; res:=false;  FBlockSocket := TTCPBlockSocket.Create;
  205. end;
  206.  
  207. procedure TAndroidModule1.AndroidModule1RequestPermissionResult(
  208.   Sender: TObject; requestCode: integer; manifestPermission: string;
  209.   grantResult: TManifestPermissionResult);
  210. begin
  211.        case requestCode of
  212.      1001:begin
  213.               if grantResult = PERMISSION_GRANTED  then
  214.                exit// ShowMessage('Success! ['+manifestPermission+'] Permission grant!!! ' )
  215.               else  //PERMISSION_DENIED
  216.                 ShowMessage('Sorry... ['+manifestPermission+'] Permission not grant... ' );
  217.           end;
  218.   end;
  219. end;
  220. end.
  221.  
  222.  
  223.  
« Last Edit: August 15, 2021, 08:00:48 pm by Guser979 »

benohb

  • Full Member
  • ***
  • Posts: 208
Re: LAMW Http-TCP server with Lnet
« Reply #8 on: September 17, 2021, 07:11:05 pm »
Guser979
Sorry for the delay. Thank you. Helpful examples. Thank you

jmpessoa

  • Hero Member
  • *****
  • Posts: 1946
Re: LAMW Http-TCP server with Lnet
« Reply #9 on: September 17, 2021, 07:19:42 pm »

Hi, Guser979!

Can you put your TCP demo in some "open drive"?

So,  I will commit it to LAMW github....


Thank you!
Lamw: Lazarus Android Module Wizard
https://github.com/jmpessoa/lazandroidmodulewizard

Guser979

  • New Member
  • *
  • Posts: 40
Re: LAMW Http-TCP server with Lnet
« Reply #10 on: September 18, 2021, 04:32:21 pm »
Ok, done. AppTCPClientDemo2 .

Tested and working. Synapse dependencies are in the JNI folder

The initial IP is my WIFI.

https://drive.google.com/file/d/190xtdTJUQqqz2Quq_JA8yLwa9G1OJuYV/view?usp=sharing

Edit : I forgot to clean up the Build/ Intermediates folder. Sorry about that.


Update :  Added entry for port. Sorry, but as I'm not a programmer, I'm not used to distributing my tools or apps. Only I use them. JMpessoa, please do what I don't know.   :D
« Last Edit: September 19, 2021, 02:03:48 am by Guser979 »

jmpessoa

  • Hero Member
  • *****
  • Posts: 1946
Re: LAMW Http-TCP server with Lnet
« Reply #11 on: September 19, 2021, 08:51:12 pm »

Quote
So,  I will commit it to LAMW github....

Done!

New demo "AppTCPClientDemo2"!

Thank you!
Lamw: Lazarus Android Module Wizard
https://github.com/jmpessoa/lazandroidmodulewizard

 

TinyPortal © 2005-2018