Socket webserver

Q&A's, tips, howto's
Locked
hilti
Posts: 140
Joined: Sun Apr 19, 2009 10:09 pm
Location: Hannover, Germany
Contact:

Socket webserver

Post by 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

Code: Select all

#!/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 OK\r\n"
	"Server: LispIO\r\n"
	"Content-Type: text/html\r\n"
	"\r\n"
	[/text]
	)
	(net-send-to (nth 1 msg) (nth 2 msg) response connection)
	(println "Hello World")
)

(exit)

client.lsp

Code: Select all

#!/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:

Code: Select all

Enter something -> hello
=> 
	"HTTP/1.0 200 OK\r\n"
	"Server: LispIO\r\n"
	"Content-Type: text/html\r\n"
	"\r\n"
But why can I not access http://127.0.0.2:8080 in my browser?

Thanks for help.
-Hilti
--()o Dragonfly web framework for newLISP
http://dragonfly.apptruck.de

winger
Posts: 46
Joined: Wed Mar 14, 2012 7:31 am

Re: Socket webserver

Post by winger »

Code: Select all

;you could see msg first . It's different from manual now.
(net-send-to (first (parse (nth 1 msg)":")) (nth 2 msg) response connection)
UDP 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:
nc -u 127.0.0.2 8080
Use TCP mode calling net-listen function if you want access http://127.0.0.2:8080 by browser.
Welcome to a newlisper home:)
http://www.cngrayhat.org

Locked