Skip to content

Instantly share code, notes, and snippets.

@iffy
Created October 8, 2019 02:54
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 iffy/cb7b01c15990cf34433e821bb21c49d1 to your computer and use it in GitHub Desktop.
Save iffy/cb7b01c15990cf34433e821bb21c49d1 to your computer and use it in GitHub Desktop.
## Run this in one session, then run the following in another:
##
## echo "hello" | nc 127.0.0.1 9001
##
import asyncdispatch
import asyncnet
proc serve(port: Port, address = "") {.async.} =
var server = newAsyncSocket()
server.setSockOpt(OptReuseAddr, true)
server.bindAddr(port, address)
server.listen()
let client = await server.accept()
# application code
let msg1 = client.recv(1024)
yield msg1
asyncCheck client.send("message 1")
client.close()
# end application code
if isMainModule:
asyncCheck serve(9001.Port, "127.0.0.1")
while true:
poll()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment