JanetDocsI'm feeling luckyGithub sign in

net/server



    cfunction

    (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
Loading...
# 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)
cellularmitosis
$ # 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
cellularmitosis