c - How to use getaddrinfo to connect to a server using the external IP? -


i'm writing small c client/server application, cannot make connection work when using external ip address. code both client , server taken here, in particular clients do:

    char *default_server_name = "localhost";     char *server_name = null;     int nport = default_dama_port;     char port[15];      // parse command line options     if (parse_options(argc, argv, &server_name, &nport) < 0) {         return -1;     }      if (server_name == null) {         server_name = default_server_name;     }      snprintf(port, 15, "%d", nport);      // connect server     int client_socket;     struct addrinfo hints, *servinfo, *p;     int rv;      memset(&hints, 0, sizeof(hints));     hints.ai_family = af_unspec;     hints.ai_socktype = sock_stream;     if ((rv = getaddrinfo(server_name, port, &hints, &servinfo)) != 0) {         fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));         exit(1);     }      (p=servinfo; p != null; p = p->ai_next) {         if ((client_socket = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) { #ifdef debug             perror("socket"); #endif             continue;         }          if (connect(client_socket, p->ai_addr, p->ai_addrlen) == -1) {             close(client_socket); #ifdef debug             perror("connect"); #endif             continue;         }          // connected succesfully!         break;     }      if (p == null) {         // loop wasn't able connect server         fprintf(stderr, "couldn't connect server\n.");         exit(1);     } 

while server:

    int nport;     char port[15];     if (parse_options(argc, argv, &nport) < 0) {         return -1;     }      snprintf(port, 15, "%d", nport);      int server_socket;     struct addrinfo hints, *servinfo, *p;     int rv;      memset(&hints, 0, sizeof(hints));     hints.ai_family = af_unspec;     hints.ai_socktype = sock_stream;     hints.ai_flags = ai_passive;      if ((rv = getaddrinfo(null, port, &hints, &servinfo)) != 0) {         fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));         exit(1);     }      (p=servinfo; p != null; p = p->ai_next) {         if ((server_socket = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) { #ifdef debug             perror("socket"); #endif             continue;         }          if (bind(server_socket, p->ai_addr, p->ai_addrlen) == -1) {             close(server_socket); #ifdef debug             perror("bind"); #endif             continue;         }          // binded successfully!         break;     }      if (p == null) {         fprintf(stderr, "failed bind socket\n");         exit(2);     }      int pl_one, pl_two;     socklen_t pl_one_len, pl_two_len;     struct sockaddr_in pl_one_addr, pl_two_addr;      if (listen(server_socket, 2) < 0) {         fatal_error("error in syscall listen.", 2);     }      // 2 clients connections.     pl_one_len = sizeof(pl_one_addr);     pl_one = accept(server_socket,                     (struct sockaddr *)&pl_one_addr,                     &pl_one_len);     if (pl_one < 0) {         fatal_error("error in syscall accept.", 3);     }      pl_two_len = sizeof(pl_two_addr);     pl_two = accept(server_socket,                     (struct sockaddr *)&pl_two_addr,                     &pl_two_len);     if (pl_two < 0) {         fatal_error("error in syscall accept.", 3);     } 

if specify ip of machine in command line, , hence server_name in clients set string 151.51.xxx.xxx, sockets cannot connect server. using 127.0.0.1 shows same behaviour, makes me think when documentation states:

the host name you're interested in goes in nodename parameter. address can either host name, "www.example.com", or ipv4 or ipv6 address (passed string).

it's joking.

am doing incorrectly? there problem firewall etc. preventing clients connect using ip addresses?

note: i've searched lot problem , people avoid using getaddrinfo @ , directly fill inaddr_any, getaddrinfo documentation states passing null nodename should fill address inaddr_any, don't see why should use old method when new 1 automatically.

as written in comments trying connect server located in network behind router.

127.0.0.1 or localhost redirections of operating system , not go on router. that's why worked you.

if specify external ip address, connection made via router , ports use need forwarded in router configuration. normal home internet end user router blocks incoming connections port default.


Comments

Popular posts from this blog

Perl - how to grep a block of text from a file -

delphi - How to remove all the grips on a coolbar if I have several coolbands? -

javascript - Animating array of divs; only the final element is modified -