資源簡介
C++獲取所有局域網可用IP,使用PING的方式簡單易懂,但缺點就是時間消耗比較多,給新手看的,大神請無視 vs2010編譯

代碼片段和文件信息
//?C++ping.cpp?:?定義控制臺應用程序的入口點。
//
#include?“stdafx.h“
string?inttostring(int?n)
{
std::stringstream?strRet;
strRet?< return?strRet.str();
}
bool?isIP(const?char*?str)
{
int?i(0);
size_t?len?=?strlen(str);
while?(len?>?0)
{
if?(?*str?‘0‘?||?*str>‘9‘)
{
if?(*str?!=‘.‘)
return?false;
i?++;
}
str++;
len--;
}
if?(i?==?3)
return?true;
return?false;
}
//??[9/22/2014?gin]
int?pingit(string?strStartIPstring?strEndIP)
{
try
{
int?pos1?=?strStartIP.find_last_of(“.“)?+?1;
string?str1?=?strStartIP.substr(pos1);
string?strIP1?=??strStartIP.substr(0pos1);
int?pos2?=?strEndIP.find_last_of(“.“)?+?1;
string?str2?=?strEndIP.substr(pos2);
string?strIP2?=??strEndIP.substr(0pos2);
if?(strIP1?!=?strIP2)
{
cout?<“錯誤:輸入IP不在同一段“?< return?0;
}
int?count?=?0;
int?s1?=?atoi(str1.c_str());
int?s2?=?atoi(str2.c_str());
cout?<“正在ping所有指定IP地址,這也許會持續較長時間,請耐心等候……“?< vector?vecIP;
for?(int?i?=?s1;i? {
string?ip?=?strIP1?+?inttostring(i);
string?conv?=?“ping?“?+?ip;
conv?+=?“?-n?1“;
int?q?=?system(conv.c_str());
if?(q?==?0)
{
count?++;
vecIP.push_back(ip);
}
}
cout?<“可用IP列表:“?< for?(int?i?=?0;?i? {
cout?< }
return?count;
}
catch?(...)
{
return?0;
}
}
int?_tmain(int?argc?_TCHAR*?argv[])
{
//? char?*s?=?new?char[16];
string?s1s2;
while(1)
{
cout<“請輸入需要ping的IP段:“< cout<“(格式:x.x.x.x且起始IP與結束IP前三位必須相同)“< cout<“起始IP:“?;
cin?>>?s1;
cout<“結束IP:“?;
cin?>>?s2;
if?(!isIP(s1.c_str())?||?!isIP(s2.c_str()))
{
cout?<“錯誤:IP格式有誤“?< }
else
break;
}
cout?<“共“?< system(“pause“);
return?0;
}
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件???????1958??2014-09-22?16:50??C++ping\C++ping\C++ping.cpp
?????文件???????4364??2014-09-22?15:47??C++ping\C++ping\C++ping.vcxproj
?????文件???????1311??2014-09-22?15:47??C++ping\C++ping\C++ping.vcxproj.filters
?????文件????????143??2014-09-22?15:47??C++ping\C++ping\C++ping.vcxproj.user
?????文件???????1553??2014-09-22?15:47??C++ping\C++ping\ReadMe.txt
?????文件????????212??2014-09-22?15:47??C++ping\C++ping\stdafx.cpp
?????文件????????330??2014-09-22?16:35??C++ping\C++ping\stdafx.h
?????文件????????236??2014-09-22?15:47??C++ping\C++ping\targetver.h
?????文件????????888??2014-09-22?15:47??C++ping\C++ping.sln
????..A..H.?????14848??2014-09-22?16:51??C++ping\C++ping.suo
?????文件?????101888??2014-09-22?16:50??C++ping\Debug\C++ping.exe
?????文件?????683168??2014-09-22?16:50??C++ping\Debug\C++ping.ilk
?????文件????1133568??2014-09-22?16:50??C++ping\Debug\C++ping.pdb
?????目錄??????????0??2014-09-22?16:52??C++ping\C++ping
?????目錄??????????0??2014-09-22?16:42??C++ping\Debug
?????目錄??????????0??2014-09-22?16:52??C++ping
-----------?---------??----------?-----??----
??????????????1944467????????????????????16
- 上一篇:16位 CRC 校驗代碼
- 下一篇:linux c語言寫的坦克大戰小游戲
評論
共有 條評論