熱門文章

2013年2月15日

簡易的網路PING程式


簡易的網路PING程式


ping是:一個電腦網路工具,用來測試特定主機能否通過IP到達。ping的運作原理是:向目標主機傳出一個ICMPecho要求封包,等待接收echo回應封包。程式會按時間和反應成功的次數,估計失去封包率(丟包率)和封包來回時間(網路時延)(Round-trip delay time)。

1983年12月,Mike Muuss寫了這個程式,在IP網路出問題時方便找出其根源。因為這個程式的運作和潛水艇的聲納相似,他便用聲納的聲音來為程式取名。David L. Mills曾提出另一個取名:Packet Internet Grouper/Gopher(後者指地鼠)。

網路管理員之間通常把ping用作動詞,如「ping一下電腦X,看他是否開著。」




// ConsolePing.cpp: 主要專案檔。

#include "stdafx.h"

using namespace System;
using namespace System::Windows::Forms;
using namespace System::Net;
using namespace System::Net::NetworkInformation;
using namespace System::Threading;
int main(array<System::String ^> ^args)
{
Console::WriteLine(L"Console Ping App");
Console::WriteLine(L"Command PING,Ping,ping,exit");
Console::WriteLine(L"ex:Ping 192.168.1.1");
Console::WriteLine(L"ex:Ping 192.168.1.5-50");
Console::WriteLine(L"ex:Ping 192.168.1.1-all");
while (true)
{
Console::WriteLine(L"請輸入命令");
String ^strCommand = Console::ReadLine();
if (strCommand->ToUpper()->Contains("EXIT") == true)
{
Console::WriteLine(L"Exit");
break;
}
else if (strCommand->ToUpper()->Contains("PING") == true)
{
//MessageBox::Show("Command is True");
array<Char> ^delimiterChars = gcnew array<Char>{' ','.', '-'};
array<String^> ^strSplitCmd = strCommand->Split(delimiterChars);
if (strSplitCmd->Length == 5 || strSplitCmd->Length == 6)
{
if (strSplitCmd->Length == 5)
{
String ^IP = gcnew String(strSplitCmd[1]+"."+strSplitCmd[2]+"."+strSplitCmd[3]+"."+strSplitCmd[4]);
Ping ^ping = gcnew Ping();
for (int i = 0; i < 4; i++)
{
PingReply ^pingreply = ping->Send(IP);//IP Address
if (pingreply->Status.ToString() == "Success")
Console::WriteLine("IP : "+IP+" Status : "+pingreply->Status.ToString()+" Time : "+pingreply->RoundtripTime.ToString());
else
Console::WriteLine("IP : "+IP+" Status : 主機無回應");
}
}
else
{
if (strSplitCmd[5] != "all")
{
Console::WriteLine(L"區段 ip");
for (int i = Convert::ToInt32(strSplitCmd[4]); i <= Convert::ToInt32(strSplitCmd[5]); i++)
{
String ^IP = gcnew String(strSplitCmd[1]+"."+strSplitCmd[2]+"."+strSplitCmd[3]+"."+i);
Ping ^ping = gcnew Ping();
PingReply ^pingreply = ping->Send(IP);//IP Address
if (pingreply->Status.ToString() == "Success")
Console::WriteLine("IP : "+IP+" Status : "+pingreply->Status.ToString()+" Time : "+pingreply->RoundtripTime.ToString());
else
Console::WriteLine("IP : "+IP+" Status : 主機無回應");
}
}
else
{
Console::WriteLine(L"所有 ip");
for (int i = Convert::ToInt32(strSplitCmd[4]); i <= 254; i++)
{
String ^IP = gcnew String(strSplitCmd[1]+"."+strSplitCmd[2]+"."+strSplitCmd[3]+"."+i);
Ping ^ping = gcnew Ping();
PingReply ^pingreply = ping->Send(IP);//IP Address
if (pingreply->Status.ToString() == "Success")
Console::WriteLine("IP : "+IP+" Status : "+pingreply->Status.ToString()+" Time : "+pingreply->RoundtripTime.ToString());
else
Console::WriteLine("IP : "+IP+" Status : 主機無回應");
}
}
}
}
else
{
MessageBox::Show("Command is False");
Console::WriteLine(L"Console Ping App");
Console::WriteLine(L"Command PING,Ping,ping,exit");
Console::WriteLine(L"ex:Ping 192.168.1.1");
Console::WriteLine(L"ex:Ping 192.168.1.5-50");
Console::WriteLine(L"ex:Ping 192.168.1.1-all");
}
}
else
{
MessageBox::Show("Command is False");
Console::WriteLine(L"Console Ping App");
Console::WriteLine(L"Command PING,Ping,ping,exit");
Console::WriteLine(L"ex:Ping 192.168.1.1");
Console::WriteLine(L"ex:Ping 192.168.1.5-50");
Console::WriteLine(L"ex:Ping 192.168.1.1-all");
}
}
    return 0;
}

沒有留言:

張貼留言