JanetDocsI'm feeling luckyGithub sign in



    (net/server host port handler)
    Start a TCP server. handler is a function that will be called with a
    stream on each connection to the server. Returns a new stream that is
    neither readable nor writeable.

2 examplesSign in to add an example
# note, if running a server from the repl, you need to (quit) your repl.

# in a terminal:
# $ while true; do date | nc 1234 -w 1; sleep 1; done

# in a janet repl:
(net/server "" 1234
  (fn [conn]
    (prin (net/read conn 4096))
    (net/close conn)))
# output doesn't actually start until you (quit) your repl's fiber:
$ # 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")
(net/server "" 1234 handle-conn)
$ chmod +x srv.janet
$ ./srv.janet


$ # in another terminal:
$ echo hello | nc 1234