Is there a simple way I can implement a basic HTTP server with FB? Like something that just waits for a GET request and sends the HTML. I don't want to have to install bloated server software when all I want to do is that and need no script or anything. Also, if I were able to control the content with FB, it'd be a lot more comfortable to me than having to do it through scripting.
I had found SNC before on this forum and had been able to make a simple client out of it, but when I tried to make a server, I failed and had no example to look at.
How to implement a HTTP server?
-
- Posts: 8586
- Joined: May 28, 2005 3:28
- Contact:
Re: How to implement a HTTP server?
For a single client it's simple:
Create a server on port 80
in a loop wait for client connections
if a new client are connected
receive the HTTP: request
and response with the right answer
...
thats all ;-)
If more than one client connected you have to create one thread per client ...
Joshy
Create a server on port 80
in a loop wait for client connections
if a new client are connected
receive the HTTP: request
and response with the right answer
...
thats all ;-)
If more than one client connected you have to create one thread per client ...
Joshy
Code: Select all
' [S]imple [N]etwork [C]onnection
#include once "snc.bi"
' creatre WEB HTNL server on port 80
dim as NetworkServer server = NetworkServer(80)
dim as NetworkConnection ptr connection
print "HTTP server running wait on client or press any key ..."
while inkey()="" andalso connection<>-1
if connection=0 then
connection = server.GetConnection()
if connection then print "client: " & server.ClientIP & " connected"
end if
if connection then
' ready to receive ?
while connection->CanGet()<>1 : sleep 100 : wend
' get data from browser (the connected client)
dim as zstring ptr pData
var nBytes = connection->GetData(pData)
if nBytes>0 andalso pData<>0 then
dim as string request = *pData
deallocate pData ' free the data (allocated by snc.bi)
' here read the client request
' ....
' ready to send ?
while connection->CanPut()<>1 : sleep 100 : wend
' here responce to the request (send HTML code, images ... to the browser)
dim as string answer = "...."
connection->PutData(strptr(answer), len(answer)+1) ' plus the 0 terminator
end if
end if
sleep 500
wend
-
- Posts: 8586
- Joined: May 28, 2005 3:28
- Contact:
Re: How to implement a HTTP server?
Last edited by D.J.Peters on Oct 13, 2016 17:49, edited 1 time in total.
Re: How to implement a HTTP server?
Thank you! That really is extremely helpful. I see by the docs that I can decide to make the server as simple or complex as I wish :)
Uhm... but I can't run the code. It says:
Might I be using an old version of SNC?
Uhm... but I can't run the code. It says:
Code: Select all
(15) error 18: Element not defined, ClientIP in 'if connection then print "client: " & server.ClientIP & " connected"'
-
- Posts: 8586
- Joined: May 28, 2005 3:28
- Contact:
Re: How to implement a HTTP server?
I don't know but it's in the zip file from Sun Mar 20, 2016.xlucas wrote:Might I be using an old version of SNC ?
Joshy
Re: How to implement a HTTP server?
Ah! Yep. Just updated it and now it compiles :) The version I had was from last year!