Continuing the theme why the tcp server does not receive an external ip address?
@avp, sorry for the pressure, but I want to deal with this topic, probably I can not ask the right question, because The answers are not quite on the topic that I mean.
I know that ipconfig provides these interfaces and that -, -, - are left for internal use. I have a dynamic IP. Both the IP and the default gateway are the same.
Both the server and the client both have a dynamic IP and access the Internet via the local network.
I will try to reformulate the question:
1. Is it possible to write a program in C ++ that will write to a variable and output data about all my interfaces and their ip to the screen (what I see in ipconfig / all), possibly without using sockets?
2. Or question 1, but using sockets? Solved part of the issue

C ++ local ip code:

#include "conio.h" #include "iostream" #include "windows.h" #include "winsock.h" int main() { WSADATA ws; hostent *h; char buf[128]; if (WSAStartup (MAKEWORD (1,1), &ws) == 0) { if (gethostname(&buf[0], 128) == 0) { printf("machine name %s \n", buf); h = gethostbyname(&buf[0]); if (h != NULL) { printf("ip local %s \n", inet_ntoa (*(reinterpret_cast<in_addr *>(*(h->h_addr_list)))) ); printf("ip global ?\n"); } else { printf("er2"); } } WSACleanup(); getch(); } return 0; } 

and that still external ip deduced.
3. Maybe you need to use a proxy so that both of them know the ip of the proxy and connect to each other and transmit information through it?
4. Or write code to recognize ip through sites like myip_ru?
5. Or does my task have no solution at all?
6. Where is the interface data stored by the console when Ipconfig / all is requested? If it is possible in detail, there in the OS, in a variable, because if I see them, it means they are somehow obtained and stored somewhere.

  • 2
    You cannot get external ip from the inside because There can be quite complex routing schemes. For computers behind NAT, it is necessary to use external services. For example, in VoIP telephony they use STUN servers, which return an external ip upon request. In any case, for p2p connections, besides the existence of a route from one client to another, they need to know about this route, this is usually achieved using a separate dedicated server that helps to establish this connection. - Yura Ivanov
  • 1. You can fool everyone and take the results of ipconfig / all (after processing them accordingly) 2. You can use the call getaddrinfo (… ) There is an example. - alexlz

1 answer 1

@Lekksa, there are a lot of questions, and time later, so I will try to answer, but in parts. If possible, I will complement the answer.

I warn you in advance, I am programming in * nix-ah.

  1. Is it possible to write a program in C ++ that will write to a variable and output data about all my interfaces and their ip to the screen (what I see in ipconfig / all), possibly without using sockets?

Here is an example (a piece from a program in Linux)

 // avp 2011 #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/types.h> #include <sys/time.h> #include <unistd.h> #include <fcntl.h> #include <ctype.h> #include <errno.h> #include <ifaddrs.h> #include <sysexits.h> #include "wa.h" /* returns список адресов out != NULL печать loglevel != 0 wa_log prionly != 0 return NULL Для free вызывать freeifaddrs(Result) */ struct ifaddrs * get_ipaddrs (FILE *out, int prionly, int loglevel) { struct ifaddrs * ifres=NULL; struct ifaddrs * ifa=NULL; void * tmp=NULL; char buf[INET6_ADDRSTRLEN]; getifaddrs(&ifres); for (ifa = ifres; ifa != NULL; ifa = ifa->ifa_next) { if (ifa ->ifa_addr->sa_family==AF_INET) { // check it is IP4 // is a valid IP4 Address tmp=&((struct sockaddr_in *)ifa->ifa_addr)->sin_addr; inet_ntop(AF_INET, tmp, buf, INET_ADDRSTRLEN); if (out) fprintf(out,"IP4 %s IP Address %s\n", ifa->ifa_name, buf); if (loglevel) wa_log(INFO,"IP4 %s IP Address %s\n", ifa->ifa_name, buf); } else if (ifa->ifa_addr->sa_family==AF_INET6) { // check it is IP6 // is a valid IP6 Address tmp=&((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr; inet_ntop(AF_INET6, tmp, buf, INET6_ADDRSTRLEN); if (out) fprintf(out,"IP6 %s IP Address %s\n", ifa->ifa_name, buf); if (loglevel) wa_log(INFO,"IP6 %s IP Address %s\n", ifa->ifa_name, buf); } } if (prionly) { if (ifres!=NULL) freeifaddrs(ifres); ifres = NULL; } return ifres; } 

In it, of course you do not need the part, but I think everything is clear with the seal. Really I have logged

 2012/09/30 02:51:50 [1992:1992] Start logging (pid 1992 tid 1992) at Sun, 30 Sep 2012 02:51:50 MSK 2012/09/30 02:51:50 [1992:1992] Working directory: /home/avp/src/ig/web-agent 2012/09/30 02:51:50 [1992:1992] IP4 lo IP Address 2012/09/30 02:51:50 [1992:1992] IP4 eth0 IP Address 2012/09/30 02:51:50 [1992:1992] IP6 lo IP Address ::1 2012/09/30 02:51:50 [1992:1992] IP6 eth0 IP Address fe80::a00:27ff:fe2e:5127 2012/09/30 02:51:50 [1992:1992] Start listening interface INADDR_ANY port 12380 

Actually, you need an analogue call getifaddrs() (see man getifaddrs) for Windows.

Answers to other questions

  1. already
  2. Sockets are not needed
  3. One of the options
  4. IMHO will not help
  5. The solution is definitely there. Look in the direction of "dynamic dns" in Google and / or "port forwarding" in the router. Frankly, he never dealt with such things. Always either worked with statics, or networkers provide communication.
  6. The data is stored in the OS kernel, set when the address is loaded / received via DHCP.

For some reason, the numbers "slip" on 1 (and it does not work with 2)

PS I hope someone from those who are truly competent in networks will pick up the topic.