function
boot.janet on line 3921 , column 3
(net/server host port &opt handler type no-reuse )
Starts a server with `net/listen` . Runs `net/accept-loop`
asynchronously if `handler` is set and `type` is `:stream` (the
default ). It is invalid to set `handler` if `type` is `:datagram` .
Returns the new server stream.
# note, if running a server from the repl, you need to (quit) your repl.
# in a terminal:
# $ while true; do date | nc 0.0.0.0 1234 -w 1; sleep 1; done
# in a janet repl:
(net/server "0.0.0.0" 1234
(fn [conn ]
(prin (net/read conn 4096 ))
(net/close conn )))
# output doesn't actually start until you (quit) your repl's fiber:
(quit )
$ # trivial server which echo's to the console.
$ cat srv.janet << EOF
#!/usr/bin/env janet
(defn handle-conn [conn ]
(print "new connection" )
(while true
(def data (net/read conn 4096 ))
(if (not data ) (break ))
(prin data ))
(net/close conn )
(print "connection closed" ))
(print "starting server on 0.0.0.0:1234" )
(net/server "0.0.0.0" 1234 handle-conn )
EOF
$ chmod +x srv.janet
$ ./srv.janet
----
$ # in another terminal:
$ echo hello | nc 0.0.0.0 1234