idHTTPServer problems lazarus / 38-Networking and Web Programming ---------------------------------------------------------------------- stargate · 2016-08-25 13:19 Hello, I am porting an app from Delphi/Windows to Lazarus/Ubuntu. I am having difficulty with idhttpserver. Initially I couldn't get it to start on any port (always threw address in use error). Then I added -dUseCThreads flag to the compiler. Now there is no error but it also doesn't listen on any set port. This is the code: 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 TForm1.ServerCommandGet(AContext: TIdContext;  ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);begin  AResponseInfo.ContentText := 'it works!';  AResponseInfo.WriteContent;end; procedure TForm1.FormCreate(Sender: TObject);var Binding: TIdSocketHandle;begin  label1.caption := released;  idhttpserver1 := tidhttpserver.create;  idhttpserver1.OnCommandGet:= @servercommandget;  Binding := idhttpserver1.Bindings.Add;  Binding.IPVersion := Id_IPv4;  Binding.IP := '127.0.0.1';  Binding.Port := 80;  idhttpserver1.Active := True;end; Id_IPv4 comes as unknown identifier and had to comment that line out to make it compile, but still it doesn't listen to any port. In this scenario I already have apache running on port 80 - I was checking to see that at least it should throw an error about port in use - but the app runs just fine with no errors. Any clues? Thank you. ---------------------------------------------------------------------- balazsszekely · 2016-08-25 13:57 Hi stargate, Please check out the following thread: http://forum.lazarus.freepascal.org/index.php/topic,32771.0.html You will find a nice example implemented by @JD. regards, GetMem ---------------------------------------------------------------------- marcov · 2016-08-25 19:05 (afaik on *nix you must be root to bind ports under 1024) ---------------------------------------------------------------------- stargate · 2016-08-26 08:05 Thank you guys for replies, GetMem, I have already seen that thread and my code is similar. I am not even concerned at this point by getting an answer on the request - I just want to see the port opened. marcov, I am running lazarus-ide as root and I also tried opening ports higher than 1024. ---------------------------------------------------------------------- JD · 2016-08-26 08:47 @stargate Please see this post on Stackoverflow. It was my initial try with Indy on Linux Mint (using TCPServer and Telnet but it should still be OK since you are try to bind a port to your application). The problem was with the text encoding. http://stackoverflow.com/questions/7767616/indy-10-tcpserver-does-not- communicate-with-connected-clients-on-linux JD ---------------------------------------------------------------------- stargate · 2016-08-26 09:02 Ok... problem solved but I don't understand how. I have started a new project with the exact same code. Now it works. Maybe I tinkered too much with the compiler options to add -dUseCThreads option. I have added to Project Options - Compiler Options - Custom Options. Now it seems to work. ---------------------------------------------------------------------- Remy Lebeau · 2016-08-30 01:10 FYI, Id_IPv4 is defined in the IdGlobal unit: 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   TIdIPVersion = (Id_IPv4, Id_IPv6); Indy defaults to IPv4 unless you edit IdCompilerDefines.inc to define IdIPv6 instead of IdIPv4. As for the code you showed, there are some other problems: Your OnCommandGet handler is not assigning the AResponseInfo.ResponseNo or AResponseInfo.ContentType properties.  And it does not need to call AResponseInfo.WriteContent manually, the content is written when the handler exits. 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 TForm1.ServerCommandGet(AContext: TIdContext; ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);begin  AResponseInfo.ResponseNo := 200;  AResponseInfo.ContentType := 'text/plain';  AResponseInfo.ContentText := 'it works!';end; And you are leaking the TIdHTTPServer object, as you do not assign an Owner to it, or Free it in the form's OnDestroy event. 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   idhttpserver1 := TIdHTTPServer.Create(Self); Or: 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   idhttpserver1 := TIdHTTPServer.Create;  ...  idhttpserver1.Free;