-
-
Save iffy/cb7b01c15990cf34433e821bb21c49d1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## 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