c++ - Get the local ip address with unix sockets -


i programming simple udp client server chat little more familiar network programming. make easier user see servers ip address (so user can type client connect). that's why server display own local ip address.

this code: (edited code handle multiple ip's)

// hostname of server char hostname[128]; if (gethostname(hostname, sizeof(hostname)) == -1) {     std::cout << "could not hostname of server. error: " << std::strerror(errno) << std::endl;     return 1; }  struct addrinfo hints, *serverinfo, *p;  std::memset(&hints, 0, sizeof(hints)); hints.ai_family = af_unspec; // use af_inet (ipv4) or af_inet6 (ipv6) force version hints.ai_socktype = sock_dgram;  // try information our hostname int rv; if ((rv = getaddrinfo(hostname, null, &hints, &serverinfo)) != 0) {     std::cout << "failed information host \"" << hostname << "\". error: " << gai_strerror(rv) << std::endl;     return 1; }  std::cout << "ip addresses " << hostname << ":" << std::endl;  // iterate on infos got , try extract ip address for(p = serverinfo; p != null; p = p->ai_next) {     char serveripaddress[inet6_addrstrlen];     void *addr;     std::string ipversion;      // pointer address itself,     // different fields in ipv4 , ipv6:     if (p->ai_family == af_inet) { // ipv4         struct sockaddr_in *ipv4 = (struct sockaddr_in *)p->ai_addr;         addr = &(ipv4->sin_addr);         ipversion = "ipv4";     } else { // ipv6         struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)p->ai_addr;         addr = &(ipv6->sin6_addr);         ipversion = "ipv6";     }      // take ip address of our hostname , convert readable format     inet_ntop(p->ai_family, addr, serveripaddress, sizeof(serveripaddress));     std::cout << "  " << ipversion << ": " << serveripaddress << std::endl; }  freeaddrinfo(serverinfo); // free linked list 

it works, doesn't display correct ip address. everytime run there different ip being displayed , it's not correct one. said still learning network programming, don't know why is. point me in right direction?

you not able "global" ip address this, local one. in order client connect you, needs global ip address, visible internet, i.e. gateway's (router's) ip. because router translates computer's ip address globally visible 1 , stores in table, incoming connections can correctly routed correct sub-network. called nating (find out more nat here).

if want client connect explicitly, need give them router's ip address (use command-line tools this, depending on os) , configure port-forwarding router automatically forward packets arriving on (ip_router, port_router) (ip_host, port_host). done in router's config panel. depending on router, found somewhere in menus. can connect router typing 192.068.0.1, or 192.168.0.0.


Comments

Popular posts from this blog

c++ - QTextObjectInterface with Qml TextEdit (QQuickTextEdit) -

javascript - angular ng-required radio button not toggling required off in firefox 33, OK in chrome -

xcode - Swift Playground - Files are not readable -