How to implement a HTTP server?

For issues with communication ports, protocols, etc.
Post Reply
xlucas
Posts: 334
Joined: May 09, 2014 21:19
Location: Argentina

How to implement a HTTP server?

Post by xlucas »

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.
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Re: How to implement a HTTP server?

Post by D.J.Peters »

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

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
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Re: How to implement a HTTP server?

Post by D.J.Peters »

Last edited by D.J.Peters on Oct 13, 2016 17:49, edited 1 time in total.
xlucas
Posts: 334
Joined: May 09, 2014 21:19
Location: Argentina

Re: How to implement a HTTP server?

Post by xlucas »

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:

Code: Select all

(15) error 18: Element not defined, ClientIP in 'if connection then print "client: " & server.ClientIP & " connected"'
Might I be using an old version of SNC?
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Re: How to implement a HTTP server?

Post by D.J.Peters »

xlucas wrote:Might I be using an old version of SNC ?
I don't know but it's in the zip file from Sun Mar 20, 2016.

Joshy
xlucas
Posts: 334
Joined: May 09, 2014 21:19
Location: Argentina

Re: How to implement a HTTP server?

Post by xlucas »

Ah! Yep. Just updated it and now it compiles :) The version I had was from last year!
TJF
Posts: 3809
Joined: Dec 06, 2009 22:27
Location: N47°, E15°
Contact:

Re: How to implement a HTTP server?

Post by TJF »

nettobac contains a server example:
Regards
Post Reply