import asyncdispatch, asyncnet
template asyncDo(x) = asyncCheck (proc {.async.} = x)()
let s = newAsyncSocket()
s.setSockOpt(OptReuseAddr, true)
s.bindAddr Port 12345
s.listen
let c1, c2 = newAsyncSocket()
for c in [c1, c2]:
asyncCheck c.connect
Please try it without the `asyncDo` template, that's extraordinary enough to
possibly be causing a compiler bug.
Right, this works as expected:
import asyncdispatch, asyncnet
let s = newAsyncSocket()
s.setSockOpt(OptReuseAddr, true)
s.bindAddr Port 12345
s.listen
let c1, c2 = newAsyncSocket()
for c in [c1, c2]:
asyncCheck c.connect("localhost", Port 12345
It's not a bug, it's a feature :) You probably missed
[this](https://nim-lang.github.io/Nim/manual.html#closures-creating-closures-in-loops)
part of the manual where it is explained that closures capture variable by
reference. In your example the inner `serve` captures `c` by reference and, as
Thanks for help.
> In your example the inner serve captures c by reference and, as a result,
> you're always listening on the last accepted connection.
I do not understand. To me, it looks like `let c =` will bind a new `c`, so
that the previous iteration's closure will retain a reference to the previous
`c`, w
not only it needs a copy but it seems to require protecting by closureScope.
This is a bit of a hassle really, I'd personally avoid closures in loops.
import asyncdispatch, asyncnet, strutils
let s = newAsyncSocket()
s.setSockOpt(OptReuseAddr, true)
s.bindAddr Port 1