zoukankan      html  css  js  c++  java
  • 判断字符串是否为ip

    判断字符串是否为ip

    1

    #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;

    ret = inet_pton(AF_INET, str, &addr);

    if (ret > 0)

    fprintf(stderr, "\"%s\" is a valid IPv4 address\n", str);

    else if (ret < 0)

    fprintf(stderr, "EAFNOSUPPORT: %s\n", strerror(errno));

    else

    fprintf(stderr, "\"%s\" is not a valid IPv4 address\n", str);

    return ret;

    }

    int inet_pton(int family, const char *strptr, void *addrptr);

    返回:1—成功;0—输入不是有效的表达格式,-1—出错。

    2

    #include <stdio.h>

    #include <string.h>

    int a,b,c,d;

    char t;

    char s[100];

    void main() {

    strcpy(s,"123.1.2.3.1111");

    if (4==sscanf(s,"%d.%d.%d.%d%c",&a,&b,&c,&d,&t)) {

    if (0<=a && a<=255

    && 0<=b && b<=255

    && 0<=c && c<=255

    && 0<=d && d<=255) {

    printf("[%s] is valid IPv4\n",s);

    } else {

    printf("[%s] is invalid IPv4\n",s);

    }

    } else {

    printf("[%s] is invalid IPv4\n",s);

    }

    }

    原文

    http://bbs.chinaunix.net/thread-928835-1-1.html

    http://www.cnblogs.com/riky/archive/2006/11/24/570713.aspx

    http://bbs.csdn.net/topics/330230349

  • 相关阅读:
    PSR
    php类与对象
    二进制、位运算及其用处
    安装LNMP笔记
    计算机基础
    Python3编写HFS(CVE2014-6287)检测脚本
    windows和iis对应版本关系
    phpStudy8.1.0.1配置子域名多网站
    Xml外部实体注入
    xss小游戏通关-全答案
  • 原文地址:https://www.cnblogs.com/mydomain/p/3114267.html
Copyright © 2011-2022 走看看