最近在實施MSN Direct項目時,需要用VPN+ADSL方案把放在西格瑪的伺服器和電視塔上的發射裝置串連在一起,為了測試網路是否正常,需要兩邊的人進行配合,通過Ping命令判斷網路是否聯通。由於電視塔僅周二下午才允許調試,調試時間有限,並且人手也有限,為了節省時間,所以萌生了做一個Windows Mobile Ping 命令的想法,這樣通過手機就可以直接測試網路是否聯通。
Windows Mobile是沒有現成的Ping可用的,所以需要自己做一個。一種最直接的想法就是用Socket實現ICMP協議(針對PING命令的部分),對於案頭版的.Net Framework中的Socket,只要如下進行定義,就可以是實現收發ICMP資料包。
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.Icmp);
但是很不幸的是.Net compact Framework不支援ProtocolType.Icmp參數,所以也無從用Socket實現了。幸好天無絕人之路,Windows Mobile還提供了Icmp的API介面,可以通過這些API介面,實現PING命令的功能,核心代碼如下:
public class Sw
{
#region P/Invoke
[DllImport("\\windows\\iphlpapi.dll", SetLastError = true)]
static extern IntPtr IcmpCreateFile();
[DllImport("\\windows\\iphlpapi.dll", SetLastError = true)]
static extern bool IcmpCloseHandle(IntPtr handle);
[DllImport("\\windows\\iphlpapi.dll", SetLastError = true)]
static extern Int32 IcmpSendEcho(IntPtr icmpHandle, Int32 destinationAddress, String requestData, Int16 requestSize, ref IP_OPTION_INFORMATION requestOptions, ref ICMP_ECHO_REPLY replyBuffer, Int32 replySize, Int32 timeout);
[StructLayout(LayoutKind.Sequential)]
public struct IP_OPTION_INFORMATION
{
public byte TTL;
public byte TOS;
public byte Flags;
public byte OptionsSize;
public IntPtr OptionsData;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
private struct ICMP_ECHO_REPLY
{
public int Address;
public int Status;
public int RoundTripTime;
public Int16 DataSize;
public Int16 Reserved;
public IntPtr DataPtr;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 250)]
public String Data;
public IP_OPTION_INFORMATION Options;
}
#endregion
public static int ping(string ip, int timeout,out string info)
{
IPEndPoint host = new IPEndPoint(Dns.GetHostEntry(ip).AddressList[0], 0);
if (host == null | Environment.OSVersion.Platform != PlatformID.WinCE)
{
info = "ip error!";
return -1;
}
int result = -1;
IntPtr ICMPHandle;
Int32 iIP;
String sData;
IP_OPTION_INFORMATION oICMPOptions = new IP_OPTION_INFORMATION();
ICMP_ECHO_REPLY ICMPReply = new ICMP_ECHO_REPLY();
Int32 iReplies;
ICMPHandle = IcmpCreateFile();
iIP = BitConverter.ToInt32(host.Address.GetAddressBytes(), 0);
sData = "abcdefghijklmnopqrstuvwxyz012345";
oICMPOptions.TTL = 255;
iReplies = IcmpSendEcho(ICMPHandle, iIP, sData, (Int16)sData.Length, ref oICMPOptions, ref ICMPReply, Marshal.SizeOf(ICMPReply), timeout);
if (ICMPReply.Status == 0)
{
result = ICMPReply.RoundTripTime;
info = "ping bytes=" + sData.Length.ToString() + " time=" + result.ToString() + "ms TTL=" + oICMPOptions.TTL.ToString();
}
else
{
info = "ping " + ip + " timed out.";
}
IcmpCloseHandle(ICMPHandle);
return result;
}
}
最終程式運行後的介面如下:
需要注意的是,GPRS串連必須是CMNET(關於如何建立GPRS串連,有興趣的朋友可以參考我以前寫的文章:讓智能手機和居家電腦互聯互連(WM6 GPRS)),否則是無法PING通公網IP的,此外我們發現Windows Mobile手機的IP為10.x.x.x,我們知道10開頭的IP地址是A類地址,查相關資料,我們可知10.0.0.0到10.255.255.255是私人地址(所謂的私人地址就是在互連網上不使用,而被用在區域網路絡中的地址)。每個A類地址理論上可串連16777214台主機,這大約是1千六百萬台,這也是同時使用GPRS的手機數上限了。由於Windows Mobile手機分配的IP地址不是公網IP,所以很遺憾,我們不能在PC上Ping Windows Mobile手機,也就無法測試PC的Ping出命令是否正常了。[葉帆工作室]