TCP Server in Android lazarus / 43-Android ---------------------------------------------------------------------- zakidrus · 2019-11-18 23:11 Hi Everyone I'am newbie in Laz4android. What I have done so far are installing the Laz4android, LAMW, jdk, sdk, ndk, compilling my first "hello world" apk, and run it in android phone. Those all . But further, I need make an apk that will work as TCP Server (listening tcp). I have done it in PC (windows) before and I believe it's possible in android as well. What is the easiest way to do that? Any component or code that work in the purpose? Many thanks. ---------------------------------------------------------------------- kordal · 2019-11-21 00:29 Hi. LAMW currently only has JUDPSocket and JTCPSocketClient components. There is no TCP server in this implementation. Decision: 1. Use JAVA and class ServerSocket. 2. Use RAD Studio 10.x and Indy or other components. ---------------------------------------------------------------------- Remy Lebeau · 2019-11-22 02:55 Indy works in FreePascal and has TCP client/server components that work on Android. ---------------------------------------------------------------------- zakidrus · 2019-11-23 13:57 Thank You Remy Lebeau for The Indy Component. Finally, I am able to put Indy on Laz4Android. But I found problem when running the app. It will close directly whenever I open the TCP port. This is my code for creating and open the port. Button 1 is triggering the app to close. 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 TAndroidModule1.AndroidModule1Create(Sender: TObject);begin  IdTCPServer1                   := TIdTCPServer.create;  IdTCPServer1.OnConnect         := IdTCPServer1Connect;  IdTCPServer1.OnDisconnect      := IdTCPServer1Disconnect;  IdTCPServer1.OnException       := IdTCPServer1Exception;  IdTCPServer1.OnExecute         := IdTCPServer1Execute;end; procedure TAndroidModule1.jButton1Click(Sender: TObject);begin  IdTCPServer1.Bindings.Add.Port := 6000;  IdTCPServer1.Active          := True;end; ---------------------------------------------------------------------- lucamar · 2019-11-23 14:25 If your mode is OBJFPC (i.e. your code has a {$mode objfpc} directive) try 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 TAndroidModule1.AndroidModule1Create(Sender: TObject);begin  IdTCPServer1                   := TIdTCPServer.create;  IdTCPServer1.OnConnect         := @IdTCPServer1Connect;  IdTCPServer1.OnDisconnect      := @IdTCPServer1Disconnect;  IdTCPServer1.OnException       := @IdTCPServer1Exception;  IdTCPServer1.OnExecute         := @IdTCPServer1Execute;end; ---------------------------------------------------------------------- kordal · 2019-11-24 01:55 Working with sockets usually requires a separate thread. As I understand it, this is especially critical in Android OS. The IdTCPServerExecute method should not directly work with the GUI of the main thread and use global variables. To do this, you need a synchronization mechanism, for example, critical sections. How Indy works in Android on FPC never tested. ---------------------------------------------------------------------- zeljkoc · 2019-11-24 07:06 I use indie for android (indy-10.2.0.3.zip) https://forum.lazarus.freepascal.org/index.php/topic,18958.msg107445.h tml#msg107445 with android i transfer data to firebird server, with indy ---------------------------------------------------------------------- zakidrus · 2019-11-24 12:28 Thanks Lucamar for the advice. I tried OBJFPC mode and use the directive @ but the problem still there. App close when I open the port. I am start thinking it's caused by Android security although I have checked all permission needed by the app ---------------------------------------------------------------------- Remy Lebeau · 2019-11-26 02:53 Quote from: zakidrus on November 23, 2019, 02:57:48 pmFinally, I am able to put Indy on Laz4Android. But I found problem when running the app. It will close directly whenever I open the TCP port. Then the TIdTCPServer.Active property setter is likely raising an exception that you are not catching. Quote from: kordal on November 24, 2019, 02:55:56 amWorking with sockets usually requires a separate thread. As I understand it, this is especially critical in Android OS. TIdTCPServer is a multi-threaded component.  It create a new worker thread for each entry in the TIdTCPServer.Bindings property, and then calls the OnConnect, OnExecute, OnDisconnect, and OnException events in the context of those worker threads. Quote from: zeljkoc on November 24, 2019, 08:06:50 amI use indie for android (indy-10.2.0.3.zip) That is an extremely old version of Indy.  The current version is 10.6.2.  Indy is available in Lazarus' Online Package Manager. ---------------------------------------------------------------------- zeljkoc · 2020-03-07 18:31 Please send link indy 10.6.2 for lazarus ---------------------------------------------------------------------- Remy Lebeau · 2020-03-10 00:24 Quote from: zeljkoc on March 07, 2020, 07:31:37 pmPlease send link indy 10.6.2 for lazarus As I already stated, Indy is available in Lazarus' Online Package Manager.  You can install Indy directly from the OPM. Although, I don't know if the version of Indy in OPM is the absolute latest version available, since Indy migrated from SVN to GitHub a few month ago, but it should be fairly recent. ---------------------------------------------------------------------- balazsszekely · 2020-03-10 05:49 @Remy Lebeau QuoteAlthough, I don't know if the version of Indy in OPM is the absolute latest version available, since Indy migrated from SVN to GitHub a few month ago, but it should be fairly recent.I regularly update Indy, so usually is fairly recent. I have a question though. Before switching to git, the version number inside the lpk file looked like this: 10.6.2.XXXX where XXXX was the last SVN revision number. Since git doesn't have a revision number equivalent, what should I put instead of XXXX? For now I went with 10.6.2.0, but perhaps there is a better way to describe the current version in OPM. ---------------------------------------------------------------------- zeljkoc · 2020-03-10 10:48 i tried to install and use for android Error: IdGlobal.pas(553,29) Fatal: Cannot find iconvenc used by IdGlobal ---------------------------------------------------------------------- Remy Lebeau · 2020-03-11 18:03 Quote from: GetMem on March 10, 2020, 06:49:00 amBefore switching to git, the version number inside the lpk file looked like this: 10.6.2.XXXX where XXXX was the last SVN revision number. Since git doesn't have a revision number equivalent, what should I put instead of XXXX? Yes, versioning is kind of a mess now that Indy is on GitHub.  The 10.6.2 portion is static (IdVers.inc), but the XXXX was dynamic, based on SVN revision number, as you said.  Actually, GitHub does have an SVN interface, which does have a revision number.  I don't use SVN when checking in Indy's code to GitHub, so I don't really know where that revision number stands, but I do know it is functional. In Indy 11, we might switch over to semantic versioning instead.  Not sure yet. ---------------------------------------------------------------------- Remy Lebeau · 2020-03-11 18:08 Quote from: zeljkoc on March 10, 2020, 11:48:40 am i tried to install and use for android Error: IdGlobal.pas(553,29) Fatal: Cannot find iconvenc used by IdGlobal iconvenc is a FreePascal library/unit, not an Indy unit.  It should be available if your search paths are setup correctly.  Unless iconvenc is not available for Android, I don't know. ---------------------------------------------------------------------- zeljkoc · 2020-03-12 11:48 Yes, the problem is with android. Others have the same problem https://www.pilotlogic.com/sitejoom/index.php/forum/cross-build- development/3820-compile-android-library-include-indy-components- failed-cannot-find-liconv ---------------------------------------------------------------------- trev · 2020-03-12 12:51 See https://stackoverflow.com/questions/10004077/how-to-install- libiconv-for-android-ndk Does Indy depend on iconv? ---------------------------------------------------------------------- DonAlfredo · 2020-03-12 14:11 I have build libiconv for Android. You might try if this works for you. https://github.com/LongDirtyAnimAlf/fpcupdeluxe/releases/download/1.6. 8c/libiconv_android.zip ---------------------------------------------------------------------- Remy Lebeau · 2020-03-12 19:05 Quote from: zeljkoc on March 12, 2020, 12:48:36 pmYes, the problem is with android. Others have the same problem https://www.pilotlogic.com/sitejoom/index.php/forum/cross-build- development/3820-compile-android-library-include-indy-components- failed-cannot-find-liconv That is not the same error that zeljkoc is experiencing.  That error shows the iconvenc unit being compiled fine (not including some hints), the project is just not linking to the actual iconv library. In zeljkoc's case, the compiler is not even finding the iconvenc unit at all. Quote from: trev on March 12, 2020, 01:51:42 pmDoes Indy depend on iconv? On 'Nix platforms, yes.  Android uses Java running on top of Linux, and being that Indy is a native code library, it runs at the Linux layer, not at the Java layer. Indy does a lot of operations that have to encode Unicode strings to byte arrays, and decode byte arrays to Unicode strings.  Now, certainly Indy could be updated to handle UTF encodings manually at least, but seeing that there are still many charsets being used in the world other than UTFs, especially for things like email, it would not be feasible for Indy to handle all of those charsets manually, so it uses platform APIs to handle them, such as iconv on 'Nix. There are some higher level hooks available in Indy if you want to handle charsets manually in your own code (like the GIdEncodingNeeded event in the IdGlobalProtocols unit, and various methods that take a user-supplied IIdTextEncoding as a parameter), but I don't think there are enough hooks available at the lower level to completely break the iconv dependency at this time.  For instance, you could disable the USE_ICONV define in IdCompilerDefines.inc, but then you will have to manually provide an implementation for the TIdMBCSEncoding class in the IdGlobal unit, or else Indy's handling of UTF-7, UTF-8, and other non-UTF charsets will break at runtime. ---------------------------------------------------------------------- eljo · 2020-03-12 19:29 Quote from: Remy Lebeau on March 12, 2020, 08:05:35 pmQuote from: zeljkoc on March 12, 2020, 12:48:36 pmYes, the problem is with android. Others have the same problem https://www.pilotlogic.com/sitejoom/index.php/forum/cross-build- development/3820-compile-android-library-include-indy-components- failed-cannot-find-liconv That is not the same error that zeljkoc is experiencing.  That error shows the iconvenc unit being compiled fine (not including some hints), the project is just not linking to the actual iconv library. In zeljkoc's case, the compiler is not even finding the iconvenc unit at all. Quote from: trev on March 12, 2020, 01:51:42 pmDoes Indy depend on iconv? On 'Nix platforms, yes.  Android uses Java running on top of Linux, and being that Indy is a native code library, it runs at the Linux layer, not at the Java layer. have you seen this https://github.com/d-mozulyov/UniConv ? it might be a better option don't know. since you are here I'd like to ask if there is any documentation on what is required to port indy to a new OS I'm contemplating of porting to ultibo and wandering how complicated that might be. ---------------------------------------------------------------------- balazsszekely · 2020-03-14 09:21 @Remy Lebeau QuoteActually, GitHub does have an SVN interface, which does have a revision number.Thanks. I will use the revision number from the SVN interface. ---------------------------------------------------------------------- Remy Lebeau · 2020-03-14 22:27 Quote from: eljo on March 12, 2020, 08:29:49 pmhave you seen this https://github.com/d-mozulyov/UniConv ? it might be a better option don't know. Interesting, but the goal is to not have to embed huge conversion tables into every app that wants to use Indy.  That is why Indy delegates to platform APIs for text conversions, let the OS handle them. Quote from: eljo on March 12, 2020, 08:29:49 pmsince you are here I'd like to ask if there is any documentation on what is required to port indy to a new OS No, there is not. Quote from: eljo on March 12, 2020, 08:29:49 pmI'm contemplating of porting to ultibo and wandering how complicated that might be. Never heard of ultibo.  I don't have a clue what the requirements are for writing code for it. ---------------------------------------------------------------------- asdf1337 · 2020-03-14 22:50 Quote from: Remy Lebeau on March 14, 2020, 11:27:23 pmQuote from: eljo on March 12, 2020, 08:29:49 pmI'm contemplating of porting to ultibo and wandering how complicated that might be. Never heard of ultibo.  I don't have a clue what the requirements are for writing code for it. Would start with seeing what happens if you just compile it - maybe it works without that many changes if used the right defines (e.g. ultibo has Winsock 2.0 interface). Otherwise you need to do all the low-level calls they use for creating/sending/receiving data through the socket (see https://ultibo.org/wiki/Unit_Reference#Network_units). ---------------------------------------------------------------------- eljo · 2020-03-14 23:37 Quote from: Remy Lebeau on March 14, 2020, 11:27:23 pmQuote from: eljo on March 12, 2020, 08:29:49 pmhave you seen this https://github.com/d-mozulyov/UniConv ? it might be a better option don't know. Interesting, but the goal is to not have to embed huge conversion tables into every app that wants to use Indy.  That is why Indy delegates to platform APIs for text conversions, let the OS handle them. Quote from: eljo on March 12, 2020, 08:29:49 pmsince you are here I'd like to ask if there is any documentation on what is required to port indy to a new OS No, there is not. A generic group list should get me started I guess. so 1) sockets api (already provided) 2) uniconv  (as showed in this thread). threading? something else that I might hit my head on? Any way thank you for the info. Quote from: Remy Lebeau on March 14, 2020, 11:27:23 pm Quote from: eljo on March 12, 2020, 08:29:49 pmI'm contemplating of porting to ultibo and wandering how complicated that might be. Quote from: Remy Lebeau on March 14, 2020, 11:27:23 pmNever heard of ultibo.  I don't have a clue what the requirements are for writing code for it. https://ultibo.org a baremetal framework to write your own firmware for raspberry PI. It has  units for a lot of things. ---------------------------------------------------------------------- Remy Lebeau · 2020-03-15 20:58 Quote from: eljo on March 15, 2020, 12:37:35 amA generic group list should get me started I guess. It hass been a long time since Indy added a new OS.  All I can really offer you is this for now: - go through IdCompilerDefines.inc and see if anything needs to be added/adjusted for ultibo. - go through IdGlobal.pas and see if anything needs to be added/adjusted for ultibo.  Type declarations, handling of text encodings, etc. - check if the existing TIdStack-derived classes are suitable for ultibo's socket API.  If not, you will have to write a new class, and then update IdStack.pas to assign that class type to the global GStackClass variable in the unit's initialization section. Quote from: eljo on March 15, 2020, 12:37:35 amthreading? Indy uses the RTL's TThread class, so threading support is the responsibility of FreePascal to handle.