Skip to content

Instantly share code, notes, and snippets.

@bakpakin
Created August 10, 2020 19:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bakpakin/15bb384114f0e80b5bf89db0dc0312f3 to your computer and use it in GitHub Desktop.
Save bakpakin/15bb384114f0e80b5bf89db0dc0312f3 to your computer and use it in GitHub Desktop.
Broken pipe handling in server
(with [conn (net/connect "127.0.0.1" "8000")]
(printf "Connected to %q!" conn)
(:write conn "Echo...")
(print "Wrote to connection...")
(def res (:read conn 1024))
(print "client done")
(pp res))
#!/bin/bash
# Start server
janet server.janet &
SERVER_PID=$!
sleep 1
# Start client
janet client.janet &
CLIENT_PID=$!
# Wait a bit, and then kill client before it completes to trigger broken pipe.
sleep 0.5
kill -SIGINT $CLIENT_PID
wait $SERVER_PID
echo "Done"
(net/server
"127.0.0.1" "8000"
(fn handler
[stream]
(defer (:close stream)
(def b @"")
(while (:read stream 1024 b)
(for i 0 5
(os/sleep 2)
(print "write return value " i ": " (:write stream b))))
(os/exit 0))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment