Comments (4)
This Port
argument was added to stay compatible with synchronous getAddrInfo
procedure declared here:
https://github.com/nim-lang/Nim/blob/devel/lib/pure/nativesockets.nim#L222
This Port
is not used in DNS request actually, but used to form up result array of returned addresses. If you don't care about, or don't know, please use Port(0)
.
This is simple example for server side
:
var server_socket = create_server_socket()
var aiList = getAddrInfo("0.0.0.0", Port(13337))
if bindAddr(server_socket, aiList.ai_addr,
aiList.ai_addrlen.Socklen) < 0'i32:
freeAddrInfo(aiList)
raiseOSError(osLastError())
As you can see you can easily pass aiList
to bindAddr
and your server will setup on 0.0.0.0:13337
.
from asynctools.
Compilation error, you mentioned got fixed.
from asynctools.
Hi, thank you very much for the quick response! I have managed to get it working, but I can't make the jump from Python's asyncio to Nim in my head. Sorry for being thick here! I was wondering if you would be willing to help. I have a very simple example of what I am trying to do in some pseudo-code that isn't working:
import asyncdispatch
import asynctools/asyncdns
proc lookup(n: string) {.async.} =
return await asyncGetAddrInfo(n, Port(0))
proc callback(f: Future) =
echo("$1 resolved to ip $2".format(f.domain, f.ip))
proc main() {.async.} =
let domains = @["www.google.ca", "www.microsoft.com", "www.apple.com"]
var answers = @[]
for domain in domains:
lookup(domain)
# add callback?
waitFor main()
I have Googled around quite a bit and I can't seem to figure it out. Again, thanks so much!
from asynctools.
For anyone else that has the same questions as me I'll continue this thread. This code works but doesn't do exactly what I want:
edit: updated code
import asyncdispatch, typetraits, nativesockets
import asynctools/asyncdns
type
DNSQueryResult = ref object
name: string
error: bool
ans: ptr AsyncAddrInfo
proc lookup(n: string): Future[DNSQueryResult] {.async.} =
var response = await asyncGetAddrInfo(n, Port(0))
return DNSQueryResult(name:n, error:false, ans:response)
proc main() {.async.} =
let domains = @["www.google.ca", "www.microsoft.com", "www.apple.com"]
var futures = newSeq[Future[DNSQueryResult]]()
for domain in domains:
futures.add(lookup(domain))
let results = await all futures
for r in results:
echo(r.name)
echo(r.ans)
echo()
waitFor main()
I'd like to be able to handle each response in a callback. Also, I don't know what the best way to get the IP address from the answer is. Still researching...
from asynctools.
Related Issues (20)
- asyncipc doesn't work on macos HOT 2
- Ubuntu 18.04 compile error for asyncpipe HOT 1
- The AsyncIPC example does not work on windows HOT 2
- execProcess unhandled exception: index out of bounds
- Fails to compile on 0.19.0 HOT 1
- asyncproc does not expose close procedure
- AsyncProc crashes when multiple processes are started
- missing .gcsafe in asyncsync HOT 2
- Help with asyncproc simultaneous stdout & stderr? HOT 1
- asyncpipe.nim(260, 16) Error: undeclared identifier: 'PCustomOverlapped' on windows HOT 4
- Crash when starting more than one asynchronous processes on Windows.
- Issues with using asyncPipe and faststream
- AsyncIpc seems to be broken on devel for Windows HOT 1
- Error: expression cannot be cast to 'pointer' HOT 2
- async stdin and stdout on Windows
- how to feed commands to external process and catch output HOT 1
- Nim v2: asyncpipe.createPipe in windows broken by upstream
- Async task not waiting for event once fired
- Multiple A records for one name HOT 9
- Terminating async process on macOS doesn't work HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from asynctools.