There is:

char* input = "google.com"; //char* input = "192.168.1.1"; 

It is necessary that the function could distinguish the DNS name from the IP. How to implement. Through try {} catch {} trying to process the input string with gethostbyname () or inet_add ()? Or is there another option?

    1 answer 1

    try-catch do not channel. Who will excite these same try-catch? Therefore, it is probably easier to use gethostbyname, and then check return codes. Well, if you want try-catch, then use throw to throw an exception by return codes from functions. Alternatively, yes, use inet_addr. An example of using gethostbyname is at http://msdn.microsoft.com/en-us/library/ms738524(v=vs.85).aspx>MSDN. Also take a look at this. I can't figure out how to get IP by DNS name! : (> question and code examples to it.

    • Well yes. This C # has infected me. That is, try this: use gethostbyname (), if it returns "not something", then IP? - Jakeroid
    • It is necessary to analyze the returned hostent structure. Read the information on the link. Everything is there. Briefly: we give DNS -> we receive IP. We give IP -> we also get IP. - gecube
    • First, I check for different INADDRANY entries ("0.0.0.0" "255.255.255.255" "inaddr_any" "inaddrany" (both nocase)), then inet_addr () and last of all gethostbyname (). No one has complained yet. - avp
    • one
      By the way, what about the regex.h use case? Or boost :: regex? - gecube
    • 2
      inet_aton will not save the father of Russian democracy? If 0 returns - it means no ip-address at the entrance. - alexlz