How to ping faster when I reach an unavailable IP address?

I have an application that uses IP or IP range. The problem is that when hosts are closed, ping takes longer than they are open. When the host is closed, the ping time is about 1-2 seconds.

How can I do this faster when hosts are closed?

This is my code:

using System; using System.Text; using System.Windows.Forms; using System.Net.NetworkInformation; namespace Range_Pinger { public partial class PingIPRange : Form { uint startIP, endIP, currentIP; int count = 0; int open = 0; int closed = 0; public PingIPRange() { InitializeComponent(); tmrPingInterval.Tick += new EventHandler(tmrPingInterval_Tick); } void tmrPingInterval_Tick(object sender, EventArgs e) { if (txtTo.Text == string.Empty) Ping(ip2str(startIP)); else { if (currentIP >= endIP) tmrPingInterval.Stop(); Ping(ip2str(currentIP)); currentIP++; } count++; tsslPingCount.Text = "Total number of pings: " + count.ToString() + " Open IPs: " + open.ToString() + " Closed IPs: " + closed.ToString(); } static uint str2ip(string ip) { string[] numbers = ip.Split('.'); uint x1 = (uint)(Convert.ToByte(numbers[0]) << 24); uint x2 = (uint)(Convert.ToByte(numbers[1]) << 16); uint x3 = (uint)(Convert.ToByte(numbers[2]) << 8); uint x4 = (uint)(Convert.ToByte(numbers[3])); return x1 + x2 + x3 + x4; } static string ip2str(uint ip) { string s1 = ((ip & 0xff000000) >> 24).ToString() + "."; string s2 = ((ip & 0x00ff0000) >> 16).ToString() + "."; string s3 = ((ip & 0x0000ff00) >> 8).ToString() + "."; string s4 = (ip & 0x000000ff).ToString(); return s1 + s2 + s3 + s4; } private void btnPing_Click(object sender, EventArgs e) { txtDisplay.Text = string.Empty; tsslPingCount.Text = string.Empty; count = 0; open = 0; closed = 0; tmrPingInterval.Interval = int.Parse(nudInterval.Value.ToString()); try { startIP = str2ip(txtFrom.Text); if (txtTo.Text != string.Empty) endIP = str2ip(txtTo.Text); currentIP = startIP; tmrPingInterval.Start(); } catch { MessageBox.Show("Invalid input. It must be something like: 255.255.255.255"); } } private void btnStop_Click(object sender, EventArgs e) { tmrPingInterval.Stop(); } private void Ping(string address) { Ping pingSender = new Ping(); PingOptions options = new PingOptions(); options.DontFragment = true; string data = "01234567890123456789012345678901"; byte[] buffer = Encoding.ASCII.GetBytes(data); int timeout = 120; try { PingReply reply = pingSender.Send(address, timeout, buffer, options) ; if (reply.Status == IPStatus.Success) { open++; txtDisplay.AppendText("Host " + address + " is open." + Environment.NewLine); } else { closed++; txtDisplay.AppendText("Host " + address + " is closed." + Environment.NewLine); } } catch (Exception ex) { txtDisplay.SelectedText += Environment.NewLine + ex.Message; } } private void tsmiExit_Click(object sender, EventArgs e) { this.Close(); } } } 

This is what I have now:

  [DllImport("iphlpapi.dll", ExactSpelling = true)] public static extern int SendARP(IPAddress DestIP, int SrcIP, byte[] pMacAddr, ref uint PhyAddrLen); private void Ping(IPAddress address) { byte[] macAddr = new byte[6]; uint macAddrLen = (uint)macAddr.Length; if (SendARP(address, 0, macAddr, ref macAddrLen) == 0) { txtDisplay.AppendText("Host " + address + " is open." + Environment.NewLine); } else txtDisplay.AppendText("Host " + address + " is closed." + Environment.NewLine); } 
+8
c # winforms ping
source share
5 answers

You should not shorten the waiting time. Try sending multiple emails at once async.

 var ping = new Ping(); ping.PingCompleted += (sender, eventArgs) => { // eventArgs.Reply.Address // eventArgs.Reply.Status }; ping.SendAsync(ip, etc.); 
+10
source share

Your address is string . Thus, it will first go through DNS to find out if this is a host name (even if it's an IP address).

I suggest using overload instead of IPAddress .

+6
source share

I created a scanner for the host not so long ago. It uses ARP to check if the computer is on the network. An ARP request is much faster than if you pinged a host. Here is the code I used to check for the presence of the host:

 //You'll need this pinvoke signature as it is not part of the .Net framework [DllImport("iphlpapi.dll", ExactSpelling = true)] public static extern int SendARP(int DestIP, int SrcIP, byte[] pMacAddr, ref uint PhyAddrLen); //These vars are needed, if the the request was a success //the MAC address of the host is returned in macAddr private byte[] macAddr = new byte[6]; private uint macAddrLen; //Here you can put the IP that should be checked private IPAddress Destination = IPAddress.Parse("127.0.0.1"); //Send Request and check if the host is there if (SendARP((int)Destination.Address, 0, macAddr, ref macAddrLen) == 0) { //SUCCESS! Igor it alive! } 

If you're interested, Nmap also uses this technique to find available hosts.

ARP scanning puts Nmap and its optimized algorithms responsible for ARP requests. And if he gets the answer back, Nmap doesn't even have to worry about ping IP addresses, as he already knows that the host is working. This makes ARP scanning much faster and more reliable than IP-based scanning. Thus, this is done by default when scanning ethernet hosts detected by Nmap on an Ethernet LAN. Even if different ping types are specified (for example, -PE or -PS), Nmap uses ARP instead of any of the targets located on the same LAN.

EDIT:

This only works on the current subnet! As long as there is no router between the requesting machine and the target, it should work fine.

ARP is a non-routing protocol and therefore can only be used between systems on the same Ethernet network. [...] arp-scan can be used to discover IP hosts on a local network. It can detect all hosts, including those that block all IP traffic, such as firewalls and systems with incoming filters. - Excerpt from the wiki version of NTA-Monitor

For more information on the SendARP function, you can check the pinvoke.net documentation.

+4
source share

You need to reconfigure the application to use multithreading tasks β†’. Complete the task for each ping, and when you receive a response from the specified host, fire up the event and update the user interface. Changing the socket timeout will help you reduce latency from outrageous to unbearable.

+3
source share

Not sure if this is any help (see the final entry in the stream), this seems like an almost identical problem. What you fear against is the timeout of the protocol stack. You can get around it if you use a socket to connect as you will have more control.

+2
source share

All Articles