判断字符串是否为合格的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 ;
}
{
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 ;
}
相关文章推荐
- 判断字符串是否为IP地址
- 判断一个字符串是否是合法IP地址
- 判断一个字符串是否为合法ip地址(ipv4)
- python实现判断一个字符串是否是合法IP地址
- 判断字符串是否为标准IP地址
- C#下判断一个字符串是否是ip地址的函数
- 判断字符串是否为IP地址。
- C判断一字符串是否是合法的IP地址
- 如何判断字符串是否是有效的的IP地址
- 密码验证合格程序(判断字符串是否有重复子串)
- 判断一个字符串是否为有效ip地址
- 面试题整理17 输入一个字符串判断一个字符串是否是有效ip地址
- 判断一个字符串的ip是否是IP合法的ip地址
- 面试题整理17 输入一个字符串判断一个字符串是否是有效ip地址
- iOS - 判断一个字符串是否是ip地址
- 如何判断一个字符串是否为正确的IP地址
- 【Python】判断一个字符串是否是合法IP地址
- 用栈判断字符串是否合格
- 判断一个字符串的ip是否是IP合法的ip地址
- 如何判断用户输入的字符串是否是IP地址