Recent

Author Topic: idHTTPServer problems  (Read 8264 times)

stargate

  • New Member
  • *
  • Posts: 12
idHTTPServer problems
« on: August 25, 2016, 03:19:52 pm »
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][+][-]
  1. procedure TForm1.ServerCommandGet(AContext: TIdContext;
  2.   ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
  3. begin
  4.   AResponseInfo.ContentText := 'it works!';
  5.   AResponseInfo.WriteContent;
  6. end;
  7.  
  8. procedure TForm1.FormCreate(Sender: TObject);
  9. var Binding: TIdSocketHandle;
  10. begin
  11.   label1.caption := released;
  12.   idhttpserver1 := tidhttpserver.create;
  13.   idhttpserver1.OnCommandGet:= @servercommandget;
  14.   Binding := idhttpserver1.Bindings.Add;
  15.   Binding.IPVersion := Id_IPv4;
  16.   Binding.IP := '127.0.0.1';
  17.   Binding.Port := 80;
  18.   idhttpserver1.Active := True;
  19. 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

  • Guest
Re: idHTTPServer problems
« Reply #1 on: August 25, 2016, 03:57:55 pm »
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

  • Administrator
  • Hero Member
  • *
  • Posts: 11383
  • FPC developer.
Re: idHTTPServer problems
« Reply #2 on: August 25, 2016, 09:05:58 pm »
(afaik on *nix you must be root to bind ports under 1024)

stargate

  • New Member
  • *
  • Posts: 12
Re: idHTTPServer problems
« Reply #3 on: August 26, 2016, 10:05:14 am »
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

  • Hero Member
  • *****
  • Posts: 1848
Re: idHTTPServer problems
« Reply #4 on: August 26, 2016, 10:47:53 am »
@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
« Last Edit: August 26, 2016, 10:57:22 am by JD »
Windows - Lazarus 2.1/FPC 3.2 (built using fpcupdeluxe),
Linux Mint - Lazarus 2.1/FPC 3.2 (built using fpcupdeluxe)

mORMot; Zeos 8; SQLite, PostgreSQL & MariaDB; VirtualTreeView

stargate

  • New Member
  • *
  • Posts: 12
Re: idHTTPServer problems
« Reply #5 on: August 26, 2016, 11:02:06 am »
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

  • Hero Member
  • *****
  • Posts: 1312
    • Lebeau Software
Re: idHTTPServer problems
« Reply #6 on: August 30, 2016, 03:10:58 am »
FYI, Id_IPv4 is defined in the IdGlobal unit:

Code: Pascal  [Select][+][-]
  1.   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][+][-]
  1. procedure TForm1.ServerCommandGet(AContext: TIdContext; ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
  2. begin
  3.   AResponseInfo.ResponseNo := 200;
  4.   AResponseInfo.ContentType := 'text/plain';
  5.   AResponseInfo.ContentText := 'it works!';
  6. 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][+][-]
  1.   idhttpserver1 := TIdHTTPServer.Create(Self);

Or:

Code: Pascal  [Select][+][-]
  1.   idhttpserver1 := TIdHTTPServer.Create;
  2.   ...
  3.   idhttpserver1.Free;
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

 

TinyPortal © 2005-2018