Code Monkey home page Code Monkey logo

Comments (4)

cheatfate avatar cheatfate commented on August 17, 2024 1

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.

cheatfate avatar cheatfate commented on August 17, 2024 1

Compilation error, you mentioned got fixed.

from asynctools.

blark avatar blark commented on August 17, 2024

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.

blark avatar blark commented on August 17, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.