Socket webserver

Started by hilti, September 10, 2012, 10:15:09 PM

Previous topic - Next topic

hilti

Hi!



I don't understand why I can't access an established socket connection via my browser or telnet? I'm trying to build a simple socket-based webserver.



server.lsp
#!/usr/bin/env newlisp

;; SET IP and PORT
(set 'ip "127.0.0.2")
(set 'port 8080)

;; BUILD UP SOCKET
(set 'connection (net-listen port ip "multi"))
(if connection
(println "LispIO webserver started on " ip ":" port)
    (println (net-error))
)

;; ENTER LOOP
(while (not (net-error))
(set 'msg (net-receive-from connection 1024))
;(println "->" msg)
(set 'response [text]
"HTTP/1.0 200 OKrn"
"Server: LispIOrn"
"Content-Type: text/htmlrn"
"rn"
[/text]
)
(net-send-to (nth 1 msg) (nth 2 msg) response connection)
(println "Hello World")
)

(exit)




client.lsp

#!/usr/bin/newlisp

(set 'socket (net-listen 8081 "" "multi"))
(if (not socket) (println (net-error)))
(while (not (net-error))
(print "Enter something -> ")
(net-send-to "localhost" 8080 (read-line) socket)
(net-receive socket buff 255)
(println "=> " buff)
)


Starting up server.lsp and client.lsp in two different terminal windows. Entering some text in the client window is immediately pushed to the server, which response with:



Enter something -> hello
=>
"HTTP/1.0 200 OKrn"
"Server: LispIOrn"
"Content-Type: text/htmlrn"
"rn"


But why can I not access http://127.0.0.2:8080">http://127.0.0.2:8080 in my browser?



Thanks for help.

-Hilti
--()o Dragonfly web framework for newLISP

http://dragonfly.apptruck.de\">http://dragonfly.apptruck.de

winger

#1

;you could see msg first . It's different from manual now.
(net-send-to (first (parse (nth 1 msg)":")) (nth 2 msg) response connection)


QuoteUDP multicast communications



If the optional string str-mode is specified as "multi" or "m", net-listen returns a socket suitable for multicastingUDP multicast communications



If the optional string str-mode is specified as "multi" or "m", net-listen returns a socket suitable for multicasting


test by nc udp mode:
Quote
nc -u 127.0.0.2 8080

Use TCP mode calling net-listen function  if you want access http://127.0.0.2:8080">http://127.0.0.2:8080 by browser.
Welcome to a newlisper home:)

http://www.cngrayhat.org\">//http://www.cngrayhat.org