nettobac  0.0.0
Network features for FreeBASIC code

Table of Contents

Each nettobac session performs the following events

  1. create an instance (nettobacClient or nettobacServer)
  2. nOpen() a connection
  3. exchange data over the connection (by nPut or nGet)
  4. nClose() the connection (optional)
  5. DELETE the instance

When using the http utilities in file nettobac_http.bas, the minimum source code is

#INCLUDE ONCE "nettobac.bas"
#INCLUDE ONCE "nettobac_http.bas"
VAR res = "" _
, msg = httpLoad(res, "")
IF msg THEN ?"error: " & *msg & " failed" : END 1
' here variable res contains the web page context

The above five steps are inside the function httpLoad(). After each step an error check gets done, and in case of an error the function breaks and returns the error code. See page Error Messages for details on possible error messages.

In case of success the function returns 0 (zero) and the downloaded context is in variable res.

A little more complex code is used in the package examples, described in the following sections.


This example perfomrs a

  • client scenario by
  • downloading files from different servers and
  • store them in the local folder data
    • "index.html": a text file (via http protocol)
    • "fb_logo.gif": an image file (via http protocol)
    • "osm.png": an image file created by php script (custom protocol)

The downloads get done in function doClientActions(). Each one is enclosed by a SCOPE block, in order to make it easy to add or remove further blocks.

When you execute this example (on the command line), it

  • checks for the folder data in the executable path
  • creates that folder if not present
  • changes to that folder
  • downloads the above mentioned files
  • and stores them in folder data
  • thereby generating the following messages
1  nettobac-0.0.0, License LGPLv2.1
2  Copyright (C) 2015-2015 by Thomas{ doT ]Freiherr[ At ]gmx[ DoT }net
3  Compiled: 11-11-2015, 10:29:16 with FreeBASIC 1.01.0 for UNIX
4 saved: index.htm
5 saved: fb_logo.gif
6 saved: osm.png

Check the files in folder src/bas/data.


This example performs a

  • http server scenario
  • by opening a port on localhost and
  • listening at this port for client connections.
  • It opens connections to client peers,
  • receives http requests from clients and
  • responds to requests by sending http data.

When you execute this example (on the command line), it outputs the following message

1  nettobac-0.0.0, License LGPLv2.1
2  Copyright (C) 2015-2015 by Thomas{ doT ]Freiherr[ At ]gmx[ DoT }net
3  Compiled: 11-11-2015, 10:29:17 with FreeBASIC 1.01.0 for UNIX
4 server started (port = 3490)

and waits for a client to connect. You can test this by starting your web browser and request the adress

1 localhost:3490

That will open the "NetToBac HTML Demo #1" page, containing two links and a button.

  • The first link jumps to a second (internal) page.
  • The second link jumps to an external page loaded from the web.
  • The button exits the demo and shuts down the server.

Before you test the links, check the output on the command line. It shows further information on the actions performed by the example_server.bas code, ie. like

1 ...
2 Client connected!
4 Client message:
5 GET / HTTP/1.1
6 Host: localhost:3490
7 User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:42.0) Gecko/20100101 Firefox/42.0
8 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
9 Accept-Language: de,en-US;q=0.7,en;q=0.3
10 Accept-Encoding: gzip, deflate
11 DNT: 1
12 Connection: keep-alive
15 sending HTML1 ... done
17 Client disconnected!
19 Client connected!
21 Client message:
22 GET /favicon.ico HTTP/1.1
23 Host: localhost:3490
24 User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:42.0) Gecko/20100101 Firefox/42.0
25 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
26 Accept-Language: de,en-US;q=0.7,en;q=0.3
27 Accept-Encoding: gzip, deflate
28 DNT: 1
29 Connection: keep-alive
32 sending ICON ... done
34 Client disconnected!

When the second link gets clicked, no further message gets shown in the command line window, since the browser connects to an external server to get the context.

When you click on the first (internal) link, further messages occur in the command line window (as shown above). The second page opens. It contains a form with two entries for a pseudo login and a button. When clicking the button, the server gets the form context and extracts the user input from the entries, to create a new page showing the results.

When the button Exit demo -> shutdown server gets clicked, the server closes all connections and shuts down. This can also get achieved by pressing any key in the command line window.