/// <summary> /// Pingar um endereco IP. /// </summary> /// <param name="ip">IP no formato xxx.xxx.xxx.xxx</param> /// <param name="timeout">Timeout para efetuar o Ping</param> /// <param name="info">Informacao retornada no formato DOS</param> /// <returns>Tempo em milisegundos</returns> public static int Ping(string ip, int timeout, out string info) { IPEndPoint host = null; 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 && iReplies > 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); }
internal static partial uint Icmp6SendEcho2(SafeCloseIcmpHandle icmpHandle, SafeWaitHandle Event, IntPtr apcRoutine, IntPtr apcContext, byte[] sourceSocketAddress, byte[] destSocketAddress, SafeLocalAllocHandle data, ushort dataSize, ref IP_OPTION_INFORMATION options, SafeLocalAllocHandle replyBuffer, uint replySize, uint timeout);
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);
public static extern uint IcmpSendEcho(IntPtr IcmpHandle, in_addr DestinationAddress, byte[] RequestData, ushort RequestSize, ref IP_OPTION_INFORMATION RequestOptions, ref ICMP_ECHO_REPLY ReplyBuffer, uint ReplySize, uint Timeout);