确定一个字符串在C有效的IP地址 [英] Determine if a string is a valid IP address in C

查看:316
本文介绍了确定一个字符串在C有效的IP地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是确定一个字符串包含IP地址的好方法。我应该使用ISDIGIT()?

What would be a good way to determine if a string contains an IP address. Should I use isdigit()?

建议将AP preciate它。

Suggestions would be appreciate it.

感谢

推荐答案

我问<一个href=\"http://stackoverflow.com/questions/318236/how-do-you-validate-that-a-string-is-a-valid-ip-address-in-c\">a对于C ++ 类似的问题。您应该能够使用稍微修改(C)的版本是我想出了当时的情况。

I asked a similar question for C++. You should be able to use a slightly modified (for C) version of what I came up with back then.

bool isValidIpAddress(char *ipAddress)
{
    struct sockaddr_in sa;
    int result = inet_pton(AF_INET, ipAddress, &(sa.sin_addr));
    return result != 0;
}

您需要的#include&LT; ARPA / inet.h&GT; 使用的 inet_pton()函数

更新基于意见的问题:如果你想知道,如果C风格字符串的包含的IP地址,那么你应该迄今给出的两个答案结合起来。使用常规的前pression发现大致匹配的IP地址模式,然后使用功能上面来检查的比赛看它是否是真正的交易。

Update based on comments to the question: If you want to know if a C-style string contains an IP address, then you should combine the two answers given so far. Use a regular expression to find patterns that roughly match an IP address, then use the function above to check the match to see if it's the real deal.

这篇关于确定一个字符串在C有效的IP地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆