您的位置:首页 > 其它

判断字符串是否为合格的IP地址

2013-08-27 10:54 375 查看
BOOL IsNomalIP(CString strServerIP, int *endIP)

{

CString m_strIP ;

int n_strid ;

int nCount = 0 ;

int IPnumber[4] ;

CString Card ;

m_strIP = strServerIP ;

for (int i = 0 ; i < m_strIP.GetLength(); i++)

{

if ( m_strIP.GetAt(i) == '.')

{

nCount++ ;

}

}

if ( nCount != 3)

{

return 0 ;

}

else

{

int end = m_strIP.ReverseFind('.') ;

CString strendIP = m_strIP.Right(m_strIP.GetLength() - end - 1) ;

if ( strendIP == _T("0") || strendIP == _T("255"))

{

*endIP = 1 ;

}

nCount = -1 ;

}

while( 1 )

{

if ( m_strIP == _T(""))

{

break ;

}

n_strid = m_strIP.Find('.') ;

if ( n_strid != -1)

{

nCount++ ;

if ( nCount > 2)

{

return 0 ;

}

Card = m_strIP.Left( n_strid ) ;

if ( Card == _T(""))

{

return 0 ;

}

for (int i = 0 ; i < Card.GetLength(); i++)

{

if ( Card.GetAt(i) < '0' || Card.GetAt(i) > '9' || (Card.GetAt(0) == '0'&& Card.GetLength() > 1))

{

return 0 ;

}

}

IPnumber[nCount] = _ttoi(Card) ;

m_strIP = m_strIP.Right(m_strIP.GetLength() - n_strid -1) ;

if ( m_strIP == _T(""))

{

return 0 ;

}

}

else

{

nCount++ ;

for (int i = 0 ; i < m_strIP.GetLength(); i++)

{

if ( m_strIP.GetAt(i) < '0' || m_strIP.GetAt(i) > '9' || (m_strIP.GetAt(0) == '0'&& m_strIP.GetLength() > 1))

{

return 0 ;

}

}

IPnumber[nCount] = _ttoi(m_strIP) ;

m_strIP = _T("") ;

/*break ;*/

}

if ( IPnumber[nCount] <0 || IPnumber[nCount] > 255)

{

return 0 ;

}

}

if ( nCount != 3)

{

return 0 ;

}

return 1 ;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: