' QB64 HTTP server demo ' Run as Administrator if using port 80 OPTION _EXPLICIT DIM host&: host& = _OPENHOST("TCP/IP:8080") ' safer to test on 8080 IF host& = 0 THEN PRINT "Failed to bind port. Try running as Administrator or use another port." END END IF PRINT "Listening on port 8080..." DO IF _CONNECTIONPENDING(host&) THEN DIM client&: client& = _OPENCONNECTION(host&) IF client& THEN DIM request$, chunk$ request$ = "" ' Read until CRLF CRLF (end of HTTP headers) DO IF LOC(client&) > 0 THEN chunk$ = INPUT$(LOC(client&), client&) request$ = request$ + chunk$ IF INSTR(request$, CHR$(13) + CHR$(10) + CHR$(13) + CHR$(10)) THEN EXIT DO END IF _LIMIT 120 LOOP ' Prepare response DIM body$, response$ body$ = "Hello from QB64 HTTP server!" response$ = "HTTP/1.1 200 OK" + CHR$(13) + CHR$(10) response$ = response$ + "Content-Type: text/plain" + CHR$(13) + CHR$(10) response$ = response$ + "Content-Length: " + LTRIM$(STR$(LEN(body$))) + CHR$(13) + CHR$(10) response$ = response$ + "Connection: close" + CHR$(13) + CHR$(10) + CHR$(13) + CHR$(10) response$ = response$ + body$ PRINT #client&, response$ _CLOSE client& END IF END IF _LIMIT 240 LOOP