Пример #1
0
    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);
    }
Пример #2
0
        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;
        }
Пример #3
0
    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);
    }
Пример #4
0
 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);
Пример #5
0
        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;
        }
Пример #6
0
 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);