Exemplo n.º 1
0
        private void OnPingCompleted(PingResponse response)
        {
            PingCompletedEventHandler temp = PingCompleted;

            if (temp != null)
            {
                temp(this, new PingCompletedEventArgs(response, DateTime.Now));
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Attempts to ping a host.
        /// </summary>
        /// <param name="serverEndPoint">EndPoint to ping</param>
        /// <param name="pingCount">Ping count</param>
        /// <returns>Ping results</returns>
        public PingResponse PingHost(IPEndPoint serverEndPoint, int pingCount)
        {
            cancel = false;

            PingResponse response = new PingResponse();

            try
            {
                //Initialize a Socket of the Type ICMP
                Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.Icmp);
                socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendTimeout, this.pingTimeout);

                // Set the receiving endpoint to the client machine
                IPHostEntry clientHostEntry = Dns.GetHostEntry(Dns.GetHostName());
                EndPoint    clientEndPoint  = (new IPEndPoint(clientHostEntry.AddressList[0], 0));

                //Create icmp packet
                IcmpPacket packet = new IcmpPacket();

                // Convert icmp packet to byte array to send over socket
                byte[] sendBuffer = packet.ToByteArray();
                if (sendBuffer == null)
                {
                    OnPingError(PingResponseType.InternalError, "Could not copy ICMP packet to byte array");

                    response.PingResult   = PingResponseType.InternalError;
                    response.ErrorMessage = "Could not copy ICMP packet to byte array";
                }
                else
                {
                    response = SendPackets(socket, clientEndPoint, serverEndPoint, sendBuffer, pingCount);
                }
            }
            catch (Exception ex)
            {
                OnPingError(PingResponseType.InternalError, ex.Message);

                response.PingResult   = PingResponseType.InternalError;
                response.ErrorMessage = ex.Message;
            }

            return(response);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Attempts to ping a host.
        /// </summary>
        /// <param name="hostname">Host to ping</param>
        /// <param name="pingCount">Ping count</param>
        /// <returns>Ping results</returns>
        public PingResponse PingHost(string hostname, int pingCount)
        {
            IPHostEntry host = NetworkUtilities.ResolveHost(hostname);

            if (host == null)
            {
                OnPingError(PingResponseType.CouldNotResolveHost, "Could not resolve the host '" + hostname + "'");

                PingResponse response = new PingResponse();
                response.PingResult   = PingResponseType.CouldNotResolveHost;
                response.ErrorMessage = "Could not resolve the host '" + hostname + "'";

                return(response);
            }
            else
            {
                return(PingHost(new IPEndPoint(host.AddressList[0], 0), pingCount));
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Attempts to ping a host.
        /// </summary>
        /// <param name="serverAddress">IPAddress to ping</param>
        /// <param name="pingCount">Ping count</param>
        /// <returns>Ping results</returns>
        public PingResponse PingHost(IPAddress serverAddress, int pingCount)
        {
            PingResponse response = null;

            try
            {
                // Convert the server IPAddress to an IPEndPoint
                response = PingHost(new IPEndPoint(serverAddress, 0), pingCount);
            }
            catch (Exception ex)
            {
                OnPingError(PingResponseType.InternalError, ex.Message);

                response              = new PingResponse();
                response.PingResult   = PingResponseType.InternalError;
                response.ErrorMessage = ex.Message;
            }

            return(response);
        }
 public PingCompletedEventArgs(PingResponse pingResponse, DateTime endDateTime)
 {
     this.pingResponse = pingResponse;
     this.endDateTime = endDateTime;
 }
Exemplo n.º 6
0
        private PingResponse SendPackets(Socket socket, EndPoint client, EndPoint server, byte[] sendBuffer, int pingCount)
        {
            //Initialize PingResponse object
            PingResponse     response = new PingResponse(pingCount);
            PingResponseType result   = PingResponseType.Ok;

            byte[] receiveBuffer = new Byte[256];
            int    byteCount     = 0;
            int    start         = 0;
            int    stop          = 0;

            response.PacketsReceived = 0;
            response.PacketsSent     = 0;

            response.ServerEndPoint = (IPEndPoint)server;
            response.ClientEndPoint = (IPEndPoint)client;

            try
            {
                OnPingStarted((IPEndPoint)server, IcmpPacket.ICMP_PACKET_SIZE);

                for (int i = 0; i < pingCount; i++)
                {
                    if (cancel)
                    {
                        response.PingResult = PingResponseType.Canceled;
                        break;
                    }

                    // Initialize the buffers. The receive buffer is the size of the
                    // ICMP header plus the IP header (20 bytes)
                    receiveBuffer = new Byte[256];
                    byteCount     = 0;
                    response.PacketsSent++;

                    try
                    {
                        // Start timing
                        start = Environment.TickCount;

                        //send the Packet over the socket
                        byteCount = socket.SendTo(sendBuffer, IcmpPacket.ICMP_PACKET_SIZE, SocketFlags.None, server);

                        if (byteCount == SOCKET_ERROR)
                        {
                            result = PingResponseType.ConnectionError;
                            response.ResponseTimes[i] = Constants.InvalidInt;
                        }
                        else
                        {
                            //ReceiveFrom will block while waiting for data
                            byteCount = socket.ReceiveFrom(receiveBuffer, 256, SocketFlags.None, ref client);

                            // stop timing
                            stop = System.Environment.TickCount;

                            if (byteCount == SOCKET_ERROR)
                            {
                                result = PingResponseType.ConnectionError;
                                response.ResponseTimes[i] = Constants.InvalidInt;
                            }
                            else if ((stop - start) > pingTimeout)
                            {
                                result = PingResponseType.RequestTimedOut;
                                response.ResponseTimes[i] = Constants.InvalidInt;
                            }
                            else if (byteCount > 0)
                            {
                                //Record time
                                response.ResponseTimes[i] = stop - start;
                                response.PacketsReceived++;
                            }
                        }

                        OnPingResponse(((IPEndPoint)server).Address, result, response.ResponseTimes[i], byteCount, ref cancel);
                    }
                    catch (Exception ex)
                    {
                        OnPingError(PingResponseType.InternalError, ex.Message);
                    }
                }
            }
            finally
            {
                //close the socket
                socket.Close();

                OnPingCompleted(response);
            }

            response.PingResult = result;
            return(response);
        }
 public PingCompletedEventArgs(PingResponse pingResponse, DateTime endDateTime)
 {
     this.pingResponse = pingResponse;
     this.endDateTime  = endDateTime;
 }
        private PingResponse SendPackets(Socket socket, EndPoint client, EndPoint server, byte[] sendBuffer, int pingCount)
        {
            //Initialize PingResponse object
            PingResponse response = new PingResponse(pingCount);
            PingResponseType result = PingResponseType.Ok;

            byte[] receiveBuffer = new Byte[256];
            int byteCount = 0;
            int start = 0;
            int stop = 0;
            response.PacketsReceived = 0;
            response.PacketsSent = 0;

            response.ServerEndPoint = (IPEndPoint)server;
            response.ClientEndPoint = (IPEndPoint)client;

            try
            {
                OnPingStarted((IPEndPoint)server, IcmpPacket.ICMP_PACKET_SIZE);

                for (int i = 0; i < pingCount; i++)
                {
                    if (cancel)
                    {
                        response.PingResult = PingResponseType.Canceled;
                        break;
                    }

                    // Initialize the buffers. The receive buffer is the size of the
                    // ICMP header plus the IP header (20 bytes)
                    receiveBuffer = new Byte[256];
                    byteCount = 0;
                    response.PacketsSent++;

                    try
                    {
                        // Start timing
                        start = Environment.TickCount;

                        //send the Packet over the socket
                        byteCount = socket.SendTo(sendBuffer, IcmpPacket.ICMP_PACKET_SIZE, SocketFlags.None, server);

                        if (byteCount == SOCKET_ERROR)
                        {
                            result = PingResponseType.ConnectionError;
                            response.ResponseTimes[i] = Constants.InvalidInt;
                        }
                        else
                        {
                            //ReceiveFrom will block while waiting for data
                            byteCount = socket.ReceiveFrom(receiveBuffer, 256, SocketFlags.None, ref client);

                            // stop timing
                            stop = System.Environment.TickCount;

                            if (byteCount == SOCKET_ERROR)
                            {
                                result = PingResponseType.ConnectionError;
                                response.ResponseTimes[i] = Constants.InvalidInt;
                            }
                            else if ((stop - start) > pingTimeout)
                            {
                                result = PingResponseType.RequestTimedOut;
                                response.ResponseTimes[i] = Constants.InvalidInt;
                            }
                            else if (byteCount > 0)
                            {
                                //Record time
                                response.ResponseTimes[i] = stop - start;
                                response.PacketsReceived++;
                            }
                        }

                        OnPingResponse(((IPEndPoint)server).Address, result, response.ResponseTimes[i], byteCount, ref cancel);
                    }
                    catch (Exception ex)
                    {
                        OnPingError(PingResponseType.InternalError, ex.Message);
                    }
                }
            }
            finally
            {
                //close the socket
                socket.Close();

                OnPingCompleted(response);
            }

            response.PingResult = result;
            return response;
        }
        private void OnPingCompleted(PingResponse response)
        {
            PingCompletedEventHandler temp = PingCompleted;

            if (temp != null)
            {
                temp(this, new PingCompletedEventArgs(response, DateTime.Now));
            }
        }
Exemplo n.º 10
0
        /// <summary>
        /// Attempts to ping a host.
        /// </summary>
        /// <param name="serverEndPoint">EndPoint to ping</param>
        /// <param name="pingCount">Ping count</param>
        /// <returns>Ping results</returns>
        public PingResponse PingHost(IPEndPoint serverEndPoint, int pingCount)
        {
            cancel = false;

            PingResponse response = new PingResponse();

            try
            {
                //Initialize a Socket of the Type ICMP
                Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.Icmp);
                socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendTimeout, this.pingTimeout);

                // Set the receiving endpoint to the client machine
                IPHostEntry clientHostEntry = Dns.GetHostEntry(Dns.GetHostName());
                EndPoint clientEndPoint = (new IPEndPoint(clientHostEntry.AddressList[0], 0));

                //Create icmp packet
                IcmpPacket packet = new IcmpPacket();

                // Convert icmp packet to byte array to send over socket
                byte[] sendBuffer = packet.ToByteArray();
                if (sendBuffer == null)
                {
                    OnPingError(PingResponseType.InternalError, "Could not copy ICMP packet to byte array");

                    response.PingResult = PingResponseType.InternalError;
                    response.ErrorMessage = "Could not copy ICMP packet to byte array";
                }
                else
                {
                    response = SendPackets(socket, clientEndPoint, serverEndPoint, sendBuffer, pingCount);
                }
            }
            catch (Exception ex)
            {
                OnPingError(PingResponseType.InternalError, ex.Message);

                response.PingResult = PingResponseType.InternalError;
                response.ErrorMessage = ex.Message;
            }

            return response;
        }
Exemplo n.º 11
0
        /// <summary>
        /// Attempts to ping a host.
        /// </summary>
        /// <param name="serverAddress">IPAddress to ping</param>
        /// <param name="pingCount">Ping count</param>
        /// <returns>Ping results</returns>
        public PingResponse PingHost(IPAddress serverAddress, int pingCount)
        {
            PingResponse response = null;

            try
            {
                // Convert the server IPAddress to an IPEndPoint
                response = PingHost(new IPEndPoint(serverAddress, 0), pingCount);
            }
            catch (Exception ex)
            {
                OnPingError(PingResponseType.InternalError, ex.Message);

                response = new PingResponse();
                response.PingResult = PingResponseType.InternalError;
                response.ErrorMessage = ex.Message;
            }

            return response;
        }
Exemplo n.º 12
0
        /// <summary>
        /// Attempts to ping a host.
        /// </summary>
        /// <param name="hostname">Host to ping</param>
        /// <param name="pingCount">Ping count</param>
        /// <returns>Ping results</returns>
        public PingResponse PingHost(string hostname, int pingCount)
        {
            IPHostEntry host = NetworkUtilities.ResolveHost(hostname);

            if (host == null)
            {
                OnPingError(PingResponseType.CouldNotResolveHost, "Could not resolve the host '" + hostname + "'");

                PingResponse response = new PingResponse();
                response.PingResult = PingResponseType.CouldNotResolveHost;
                response.ErrorMessage = "Could not resolve the host '" + hostname + "'";

                return response;
            }
            else
            {
                return PingHost(new IPEndPoint(host.AddressList[0], 0), pingCount);
            }
        }