怎样展开IP地址范围?100分!

happyhuang 2003-10-09 01:48:17
应该是很常用的功能

比如
输入 202.202.12.*
输出 202.202.12.1 202.202.12.2 202.202.12.3 ... 202.202.12.255

输入 202.202.*.*
输出 202.202.1.1 202.202.1.2 202.202.1.3 ... 202.202.255.255

输入 202.202.**
输出 语法错误

输入 202.202.12.7-12
输出 202.202.12.7 202.202.12.8 202.202.12.9 202.202.12.10 202.202.12.11 202.202.12.12

思路可能简单,但是实现很繁琐

应该有人做过,希望有代码,谢谢!
...全文
160 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
happyhuang 2003-10-13
  • 打赏
  • 举报
回复
我用hing的代码已经调试通过了,非常感谢大家
Mark
shootingstars 2003-10-11
  • 打赏
  • 举报
回复
我的上面那个写错了,这是新写的,经过测试
#include <iostream.h>
#include <string.h>
#include <Winsock2.h>

void main(){
char str[30];
unsigned int starNum=0;
int dotNum=0;
cin>>str;
for(int i=0;i<strlen(str);i++){
if(str[i]=='*'){
starNum++;
str[i]='0';
}
if(str[i]=='.')
dotNum++;
}
unsigned long IPAddress;
if( (IPAddress=inet_addr(str)) == 0 || dotNum!=3){
cout<<"error IP Address"<<endl;
return;
}

unsigned char *p = (unsigned char *)&IPAddress;
int i1,i2,i3,i4,j1,j2,j3,j4;
for(i1=0;i1<starNum;i1++)
for(j1=0;j1<256;j1++)
{
p[3] = j1;
struct in_addr IP;
IP.S_un.S_addr = IPAddress;
cout<<inet_ntoa(IP)<<endl;
for(i2=0;i2<starNum-1;i2++)
for(j2=0;j2<256;j2++)

{
p[2] = j2;
struct in_addr IP;
IP.S_un.S_addr = IPAddress;
cout<<inet_ntoa(IP)<<endl;

for(i3=0;i3<starNum-2;i3++)
for(j3=0;j3<256;j3++)

{
p[1] = j3;
struct in_addr IP;
IP.S_un.S_addr = IPAddress;
cout<<inet_ntoa(IP)<<endl;

for(i4=0;i4<starNum-3;i4++)
for(j4=0;j4<256;j4++)

{
p[0] = j4;
struct in_addr IP;
IP.S_un.S_addr = IPAddress;
cout<<inet_ntoa(IP)<<endl;
}
}
}

}



}


hing 2003-10-11
  • 打赏
  • 举报
回复
上面是202.202.12.*的情况,下面是202.202.12.7-12的情况
判断合法性:
/////////////////////////////////////////////////////
//
// 判断是否是合法的 - 符号
//
// legal: (x.x.x.min-max)
// 202.204.5.202-205
// illegal:

// 202.204.-.5 situation A
// 202.204.202-205.5

// 202.204.5.202- situation B
// 202.204.5.-202 situation D
// 202.204.5.205-202 situation C
// 202.204.5.205-2023 situation E
//
BOOL IPHasHyphen(CString strIP)
{
CString replaceMe = strIP,
strEnd = _T("");

// situation B
if ('-' == strIP.GetAt(strIP.GetLength()-1) ) return FALSE;
if (1 != replaceMe.Replace('-', '1')) return FALSE;

int i = 0,
j = 0;
char ch = 0;

// situation A
for (i=0; i<strIP.GetLength()&&j<3; i++)
{
ch = strIP.GetAt(i);

// for 202.204.5.
if (!( (ch >= '0' && ch <= '9')||('.' == ch) )) return FALSE;

if ('.' == ch) j++;
}

// situation C
CString str1, str2;
int nHyphen, n1, n2;

strEnd = strIP.Right(strIP.GetLength() - i);

nHyphen = strEnd.Find('-');
if (0 == nHyphen) return FALSE; // situation D

str1 = strEnd.Left(nHyphen);
str2 = strEnd.Right(strEnd.GetLength() - nHyphen - 1);
n1 = StrToInt(str1);
n2 = StrToInt(str2);

// situation E
if ( (n1 < 0) || (n1 > 255) || (n2 < 0) || (n2 > 255) ) return FALSE;

if (n2 < n1) return FALSE;
else return TRUE;
}

判断是否在地址范围
{
// 判断前面部分 202.204.5
i = strRangeIP.ReverseFind('.'); if (-1 == i) return FALSE;

str1 = strIP.Left(i);
str2 = strRangeIP.Left(i);

if (str1 != str2) return FALSE;

// 判断后面部分 199-204

// 1. 得到 range 的数值
CString strEnd;
int nHyphen, n1, n2, n3;

strEnd = strRangeIP.Right(strRangeIP.GetLength() - i - 1);

nHyphen = strEnd.Find('-');
if (0 == nHyphen) return FALSE; // 202.204.5.-204 (应该已经过滤掉了)

str1 = strEnd.Left(nHyphen);
str2 = strEnd.Right(strEnd.GetLength() - nHyphen - 1);
n1 = StrToInt(str1); // 199
n2 = StrToInt(str2); // 204

// situation E
if ( (n1 < 0) || (n1 > 255) || (n2 < 0) || (n2 > 255) ) return FALSE;
if (n2 < n1) return FALSE; // 这两行判断的也应该已经过滤掉了

// 2. 得到 strIP 的数值
strEnd = strIP.Right(strIP.GetLength() - i - 1);
n3 = StrToInt(strEnd);

if ( (n3 < n1) || (n3 > n2) ) return FALSE;
else return TRUE;
}
else return FALSE;

hing 2003-10-11
  • 打赏
  • 举报
回复
刚好有这样一个代码,很简单
可以满足 1.1.1.* 到 *.*.*.* 的情况

//
// 不合法的 strRangeIP
// 202.204.*. situation A
// 202.204.*.203 situation A // 不支持这种情况
// 202.204.* situation C
// 202.204..* situation B
{
// situation A
if ( '*' != strRangeIP.GetAt(strRangeIP.GetLength()-1) )
return FALSE;

// situation B
CString replaceMe = strRangeIP;
replaceMe.Replace('*', '1');
if (INADDR_NONE == inet_addr(replaceMe)) return FALSE;

// situation C
if ( 3 == replaceMe.Replace('.','a') ) return FALSE;

return TRUE;
}

// 判断
{
i = strRangeIP.Find('*');
if (-1 == i) return FALSE; // 没有 * 的情况
if (0 == i) return TRUE; // *.*.*.* 的情况

str1 = strIP.Left(i);
str2 = strRangeIP.Left(i);

if (str1 == str2) return TRUE;
else return FALSE;
}
shootingstars 2003-10-11
  • 打赏
  • 举报
回复
输入串为str
伪代码:
// 判断是否正确的IP地址
将str中的*替换为0
if( IPAddress=inet_addr(str) == INADDR_NONE) return false

// 循环输出
char *p = (char*)&IPAddress;
for(i=0;i<*的个数;i++){
for(j=0;j<256;j++){
p[3-i] = j;
输出inet_ntoa(IPAddress);
}
}
Realdodo 2003-10-11
  • 打赏
  • 举报
回复
嘿嘿,无私的奉献代码啊
给楼主一个我最近写的CIpRange类,所有的功能封装在类中,附有详尽注释及用法说明,并且已经修改的完全符合楼主的要求了。另外还用这个类写了一个基于控制台的示例程序,要的话就来下载啊!

ftp://vc:vc@218.197.121.40/IP解析程序/testParseIp.rar
studyingpersons 2003-10-10
  • 打赏
  • 举报
回复
up
studyingpersons 2003-10-10
  • 打赏
  • 举报
回复
mark
happyhuang 2003-10-09
  • 打赏
  • 举报
回复
可以,随便,只要能出来
xyfsky 2003-10-09
  • 打赏
  • 举报
回复
是以什么类型存储的,可不可以转换成字符类型?

18,356

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 网络编程
c++c语言开发语言 技术论坛(原bbs)
社区管理员
  • 网络编程
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧