public int Ping(string host, int timeout) { uint addr = BitConverter.ToUInt32(IPAddress.Parse(host).GetAddressBytes(), 0); int req = 123456789; ICMP_ECHO_REPLY rep = new ICMP_ECHO_REPLY(); try { IntPtr h = IcmpCreateFile(); uint retval = IcmpSendEcho(h, addr, ref req, 4, IntPtr.Zero, ref rep, 32, timeout*1000); if (retval == 0) { Debug.Log("Error sending ping"); } IcmpCloseHandle(h); } catch (Exception e) { Debug.Log("Error doing ping: " + e.ToString()); return -1; } return (int)rep.RoundTripTime; }
/// <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); }
public static int Ping(IPAddress ip) { in_addr destinationAddress = new in_addr((ip ?? IPAddress.Parse("0.0.0.0")).ToString()); IntPtr icmpHandle = IcmpCreateFile(); byte[] requestData = new byte[] { 0 }; byte[] replyByte = new byte[1000]; uint result = IcmpSendEcho(icmpHandle, destinationAddress, requestData, (ushort)requestData.Length, IntPtr.Zero, replyByte, (uint)replyByte.Length, 1000); if (result > 0) { GCHandle handle = GCHandle.Alloc(replyByte, GCHandleType.Pinned); ICMP_ECHO_REPLY reply = (ICMP_ECHO_REPLY)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(ICMP_ECHO_REPLY)); return((int)reply.RoundTripTime); } ERROR error = (ERROR)GetLastError(); return(-1); }
public int Ping(string host, int timeout) { uint addr = BitConverter.ToUInt32(IPAddress.Parse(host).GetAddressBytes(), 0); int req = 123456789; ICMP_ECHO_REPLY rep = new ICMP_ECHO_REPLY(); try { IntPtr h = IcmpCreateFile(); uint retval = IcmpSendEcho(h, addr, ref req, 4, IntPtr.Zero, ref rep, 32, timeout * 1000); if (retval == 0) { Debug.Log("Error sending ping"); } IcmpCloseHandle(h); } catch (Exception e) { Debug.Log("Error doing ping: " + e.ToString()); return(-1); } return((int)rep.RoundTripTime); }
public static int Ping(IPAddress IP) { IntPtr ICMPHandle; Int32 iIP; String sData; ICMP_OPTIONS oICMPOptions = new ICMP_OPTIONS(); ICMP_ECHO_REPLY ICMPReply = new ICMP_ECHO_REPLY(); Int32 iReplies; ICMPHandle = IcmpCreateFile(); iIP = BitConverter.ToInt32(IP.GetAddressBytes(), 0); sData = "x"; oICMPOptions.Ttl = 255; iReplies = IcmpSendEcho(ICMPHandle, iIP, sData, sData.Length, ref oICMPOptions, ref ICMPReply, Marshal.SizeOf(ICMPReply), 30); IcmpCloseHandle(ICMPHandle); return(iReplies); }
public static int Ping(IPAddress IP) { IntPtr ICMPHandle; Int32 iIP; String sData; ICMP_OPTIONS oICMPOptions = new ICMP_OPTIONS(); ICMP_ECHO_REPLY ICMPReply = new ICMP_ECHO_REPLY(); Int32 iReplies; ICMPHandle = IcmpCreateFile(); iIP = BitConverter.ToInt32(IP.GetAddressBytes(), 0); sData = "x"; oICMPOptions.Ttl = 255; iReplies = IcmpSendEcho(ICMPHandle, iIP, sData, sData.Length, ref oICMPOptions, ref ICMPReply, Marshal.SizeOf(ICMPReply), 30); IcmpCloseHandle(ICMPHandle); return iReplies; }
public static int PingQdata(ref queueData qData) { IntPtr ICMPHandle; Int32 iIP; String sData; ICMP_OPTIONS oICMPOptions = new ICMP_OPTIONS(); ICMP_ECHO_REPLY ICMPReply = new ICMP_ECHO_REPLY(); Int32 iReplies; int[] roundTrips = new int[qData.iPingCount]; int averageRoundTrip = 0; //reset ping reply count qData.iPingReplies = 0; /* * qData.iPingTimeout = (byte)qData.iPingTimeout; * qData.sIP = IP.ToString(); * qData.IP = IP; */ ICMPHandle = IcmpCreateFile(); iIP = BitConverter.ToInt32(qData.IP.GetAddressBytes(), 0); sData = "x"; oICMPOptions.Ttl = (byte)qData.iPingTimeout; //time to live for (int i = 0; i < qData.iPingCount; i++) { iReplies = IcmpSendEcho(ICMPHandle, iIP, sData, sData.Length, ref oICMPOptions, ref ICMPReply, Marshal.SizeOf(ICMPReply), 30); if (iReplies == 1) { roundTrips[i] = ICMPReply.RoundTripTime; qData.iPingReplies++; } else { roundTrips[i] = -1; } } //calc average int counts = 0; for (int i = 0; i < qData.iPingCount; i++) { if (roundTrips[i] != -1) { averageRoundTrip += roundTrips[i]; counts++; } } if (counts > 0) { averageRoundTrip = averageRoundTrip / counts; } else { averageRoundTrip = 0; } qData.iPingReplyTime = averageRoundTrip; IcmpCloseHandle(ICMPHandle); return(qData.iPingReplies); }
private static extern Int32 IcmpSendEcho(IntPtr icmpHandle, Int32 destinationAddress, String requestData, Int32 requestSize, ref ICMP_OPTIONS requestOptions, ref ICMP_ECHO_REPLY replyBuffer, Int32 replySize, Int32 timeout);
private static extern uint IcmpSendEcho(IntPtr icmpHandle, uint ipAddr, ref int requestData, ushort requestSize, IntPtr optionInfo, ref ICMP_ECHO_REPLY replyBuffer, uint replySize, int timeout);
public static extern uint IcmpSendEcho(IntPtr IcmpHandle, in_addr DestinationAddress, byte[] RequestData, ushort RequestSize, IntPtr pRequestOptions, ref ICMP_ECHO_REPLY ReplyBuffer, uint ReplySize, uint Timeout);
public static int PingQdata(ref queueData qData) { IntPtr ICMPHandle; Int32 iIP; String sData; ICMP_OPTIONS oICMPOptions = new ICMP_OPTIONS(); ICMP_ECHO_REPLY ICMPReply = new ICMP_ECHO_REPLY(); Int32 iReplies; int[] roundTrips= new int[qData.iPingCount]; int averageRoundTrip=0; //reset ping reply count qData.iPingReplies = 0; /* qData.iPingTimeout = (byte)qData.iPingTimeout; qData.sIP = IP.ToString(); qData.IP = IP; */ ICMPHandle = IcmpCreateFile(); iIP = BitConverter.ToInt32(qData.IP.GetAddressBytes(), 0); sData = "x"; oICMPOptions.Ttl = (byte) qData.iPingTimeout; //time to live for(int i=0; i<qData.iPingCount; i++){ iReplies = IcmpSendEcho(ICMPHandle, iIP, sData, sData.Length, ref oICMPOptions, ref ICMPReply, Marshal.SizeOf(ICMPReply), 30); if (iReplies == 1) { roundTrips[i] = ICMPReply.RoundTripTime; qData.iPingReplies++; } else roundTrips[i] = -1; } //calc average int counts = 0; for (int i = 0; i < qData.iPingCount; i++) { if (roundTrips[i] != -1) { averageRoundTrip += roundTrips[i]; counts++; } } if(counts>0) averageRoundTrip = averageRoundTrip / counts; else averageRoundTrip = 0; qData.iPingReplyTime = averageRoundTrip; IcmpCloseHandle(ICMPHandle); return qData.iPingReplies; }