免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
123下一页
最近访问板块 发新帖
查看: 30210 | 回复: 26
打印 上一主题 下一主题

[C] C语言如何判断输入的字符串是否为IP地址! [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-04-26 11:19 |只看该作者 |倒序浏览
如题!~

论坛徽章:
95
程序设计版块每日发帖之星
日期:2015-09-05 06:20:00程序设计版块每日发帖之星
日期:2015-09-17 06:20:00程序设计版块每日发帖之星
日期:2015-09-18 06:20:002015亚冠之阿尔艾因
日期:2015-09-18 10:35:08月度论坛发贴之星
日期:2015-09-30 22:25:002015亚冠之阿尔沙巴布
日期:2015-10-03 08:57:39程序设计版块每日发帖之星
日期:2015-10-05 06:20:00每日论坛发贴之星
日期:2015-10-05 06:20:002015年亚冠纪念徽章
日期:2015-10-06 10:06:482015亚冠之塔什干棉农
日期:2015-10-19 19:43:35程序设计版块每日发帖之星
日期:2015-10-21 06:20:00每日论坛发贴之星
日期:2015-09-14 06:20:00
2 [报告]
发表于 2007-04-26 11:30 |只看该作者
根据 IP 地址的规则判断。

论坛徽章:
0
3 [报告]
发表于 2007-04-26 11:45 |只看该作者
以下是一个判断输入字符串是否是IPv4地址的例子,判断是否为IPv6地址的方法你自己钻研吧。

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>

int if_a_string_is_a_valid_ipv4_address(const char *str)
{
    struct in_addr addr;
    int ret;
    volatile int local_errno;

    errno = 0;
    ret = inet_pton(AF_INET, str, &addr);
    local_errno = errno;
    if (ret > 0)
        fprintf(stderr, "\"%s\" is a valid IPv4 address\n", str);
    else if (ret < 0)
        fprintf(stderr, "EAFNOSUPPORT: %s\n", strerror(local_errno));
    else
        fprintf(stderr, "\"%s\" is not a valid IPv4 address\n", str);

    return ret;
}

论坛徽章:
0
4 [报告]
发表于 2007-04-26 11:46 |只看该作者

  1. #include <stdio.h>
  2. #include <string.h>

  3. void check_ip(char *str)
  4. {
  5.         int i=0,count = 0;;

  6.         for ( ; i < strlen(str) ; i++ ) {
  7.                 if ( !(str[i] >= '0' && str[i] <= '9' )) {
  8.                         if ( str[i] == '.' ) {
  9.                                 count++;
  10.                                 continue;
  11.                         }
  12.                         printf("[-] Not IP.\n");
  13.                         exit(0);
  14.                 }
  15.         }

  16.         if (count == 3 )
  17.                 printf("[+] OK.\n");
  18.                 else
  19.                                 printf("[-] NOT IP.\n");

  20. }

  21. int main(void)
  22. {
  23.         char *s="127.0.0.1.";

  24.         check_ip(s);

  25.         return 0;
  26. }
复制代码

[ 本帖最后由 W.Z.T 于 2007-4-26 11:54 编辑 ]

论坛徽章:
0
5 [报告]
发表于 2007-04-26 12:20 |只看该作者
使用inet_pton判断

论坛徽章:
3
2015年迎新春徽章
日期:2015-03-04 09:56:11数据库技术版块每日发帖之星
日期:2016-08-03 06:20:00数据库技术版块每日发帖之星
日期:2016-08-04 06:20:00
6 [报告]
发表于 2007-04-26 12:22 |只看该作者
那么麻烦做什么?
直接inet_pton判断不就得了?

论坛徽章:
0
7 [报告]
发表于 2007-04-26 13:26 |只看该作者
原帖由 cjaizss 于 2007-4-26 12:22 发表
那么麻烦做什么?
直接inet_pton判断不就得了?


还得加头文件

论坛徽章:
0
8 [报告]
发表于 2007-04-26 14:16 |只看该作者
谢谢!~~

论坛徽章:
0
9 [报告]
发表于 2007-04-26 15:52 |只看该作者
正则表达式

论坛徽章:
0
10 [报告]
发表于 2007-04-26 16:39 |只看该作者
可以用正则表达式判断

当然int_pton也很简单
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP