CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 20 of 20
  1. #16
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

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

    Code:
    bool ContainsAlpha(const std::string& s)
    {
      return(std::string::npos != s.find_first_not_of("0123456789.:"); 
    }
    Last edited by JohnW@Wessex; March 6th, 2009 at 09:26 AM. Reason: Incorrect code
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

  2. #17
    Join Date
    Oct 2007
    Posts
    63

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

    well,

    there are functions in ctype.h called isxdigit() and ispunct() that would come to use here.,

    Code:
     //c language code, but you get the point right?
    #include<ctype.h>
    #include<stdio.h>
    
    int check_ip(char *p)
    {
        while(*p != '\0')
        {
            if(isxdigit(*p)==0)
            {
                if(ispunct(*p)==0)
                {
                    return 0;
                }
            }
            p++;
        }
        return 1;
    }
    
    
    int main()
    {
        char ip[16]="127.0.0.1";
        char name[16]="123google.com";
        char ipport[22]="127.0.0.1:8080";
        char ipv6[50]="2001:db8:85a3:0:0:8a2e:370:7334";
    
        /*Check if the string "ip" is an ip address or not*/
         if(check_ip(ip)==1)
        {
            printf("&#37;s is ip",ip);
        }
    
        /*Check if the string "name" is an ip address or not*/
        if(check_ip(name)==1)
        {
            printf("%s is ip",name);
        }
    
        /*Check if the string "ipport" is an ip address or not*/
        if(check_ip(ipport)==1)
        {
            printf("%s is ip\n",ipport);
        }
    
         /*Check if the string "ipv6" is an ip address or not*/
         if(check_ip(ipv6)==1)
        {
            printf("%s is ip\n",ipv6);
        }
    
        return 0;
    }
    output
    Code:
    $ gcc isdigit.c
    $ ./a.out 
    127.0.0.1 is ip
    127.0.0.1:8080 is ip
    2001:db8:85a3:0:0:8a2e:370:7334 is ip
    me thinks that satisfies the OPs requirements....
    Last edited by creeping death; March 6th, 2009 at 01:57 PM.

  3. #18
    Join Date
    Apr 2004
    Posts
    102

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

    I think you're starting on the right track, but you need to do a lot more checking to verify a valid IP address: the number of fields and the numeric value in each field.

    Hints: keep track of the location of the lastDot and the currentDot, as well as the total dotCount. If you hit a colon ':' or a non-numeric before you reach 3 dots, return the failure condition. If you encounter a dot set the currentDot to the current position, then use the two to find the field width, then check the field width, then check for a substring that is an integer between 0 and 255 (or 0 and 65535 for the port number).

    Also, keep in mind that the max size of a valid IPv4 address c-string is 22 characters (including the null-terminator) i.e. "255.255.255.255:65535"

    There's probably a couple conditions I missed in there, but that's a general rundown.

  4. #19
    Join Date
    Nov 2008
    Location
    England
    Posts
    748

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

    Its also valid to append :port_number after a hostname, so that case would need to report hostname.
    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.

  5. #20
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

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

    If I was trying to solve this I would probably do it like this.

    Assuming you have a function like this...
    std::vector<std::string> Split(const std::string &text, char delimiter)

    Split the string at every ':'
    For an IP address you should have either one or two elements.

    Split the first element at every '.'
    For an IP address you should have four elements.

    Check that all elements in both vectors are integers between 0 and 255.
    EDIT: Except for the port number of course.
    Last edited by JohnW@Wessex; March 9th, 2009 at 04:28 AM.
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

Page 2 of 2 FirstFirst 12

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