Simplified web server listen sockets

Previous topic - Next topic

matchy

Here is a simply constructed http web server but I can only get one request. Sock closes and listens again, on another sock id, but not accepting the next one. Help!?  :-[

Update: solved - works okay now.

Code (glbasic) Select

// --------------------------------- //
// Project: webservermin
// Start: Sunday, May 26, 2013
// IDE Version: 10.283 / 11.414

TYPE _net
num = 0
ok = 0
accept = -1
listen = 0
ip% = 0
sock = 0
port = 80
rv = 0
length = 2048
msg$ = 0
ENDTYPE

TYPE _bug
x
y
log$[]
FUNCTION out: msg$
DEBUG msg$ + "\n"
ENDFUNCTION
ENDTYPE

GLOBAL net AS _net
GLOBAL bug AS _bug

loop()

FUNCTION loop:
AUTOPAUSE FALSE
ALLOWESCAPE TRUE
SYSTEMPOINTER TRUE
setup_webserver()
WHILE TRUE
CLEARSCREEN
loop_webserver()
SHOWSCREEN
WEND
ENDFUNCTION

FUNCTION setup_webserver:
net.ok = SOCK_INIT()
net.listen = SOCK_TCPLISTEN(net.port)
bug.out("net.listen:" + net.listen)
ENDFUNCTION

FUNCTION loop_webserver:
IF net.accept = -1
net.accept = SOCK_TCPACCEPT(net.listen, net.ip)
IF net.accept <> -1
bug.out("net.accept:" + net.accept)
ENDIF
ELSE
net.rv = SOCK_RECV(net.accept, net.msg$, net.length)
IF net.rv > 0
parse_html()
ENDIF
IF net.rv = -2
net.msg$ = "<html><body>Hello World!</body></html>"
net.ok = SOCK_TCPSEND(net.accept, net.msg$)
net.rv = SOCK_CLOSE(net.accept)
bug.out("net.rv:" + net.rv)
net.accept = -1
///// net.listen = SOCK_TCPLISTEN(net.port)   //// no need to relisten - solved
bug.out("net.listen:" + net.listen)
ENDIF
ENDIF
ENDFUNCTION

FUNCTION parse_html:
bug.out("msg:" + net.msg$)
LOCAL arr$[]
LOCAL i = SPLITSTR(net.msg$, arr$[], " ")
bug.out("arr$[1]:" + arr$[1])
ENDFUNCTION
[code=glbasic]

Marmor


erico


matchy

ok Marmor but, to flow like water, isn't there any other sample code somewhere for webserver? I'm just not sure if I am handling the http requests correctly.

hardyx

#4
Look the last posts of this thead:
http://www.glbasic.com/forum/index.php?topic=4153.msg30553

I think you must to change the IF condition in loop_webserver with this. You must to call SOCK_TCPACCEPT in each loop, not SOCK_TCPLISTEN. This creates a new client connection from the requests queued in the listening socket. I don't tried this but it must work.

Code (glbasic) Select
               
    IF net.rv = -2
            net.msg$ = "<html><body>Hello World!</body></html>"
            net.ok = SOCK_TCPSEND(net.accept, net.msg$)
            net.rv = SOCK_CLOSE(net.accept)
            bug.out("net.rv:" + net.rv)

            net.accept = SOCK_TCPACCEPT(net.listen, net.ip)
            bug.out("net.accept:" + net.accept)
    ENDIF

matchy

Oh you are right hardyx,  :booze:it works now that I just simply remove the second TCPLISTEN. There's no need to add the second TCPACCEPT as it is loops back to the first one. Cheers.

kanonet

I would like to see this project when its finished. ;)
Lenovo Thinkpad T430u: Intel i5-3317U, 8GB DDR3, NVidia GeForce 620M, Micron RealSSD C400 @Win7 x64

matchy

kanonet, what else are you expecting as it is minimal?  ;/  :S

kanonet

Ah so its finished now? Did think it was just created as a prototype and might get integrated/reused in some fancy big project (no idea what kind) later, thats why I asked.
Lenovo Thinkpad T430u: Intel i5-3317U, 8GB DDR3, NVidia GeForce 620M, Micron RealSSD C400 @Win7 x64

hardyx

You can make a mini server in the Raspberry Pi with GLBasic. :happy:

Marmor

thx for the help , iam busy this week

matchy

hardyx, perhaps a simple gpio control panel web server.  ;)

mentalthink

This it's very good for arduino too , thanks for the code... matchy very usfeull. , this and Html5 can do very interesting things...  :noggin: