Hello. It is necessary to output IP to Edit in C ++ Builder, everything works well. But the problem is that h never zero, if the computer is not online, it outputs 127.0.0.1, but I would like it to be output

 ShowMessage("Вы не в сети."); 

Here is the code:

 const int WSVer = 0x101; WSAData wsaData; hostent *h; char Buf[128]; if (WSAStartup(WSVer, &wsaData) == 0) { if (gethostname(&Buf[0], 128) == 0) { h = gethostbyname(&Buf[0]); if (h != NULL) Edit4->Text=(inet_ntoa (*(reinterpret_cast<in_addr *>(*(h->h_addr_list))))); else ShowMessage("Вы не в сети."); WSACleanup(); } } 

Maybe give a good link on this topic.
Thank you all in advance.

    2 answers 2

    Maybe just to check that the IP does not match the mask 127. *?

    • how? I thought I could check with h, that is, enter another variable (for example f), but I don’t understand what type h has. I get an error, try and check strstr, nothing works, does not compile ( - chip
    • Look at the source of the hostent data type, or Google will save: netcode.ru/cpp/… . let's look at h_addr_list (property of the hostent structure, of which h is an instance) and if at least one element is 127. *, then the network is not connected. - uramer239
    • Thanks, I’ve already invented it (though it’s output to edit, but the main thing for me was to display the message): <pre> Edit4-> Text = (inet_ntoa (* (reinterpret_cast <in_addr *> (* (h-> h_addr_list)))))); if (Edit4-> Text == "127.0.0.1") ShowMessage ("You are offline."); </ pre> - chip

    And it will not be equal to 0. Catch the LocalHost range and output what you need.

    • Can you explain in more detail, or an example, or read a link, something I on this subject can not find anything good. - chip