Giter Club home page Giter Club logo

Comments (6)

gdamore avatar gdamore commented on July 24, 2024

You can allocate a listener, and then set the NNG_OPT_LOCADDR on it. That can specify a complete address -- including the TCP port number.

from nng.

rty813 avatar rty813 commented on July 24, 2024

I want to specify dialer's port, should I set NNG_OPT_LOCADDR on listener?

from nng.

rty813 avatar rty813 commented on July 24, 2024

In reqrep demo, I call nng_dialer_get_addr in client's every loop, but the sa_addr and sa_port different from every call. Is this OK?
And how to decode the address? for example, sa_addr = 2123998088

while (1) {

  printf("CLIENT: SENDING DATE REQUEST\n");
  if ((rv = nng_send(sock, cmd, sizeof(cmd), 0)) != 0) {
	  fatal("nng_send", rv);
  }
  if ((rv = nng_recv(sock, &buf, &sz, NNG_FLAG_ALLOC)) != 0) {
	  fatal("nng_recv", rv);
  }
  
  if (sz == sizeof(uint64_t)) {
	  uint64_t now;
	  GET64(buf, now);
	  printf("CLIENT: RECEIVED DATE: ");
	  showdate((time_t) now);
  } else {
	  printf("CLIENT: GOT WRONG SIZE!\n");
  }
  nng_msleep(sleep);
  sleep++;
  if (sleep == 4) {
	  sleep = 4000;
  }
        nng_sockaddr addr;
        nng_dialer_get_addr(dialer, NNG_OPT_LOCADDR, &addr);
        printf("%d, %d\n", addr.s_in.sa_port, addr.s_in.sa_addr);
  }

image

from nng.

rty813 avatar rty813 commented on July 24, 2024

@gdamore any suggestion ?

from nng.

gdamore avatar gdamore commented on July 24, 2024

The documentation should indicate properly that the option for NNG_OPT_LOCADDR is an nng_sockaddr. However, be advised that the port numbers are in network byte order, not host byte order. For sa_addr, you're looking at the network byte order IP v4 address - so:

126.153.163.136

This is standard BSD socket practice.

When you call dial, by default you'll get a unique local address -- because that's the way TCP works. You can request a specific source address, but that requires you set that option on the dialer. Note that doing so will impair automatic redialing if the connection is lost, and will prevent being able to establish multiple outbound connections so I don't recommend it unless you have no choice and you know exactly what you're doing (which doesn't seem to be the case here.)

from nng.

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.