GLBasic forum

Main forum => GLBasic - en => Topic started by: matchy on 2013-May-26

Title: Simplified web server listen sockets
Post by: matchy on 2013-May-26
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]
Title: Re: Simplified web server listen sockets
Post by: Marmor on 2013-May-29
be patient sry ....
Title: Re: Simplified web server listen sockets
Post by: erico on 2013-May-29
...like water? :P :-[
Title: Re: Simplified web server listen sockets
Post by: matchy on 2013-May-29
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.
Title: Re: Simplified web server listen sockets
Post by: hardyx on 2013-May-30
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
Title: Re: Simplified web server listen sockets
Post by: matchy on 2013-May-30
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.
Title: Re: Simplified web server listen sockets
Post by: kanonet on 2013-May-30
I would like to see this project when its finished. ;)
Title: Re: Simplified web server listen sockets
Post by: matchy on 2013-May-30
kanonet, what else are you expecting as it is minimal?  ;/  :S
Title: Re: Simplified web server listen sockets
Post by: kanonet on 2013-May-30
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.
Title: Re: Simplified web server listen sockets
Post by: hardyx on 2013-May-30
You can make a mini server in the Raspberry Pi with GLBasic. :happy:
Title: Re: Simplified web server listen sockets
Post by: Marmor on 2013-May-30
thx for the help , iam busy this week
Title: Re: Simplified web server listen sockets
Post by: matchy on 2013-May-31
hardyx, perhaps a simple gpio control panel web server.  ;)
Title: Re: Simplified web server listen sockets
Post by: mentalthink on 2013-May-31
This it's very good for arduino too , thanks for the code... matchy very usfeull. , this and Html5 can do very interesting things...  :noggin: