CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 20
  1. #1
    Join Date
    Apr 2001
    Posts
    1,029

    determining if a string is an ip address or a hostname?

    Hello,

    Is it possible to determine is a string is an ip address or a hostname? I need to be able to differentiate between the two.

    Thanks!

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: determining if a string is an ip address or a hostname?

    An IP address is numer . number . number . number. That should be easy enough to check for.

  3. #3
    Join Date
    Mar 2009
    Posts
    58

    Re: determining if a string is an ip address or a hostname?

    You could do it this way:


    Sorry dont know how to use code blocks, can some one tell us


    char *str="12.34.34.34:0.0";

    if(str[0] > '9')
    cout<<"not ip address"<<endl;
    else
    cout<<"ip address"<<endl;


    Hope this helps

  4. #4
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: determining if a string is an ip address or a hostname?

    Quote Originally Posted by newbie30 View Post
    You could do it this way:


    Sorry dont know how to use code blocks, can some one tell us


    char *str="12.34.34.34:0.0";

    if(str[0] > '9')
    cout<<"not ip address"<<endl;
    else
    cout<<"ip address"<<endl;


    Hope this helps
    What happens if the host name begins with a number?
    Last edited by GCDEF; March 5th, 2009 at 12:31 PM.

  5. #5
    Join Date
    Mar 2009
    Posts
    58

    Re: determining if a string is an ip address or a hostname?

    I was assuming that either his ip address was in this format:

    10.2.1.10:0.0 or a total string format ie asmith:0.0

    yes i know i shouldnt assume

  6. #6
    Join Date
    Jul 2007
    Posts
    609

    Re: determining if a string is an ip address or a hostname?

    This code calls up a lot of custom stuff that won't compile but may be of help to know how to go about it:

    Code:
    	bool ValidateIP(string ip)
    	{
    		xVector<string> exploded = Parser::Explode(ip,".");
    		
    		if(exploded.GetSize()!=4)return false;
    		
    		string tmp;
    		for(int i=0;exploded.Get(tmp,i);i++)
    		{
    			int octet = BitStream::Str2Int(tmp);
    			if(octet<0 || octet>255)return false;
    		}
    		
    		return true;
    	}

    Basically you want to explode the string by ".", then make sure that all you have is 4 ints, and that they are between 0 and 255.
    http://www.uovalor.com :: Free UO Server

  7. #7
    Join Date
    Feb 2009
    Location
    India
    Posts
    444

    Re: determining if a string is an ip address or a hostname?

    Just check if the first character is a digit
    isdigit(ipaddress[0])
    «_Superman
    I love work. It gives me something to do between weekends.

    Microsoft MVP (Visual C++)

  8. #8
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: determining if a string is an ip address or a hostname?

    Quote Originally Posted by _Superman_
    Just check if the first character is a digit
    In post #4, GCDEF asked: "What happens if the host name begins with a number?"
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  9. #9
    Join Date
    Feb 2009
    Location
    India
    Posts
    444

    Re: determining if a string is an ip address or a hostname?

    The following code snippet is from MSDN sample code for the function gethostbyaddr

    if (isalpha(host_name[0])) { /* host address is a name */
    printf("Calling gethostbyname with %s\n", host_name);
    remoteHost = gethostbyname(host_name);
    } else {

    http://msdn.microsoft.com/en-us/libr...21(VS.85).aspx
    «_Superman
    I love work. It gives me something to do between weekends.

    Microsoft MVP (Visual C++)

  10. #10
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: determining if a string is an ip address or a hostname?

    Quote Originally Posted by _Superman_
    The following code snippet is from MSDN sample code for the function gethostbyaddr
    In that case the sample code has a bug since GCDEF's point seems valid to me.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  11. #11
    Join Date
    Feb 2009
    Location
    India
    Posts
    444

    Re: determining if a string is an ip address or a hostname?

    If the hostname is given something like 123greetings.com, then it would be a problem.
    Names beginning with http:// or www would be fine.
    «_Superman
    I love work. It gives me something to do between weekends.

    Microsoft MVP (Visual C++)

  12. #12
    Join Date
    May 2007
    Location
    Scotland
    Posts
    1,164

    Re: determining if a string is an ip address or a hostname?

    You could always check the string to see if it contains letters... there is probably a more efficient and/or concise way of doing this, but something like the following should work:

    Code:
    #include <iostream>
    #include <string>
    #include <cctype>     //for tolower
    #include <algorithm>  //for std::find_if
    #include <functional> //for std::unary_function
    
    class IsAlpha : public std::unary_function<char,void>
    {
    public:
      bool operator()(char c)
      {
        int val = tolower(c);
        return (val>='a' && val<='z');
      }
    };
    
    bool ContainsAlpha(const std::string& s)
    {
      return(s.end() != std::find_if(s.begin(), s.end(), IsAlpha())); 
    }
    
    int main()
    {
      std::cout << ContainsAlpha("192.168.0.1:8080") << std::endl;
      std::cout << ContainsAlpha("123greetings.com") << std::endl;
      
      system("PAUSE");
    }
    Last edited by PredicateNormative; March 13th, 2009 at 05:27 AM. Reason: Removed incorrect std:: scoping before tolower

  13. #13
    Join Date
    Nov 2006
    Location
    Essen, Germany
    Posts
    1,344

    Re: determining if a string is an ip address or a hostname?

    When there&#180;s no parsing include you can assume the following:
    If it&#180;s a IPv4 address the string must consist of three dots ("."), everything else must be digits (0-9). If this rule is violated the address is either a logical host name or corrupt

    Edit:
    Way too late
    - Guido

  14. #14
    Join Date
    Apr 2001
    Posts
    1,029

    Re: determining if a string is an ip address or a hostname?

    Can you do this without making IsAlpha a class?

    Thanks

  15. #15
    Join Date
    Nov 2008
    Location
    England
    Posts
    748

    Re: determining if a string is an ip address or a hostname?

    IsAlpha() alone wont work for IPv6 addresses, the OP didn't state whether he was supporting IPv6. I think regex maybe a better solution?
    Get Microsoft Visual C++ Express here or CodeBlocks here.
    Get STLFilt here to radically improve error messages when using the STL.
    Get these two can't live without C++ libraries, BOOST here and Loki here.
    Check your code with the Comeau Compiler and FlexeLint for standards compliance and some subtle errors.
    Always use [code] code tags [/code] to make code legible and preserve indentation.
    Do not ask for help writing destructive software such as viruses, gamehacks, keyloggers and the suchlike.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured