Dave's Brain

Browse - Programming Tips - Do you have some simple C/C++ code to validate an internet host name / IP-address ?

Date: 2015feb6
Language: C/C++

Q.  Do you have some simple C/C++ code to validate an internet host name / IP-address ?

A.

// helper inline bool isValidHostChar(const char c) { return isalnum(c) || c == '.' || c == '-' || c == '_'; } // Not perfect but catches most user mistakes bool isValidHost(const char *host) { if (host == NULL) return false; if (strlen(host) < 3) return false; for (const char *p = host; *p; p++) { if (!isValidHostChar(*p)) return false; } return true; }

Add a comment

Sign in to add a comment
Copyright © 2008-2017, dave - Code samples on Dave's Brain is licensed under the Creative Commons Attribution 2.5 License. However other material, including English text has all rights reserved.