免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12
最近访问板块 发新帖
楼主: BigMonkey
打印 上一主题 下一主题

[函数] 有没有那个函数能够检测一个字符串是不是一个合法的IP地址?? [复制链接]

论坛徽章:
0
11 [报告]
发表于 2005-09-20 11:26 |只看该作者

有没有那个函数能够检测一个字符串是不是一个合法的IP地址??

何苦用正则表达式啊?
sunlan兄台那么做不是也不错吗?

论坛徽章:
0
12 [报告]
发表于 2005-09-20 11:37 |只看该作者

有没有那个函数能够检测一个字符串是不是一个合法的IP地址??

inet_ntop

论坛徽章:
0
13 [报告]
发表于 2005-09-20 14:27 |只看该作者

有没有那个函数能够检测一个字符串是不是一个合法的IP地址??

[quote]原帖由 "homer3382"]用正则表达式可以判断[/quote 发表:


怎么用正则表达式呢?
好像有regex.h可以用,
能给个例子么?

论坛徽章:
0
14 [报告]
发表于 2005-09-20 14:27 |只看该作者

有没有那个函数能够检测一个字符串是不是一个合法的IP地址??

以下是我的代码,昨天晚上用了40多分钟才搞定
/*
//Summary: this is to test the IP_check() function which is to check the regulation of IP
//$Id: IP_check_test.c,v 1.2 2005/09/20 04:16:37  $
*/

#include <string.h>;
#include <stdio.h>;
#include <stdlib.h>;

int IP_check(char* chInput, int iLen) // return 1 is to show that chInput is a correct IP, or 0 not a IP
{
        int i=0;
        int i0=0;
        int i1=0;
        int iR=0;
        char chIP[4][5]={0};

        if (strchr(chInput, '.')==NULL)
                return 0;

        if (iLen>;15)
                return 0; //not a IP
        if (iLen<7)
                return 0;

        for(i=0; i<iLen; i++)
        {
                if (i==0 && chInput=='.')
                        return 0;

                if (chInput=='.')
                {
                        i1++;
                        iR=iR+strlen(chIP[i1-1])+1;
                        continue;
                }

                if (i1==0)
                {
                        if (i>;2)
                                return 0;
                        chIP[i1]=chInput;
                }
                else
                {
                        if ((i-iR)>;2)
                                return 0;
                        chIP[i1][i-iR]=chInput;
                }
        }

        if (i1!=3)
                return 0;

        for(i=0; i<4; i++)
        {
                if (atoi(chIP)>;=0 && atoi(chIP)<255)
                        ;
                else
                        return 0;
        }
        return 1;
}

int main(int argc, char* argv[])
{
        int ret=0;
        ret=IP_check(argv[1], strlen(argv[1]));

        if (ret==0)
                printf("not a IP\n";
        if (ret==1)
                printf("is a correct IP\n";

        return 0;
}

论坛徽章:
0
15 [报告]
发表于 2005-09-20 15:11 |只看该作者

有没有那个函数能够检测一个字符串是不是一个合法的IP地址??

if (inet_pton(AF_INET, ip, dst) ==0)
{
  invalid ip
}

论坛徽章:
0
16 [报告]
发表于 2011-10-11 20:02 |只看该作者
回复 6# sunlan


  1. static bool IsAddress( const char *ipstr )
  2. {
  3. int a1, a2, a3, a4;

  4. if (sscanf( ipstr, "%3d.%3d.%3d.%3d", &a1, &a2, &a3, &a4 ) != 4)
  5. {
  6. return( FALSE );
  7. } else {
  8. return( TRUE );
  9. }
  10. }
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP