HTML

Discussions about the BBC BASIC language, with particular reference to BB4W and BBCSDL
Jo.2a
Posts: 4
Joined: Tue 14 Feb 2023, 13:32

HTML

Post by Jo.2a »

Hello
it's possible to generate HTML code and send to a client (using BBC software as server) ?
Thank
Jo
Hated Moron

Re: HTML

Post by Hated Moron »

Jo.2a wrote: Tue 01 Aug 2023, 07:49 it's possible to generate HTML code and send to a client (using BBC software as server) ?
Yes, simple web servers have previously been written in BBC BASIC. Here's one by Jon Ripley from 2007 that I found lying around, it's compatible with both BBC BASIC for Windows and BBC BASIC for SDL 2.0:

Code: Select all

      REM Jon Ripley's Simple Web Server

      INSTALL @lib$+"socklib"

      PROC_initsockets

      ON ERROR ON ERROR OFF:PROC_exitsockets:PRINT REPORT$" at line ";ERL:END
      ON CLOSE PROC_exitsockets:QUIT

      eol$ = CHR$13+CHR$10
      host$ = FN_gethostname
      port$ = "80"

      PRINT "Host name is """host$""""

      REPEAT
        listen_socket% = FN_tcplisten(host$,port$)
        IF listen_socket% <= 0 ERROR 100, "Failed to create socket"
        PRINT '"Listening on port ";port$

        REPEAT
          SYS "Sleep", 1
          accept_socket% = FN_check_connection(listen_socket%)
        UNTIL accept_socket%

        IF FN_readlinesocket(accept_socket%, 6000, line$)
        PRINT "Request:"line$
        REPEAT
          IF FN_readlinesocket(accept_socket%, 6000, head$)
          PRINT"Header:";head$
        UNTIL head$=""

        IF LEFT$(line$,4)="GET " THEN
          reply$="HTTP/1.0 200 OK"+eol$+"Content-type: text/html"+eol$+eol$+\
          \"<html><head><title>Hello World!</title></head><body>"+\
          \"<h1>Hello World!</h1><hr>Powered by <i>BBC BASIC</i>"+\
          \"</body></html>"
        ELSE
          reply$="HTTP/1.0 501 Not Implemented"+eol$+"Content-type: text/plain"+eol$+eol$+\
          \"501 - Method not implemented"
        ENDIF

        IF reply$<>"" IFFN_writelinesocket(accept_socket%,reply$)

        PROC_closesocket(accept_socket%)
      UNTIL FALSE
There are of course many ways in which this could be enhanced.
Jo.2a
Posts: 4
Joined: Tue 14 Feb 2023, 13:32

Re: HTML

Post by Jo.2a »

Hello
thank-you for your help
I try the example but i have a problem :
syntax error with this line : if fn_readlinesocket(accept_socket%, 6000, line$)
I don't understand why
thank for your help
Jo
Hated Moron

Re: HTML

Post by Hated Moron »

Jo.2a wrote: Thu 03 Aug 2023, 16:27 syntax error with this line : if fn_readlinesocket(accept_socket%, 6000, line$)
I don't understand why
My guess: you have the Lowercase keywords option turned on (LINE is a keyword, being displayed in orange, by default, is the giveaway).

Since BBC BASIC has always required keywords to be in CAPITALS, right back to the original version in 1981, you will commonly encounter programs that are incompatible with this option. You are strongly advised not to use it; if you do, be prepared to make the necessary changes when incorporating code written by other people.

The Cross-reference utility (Utilities menu in both BBC BASIC for Windows and BBC BASIC for SDL 2.0) issues a warning if it finds that the program is incompatible with the Lowercase keywords mode.
Jo.2a
Posts: 4
Joined: Tue 14 Feb 2023, 13:32

Re: HTML

Post by Jo.2a »

Hello
I used uppercase keywords but the result is the same : syntax error on this line -> IF FN_readlinesocket(accept_socket%, 500, LINE$)
if you have un idea or an other example ?
thanks
Jo
Hated Moron

Re: HTML

Post by Hated Moron »

Jo.2a wrote: Fri 04 Aug 2023, 11:43 I used uppercase keywords but the result is the same : syntax error on this line -> IF FN_readlinesocket(accept_socket%, 500, LINE$)
if you have un idea or an other example ?
Read again what I wrote above - LINE is a keyword, you cannot use it as a variable name! :roll:

The code I listed works correctly, so long as you do not enable the Lowercase keywords option and copy-and-paste it accurately.

In particular, compare the line you listed:

Code: Select all

      IF FN_readlinesocket(accept_socket%, 500, LINE$)
with the code I originally listed:

Code: Select all

      IF FN_readlinesocket(accept_socket%, 6000, line$)
Hopefully you can see that they are not the same.
Jo.2a
Posts: 4
Joined: Tue 14 Feb 2023, 13:32

Re: HTML

Post by Jo.2a »

Thanks
I'm French and i realize that my english is very bad
I will try as soon as possible with your indications
have a nice day
Jo