How to serve data
lazarus / 38-Networking and Web Programming
----------------------------------------------------------------------
fpcoder · 2014-07-06 03:39
How do I provide simple statistical data over internet to an app on a
remote computer:
Temperature, Location
(double) (string)
This is for Linux and many users may be connected at the same time.
Have looked at Synapse, LNET, Indy and Brook but dont know what is
most suitable. Its should be as simple as possible.
----------------------------------------------------------------------
Leledumbo · 2014-07-06 08:43
All is possible, now it's up to you want to learn which one.
Simplicity is relative, when you use those libraries.
----------------------------------------------------------------------
BeniBela · 2014-07-06 18:50
Serving data can be done really simple
No need to over complicate it with libraries, unless you expect
10000nd of visitors/second
Code: [Select]program serving;
begin
writeln('Content-Type: text/html');
writeln('');
writeln('
The simplest http
server');
end.
----------------------------------------------------------------------
hinst · 2014-07-06 18:53
find fcl-web directory in your FPC installation and take a look at
units & examples there
Code: [Select] THTTPServer = class(TFPHttpServer)
public procedure HandleRequest(
var aRequest: TFPHTTPConnectionRequest;
var aResponse: TFPHTTPConnectionResponse
); override;
end;
you can use fphttpserver unit, then override HandleRequest method of
TFPHttpServer class, then handle the request well I could go in more
detail but I hope you will figure this out
----------------------------------------------------------------------
fpcoder · 2014-07-07 17:55
A RaspBerry Pi with 512mb ram will be server. Thats why resource
consumption should be as low as possible and hoping that LNet is the
ligthest solution I tried its LTCPComponent. It works fine.
Some solutions (Indy?) might use more cpu / memory? In the process of
trying Indy I converted the Indy 9 Delphi demo (Basic ClientServer) to
Lazarus Indy 10. (Attached in case someone needs it) But I know LNet
works with RaspBerry/ARM and is known to be solid so LNet is the
choice if nothing unexpected happens.
Hinst tip made me look at \fcl-web for the first time. Will have a
better look at that, maybe for future use.
@BeniBela: its a usual program, not a web browser, that will receive
the server data so I dont think I can use your method? Not sure
exactly how you mean. I'm a bit new to internet programming.
----------------------------------------------------------------------
hinst · 2014-07-07 19:45
You can install Apache web server
Code: [Select]sudo apt-get install apache2
(apache2 or just apache, I am not sure)
Then set it up (which is not so easy, you should read some article
explaining how to do this)
Then put your program to cgi-bin directory of apache web server, then
enable cgi-bin. Just Google it, read articles explaining how to do it
and where the directory is located. Then you won't need to implement
web server in your application, Apache will then act as a web server
and execute your program each time it receives a request for it
(http://yoursite/cgi-bin/yourapp or something)
Just a tip: to minimize resource consumption make sure you limit
number of simultaneous connections; you can set it in Apache config
file, like to 30 simultaneous connections maximum; then tune some more
settings which control how many instances of Apache process you want
to have. On my system it had like 5..9 processes by default, you may
want to limit it to 2. I think it is impossible to reduce it to 1
though, it always will have at least 2 processes running. The general
idea is that it should use multiple threads inside one process, not
multiple processes... you can read about it too
Like I said, if you use Apache as a web server, you won't have to
implement web server or any networking in your application at all
---
you either use sockets or http. "serve" data usually means http.
In http (or call it web) you send http request, then receive response
When using socket you can keep connection open for hours and send,
say, 1 byte per minute or anything else, I don't know
when using cgi-bin, Apache writes request to standard input of the
program (you can read it like Read(s)) and the program may write
response to standard output (like Write('hello')), and it does not
need to take care of networking because Apache takes care of it. It
then sends your response wherever the request came from. the client
program has to send http requests though