/// <summary> /// This function performs the actual ping. It sends the ping packets created to /// the destination /// </summary> public bool DoPing() { bool success = false; // Send an echo request while (pingCount > 0) { try { pingCount--; // Increment the sequence count in the ICMP header icmpHeader.Sequence = (ushort)(icmpHeader.Sequence + (ushort)1); // Build the byte array representing the ping packet. This needs to be done // before ever send because we change the sequence number (which will affect // the calculated checksum). pingPacket = icmpHeader.BuildPacket(pingId, pingPayload); // Mark the time we sent the packet pingSentTime = DateTime.Now; // Send the echo request pingSocket.SendTo(pingPacket, destEndPoint); int Brecieved = pingSocket.ReceiveFrom( receiveBuffer, 0, receiveBuffer.Length, SocketFlags.None, ref castResponseEndPoint ); //IF you get to here, then you got a response. success = true; break; } catch (SocketException err) { //PING FAILED, try it again for remaining ping counts } } return(success); }
/// <summary> /// This function performs the actual ping. It sends the ping packets created to /// the destination and posts the async receive operation to receive the response. /// Once a ping is sent, it waits for the receive handler to indicate a response /// was received. If not it times out and indicates this to the user. /// </summary> public void DoPing() { // If the packet hasn't already been built, then build it. Console.WriteLine("In DoPing() method, do the pinging..."); Console.WriteLine(); if (protocolHeaderList.Count == 0) { BuildPingPacket(); } Console.WriteLine("Pinging {0} with {1} bytes of data ", destEndPoint.Address.ToString(), pingPayloadLength); try { // Post an async receive first to ensure we receive a response to the echo request // in the event of a low latency network. pingSocket.BeginReceiveFrom( receiveBuffer, 0, receiveBuffer.Length, SocketFlags.None, ref castResponseEndPoint, receiveCallback, this ); // Keep track of how many outstanding async operations there are Interlocked.Increment(ref pingOutstandingReceives); // Send an echo request and wait for the response while (pingCount > 0) { Interlocked.Decrement(ref pingCount); // Increment the sequence count in the ICMP header if (destEndPoint.AddressFamily == AddressFamily.InterNetwork) { icmpHeader.Sequence = (ushort)(icmpHeader.Sequence + (ushort)1); // Build the byte array representing the ping packet. This needs to be done // before ever send because we change the sequence number (which will affect // the calculated checksum). pingPacket = icmpHeader.BuildPacket(protocolHeaderList, pingPayload); } else if (destEndPoint.AddressFamily == AddressFamily.InterNetworkV6) { icmpv6EchoRequestHeader.Sequence = (ushort)(icmpv6EchoRequestHeader.Sequence + (ushort)1); // Build the byte array representing the ping packet. This needs to be done // before ever send because we change the sequence number (which will affect // the calculated checksum). pingPacket = icmpv6Header.BuildPacket(protocolHeaderList, pingPayload); } // Mark the time we sent the packet pingSentTime = DateTime.Now; // Send the echo request pingSocket.SendTo(pingPacket, destEndPoint); // Wait for the async handler to indicate a response was received if (pingReceiveEvent.WaitOne(pingReceiveTimeout, false) == false) { // timeout occurred Console.WriteLine("Request timed out."); } else { // Reset the event pingReceiveEvent.Reset(); } // Sleep for a short time before sending the next request Thread.Sleep(1000); } } catch (SocketException err) { Console.WriteLine("Socket error occurred: {0}", err.Message); throw; } }