熱門文章

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;
}

2013年2月10日

C++/CLI With OpenCV

OpenCV過去多半是在C/C++環境下運行,當使用.Net Framework的CLR語言想使用OpenCV將透過第三方編譯的Emgu CV函式庫來做匯入編譯。
C++/CLI這時就有其優勢可同時使用managed與unmanaged寫法,首先先到OpenCV官方下載最新版的OpenCV Lib 以及Intel TBB

解壓縮OpenCV-2.4.3.exe後將會產生一個opencv資料夾,裡面將會使用到
  1. opencv\build\include
  2. opencv\build\x86\vc10\lib
  3. opencv\build\x86\vc10\bin
解壓縮tbb41_20130116oss_win.zip後將會產生一個tbb41_20130116oss資料夾,裡面將會使用到
  1. C:\Users\Henry\Desktop\tbb41_20130116oss\lib\ia32\vc11\tbb_debug.lib
  2. ※vc11 for Visual Studio2012 根據自己IDE版本選擇
開啟一個新的CLR主控台專案,並把opencv資料夾內的include與lib資料夾複製到專案資料夾內(同cpp與h檔同位置)。
再把tbb41_20130116oss內的tbb_debug.lib放置專案內剛複製的lib資料夾。

在Include目錄及程式庫目錄增加上面路徑之目錄



在程式內載入兩個.H檔
#include "include\opencv\cv.h"
#include "include\opencv\highgui.h"

在連結器部分其他相依性加入

opencv_calib3d243.lib
opencv_contrib243.lib
opencv_core243.lib
opencv_features2d243.lib
opencv_imgproc243.lib
opencv_flann243.lib
opencv_highgui243.lib
opencv_objdetect243.lib
tbb_debug.lib



程式部分輸入

#include "stdafx.h"
#include "include\opencv\cv.h"
#include "include\opencv\highgui.h"
using namespace System;

int main(array<System::String ^> ^args)
{
    Console::WriteLine(L"Hello World");
IplImage *Image = cvLoadImage("img_1357809933.jpg");
cvNamedWindow("Show Image",CV_WINDOW_AUTOSIZE);
cvShowImage("Show Image",Image);
cvWaitKey(0);
cvReleaseImage(&Image);
cvDestroyWindow("Show Image");
    return 0;
}

並把opencv\build\x86\vc10\bin內所有DLL檔案複製到Debug或是Release資料夾內
也把檔名"img_1357809933.jpg"的圖片放置專案資料夾內
  1. 如果是從Visual Studio直接編譯執行圖片請放置專案名稱資料夾(同cpp與h檔同位置)
  2. 如果從編譯完的執行檔執行請放到與執行檔同資料夾
執行結果如下
(照片為網路轉載)