I want to write a program that listens on a http port and can send a webpage back

General FreeBASIC programming questions.
Post Reply
f0rgotten
Posts: 7
Joined: Dec 12, 2018 23:55

I want to write a program that listens on a http port and can send a webpage back

Post by f0rgotten »

Hello, and to whom it may concern,

I want to write a freebasic program that can listen on an arbitrary port and respond to the url sent on that port with a dynamically rendered page. I'm pretty comfortable with the actual programming, but I'm not sure how to get freebasic to listen to the web.

Any pointers to the right page in the manual would be just fine, or suggestions on how to do this would be lovely.
adeyblue
Posts: 301
Joined: Nov 07, 2019 20:08

Re: I want to write a program that listens on a http port and can send a webpage back

Post by adeyblue »

I'm sure there are nicer wrappers or libraries for this somewhere on the forum that someone else will recommend, but at the most basic its the same as C. For a server, you use the functions
socket
bind
listen
accept
recv
send
shutdown
close(socket)

Those 8* are pretty much all you need and the same whether you're Windows-y or Linux-y, or something-else-y. Which include files you need for them does change depending on what platform you're building on.

A Windows tutorial for a simple client/server in C starts here. Implementing actual HTTP over that is just a matter of reading the requests, then sending out properly formatted responses. HTTPS is more complicated.

* Well, Windows requires an extra one, WSAStartup, before you do any of those
caseih
Posts: 2158
Joined: Feb 26, 2007 5:32

Re: I want to write a program that listens on a http port and can send a webpage back

Post by caseih »

Theoretically FB already has support for GTK3 (bi files), although I cannot seem to locate a workable code example There is a library designed to work with GTK (without a GUI) called libsoup. It's a nice high-level library that is event driven, so you don't have to implement your own threads to listen for connections. Apparently it can be used in a synchronous manner also.

https://wiki.gnome.org/Projects/libsoup

As I said, FB should already be able to interface with it using GObject Introspection. But examples for GTK3 with GIR are lacking last I checked.

EDIT: actually I can't find any GIR header files in FB. I swear I saw them once. They eliminate the need to port each gtk header file individually.
thrive4
Posts: 72
Joined: Jun 25, 2021 15:32

Re: I want to write a program that listens on a http port and can send a webpage back

Post by thrive4 »

Maybe this could help:
viewtopic.php?t=4199&hilit=Simple+Web+Server&start=15
The modified code by 'cha0s' works rather well.
Tested on windows 7 with

Code: Select all

Const WM_SHELLNOTIFY  = WM_USER + 5
Const ID_RUN          = 1001
Const ID_PAUSE        = 1002
Const ID_EXIT         = 1003
Const SERVER_ADDR     = "127.0.0.1"
Const HOMEDIR         = "wwwroot"
Const DEFAULT_PORT    = 80
Const SERVER_BUFFSIZE = 16
Note no ssl support or only http:// no https://
Bye the bye a really small webserver for window is:
https://en.wikipedia.org/wiki/Xitami
https://imatix-legacy.github.io/xitami.com/
Also no native ssl support...
f0rgotten
Posts: 7
Joined: Dec 12, 2018 23:55

Re: I want to write a program that listens on a http port and can send a webpage back

Post by f0rgotten »

This is probably enough that I can come up with something from this. Thanks!
TJF
Posts: 3809
Joined: Dec 06, 2009 22:27
Location: N47°, E15°
Contact:

Re: I want to write a program that listens on a http port and can send a webpage back

Post by TJF »

Perhaps you'll find your needs in the NetToBac solution, a library with BASIC-like syntax. nOPEN and nCLOSE a network connection like a file on the disk and nGET or nPUT data over the socket. Find the docu at

https://users.freebasic-portal.de/tjf/P ... /doc/html/

Regards
f0rgotten
Posts: 7
Joined: Dec 12, 2018 23:55

Re: I want to write a program that listens on a http port and can send a webpage back

Post by f0rgotten »

That's even better. Thank you very much.
Post Reply