Hi guys,
I have written a code for my forms application, this application scans all active IP's in a big range using PING.ASYNC. I used a windows MSDN exmaple for this.
When i run this code in my forms application some ping returns with a delay of +/- 10 seconds, or worse some never...
When i run the EXACT the same code with a consule application all my result get back within a timespan of 500 milleseconds.
Code example consule :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Siemens;
using IPScanner;
namespace ConsuleInterface
{
class Program
{
static void Main(string[] args)
{
Scanner Scanner = new Scanner();
StringBuilder Scanlist = new StringBuilder();
Console.WriteLine("Start scan, please wait");
Scanlist = Scanner.Find("192.168.0.1");
Console.Write(Scanlist);
Console.ReadKey();
}
}
}
Code example forms :
private void BTN_Scan_IP_Range_Click(object sender, EventArgs e)
{
TxtDump.Text = "";
TxtDump.Text = "Starting scan, please wait for 20 seconds for results";
TxtDump.Text = Scanner.Find(IP_Adres_PLC.Text).ToString();
}
Source code :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;
using System.Net.NetworkInformation;
using Siemens;
using System.Diagnostics;
using System.Threading;
using System.Runtime.InteropServices;
namespace IPScanner
{
public class Scanner
{
static CountdownEvent countdown;
static int upCount = 0;
static object lockObj = new object();
const bool resolveNames = false;
static StringBuilder Scanlist = new StringBuilder();
public StringBuilder Find(string IpAdress)
{
string[] IPV6 = IpAdress.Split(new char[] { ':' });
if (IPV6.Length == 1)
{
string[] IPV4 = IpAdress.Split(new char[] { '.' });
if (IPV4.Length <= 3)
{
return Scanlist.AppendLine("Invalid IP adress entered, please enter a valid ip adres. Example: (IPv4) 192.168.1.1");
}
foreach (Char c in IpAdress.ToCharArray())
{
if (Char.IsDigit(c)) continue;
else if (c == '.') continue;
else return Scanlist.AppendLine("Invalid IP adress entered, please enter a valid ip adres. Example: (IPv4) 192.168.1.1");
}
countdown = new CountdownEvent(1);
Stopwatch sw = new Stopwatch();
sw.Start();
for (int i = 1; i <= 254; i++)
{
string IP = string.Format("{0}.{1}.{2}.{3}", IPV4[0], IPV4[1], IPV4[2], i);
Ping p = new Ping();
p.PingCompleted += new PingCompletedEventHandler(p_PingCompleted);
countdown.AddCount();
p.SendAsync(IP, 100, IP);
}
countdown.Signal();
countdown.Wait();
sw.Stop();
TimeSpan span = new TimeSpan(sw.ElapsedTicks);
Scanlist.AppendLine(String.Format("Took {0} milliseconds. {1} hosts active.", sw.ElapsedMilliseconds, upCount));
return Scanlist;
}
else if (IPV6.Length > 2)
{
return Scanlist.AppendLine("IPv6 is not supported yet");
}
else
{
return Scanlist.AppendLine("Invalid IP adress entered, please enter a valid ip adres. Example: (IPv4) 192.168.1.1 or (IPv6) 3ffe:6a88:85a3:08d3:1319:8a2e:0370:7344");
}
}
public void p_PingCompleted(object sender, PingCompletedEventArgs e)
{
S7CpuInfo CPU_Info = new S7CpuInfo();
string ip = (string)e.UserState;
if (e.Reply != null && e.Reply.Status == IPStatus.Success)
{
string mac = getMAC(IPAddress.Parse(ip));
Scanlist.AppendLine(string.Format("{0} is online - MAC adres - {1} - response time {2} ms)", ip, mac, e.Reply.RoundtripTime));
if (Global.PLC.ConnectTo(ip,0,2) == 0)
{
Global.PLC.GetCpuInfo(CPU_Info);
string cpuname = "", cputype = "";
cpuname = CPU_Info.ModuleName(); cputype = CPU_Info.ModuleTypeName();
Scanlist.AppendLine(string.Format("Siemens PLC found - Type {0} - PLC name {1}", cputype, cpuname));
}
else
{
Scanlist.AppendLine(string.Format("Not an Siemens PLC"));
}
upCount++;
}
else if (e.Reply == null)
{
Scanlist.AppendLine(string.Format("Pinging {0} failed. (Null Reply object?)", ip));
}
countdown.Signal(1);
}
[DllImport("iphlpapi.dll", ExactSpelling = true)]
public static extern int SendARP(int DestIP, int SrcIP, byte[] pMacAddr, ref uint PhyAddrLen);
public static string getMAC(IPAddress address)
{
int intAddress = BitConverter.ToInt32(address.GetAddressBytes(), 0);
byte[] macAddr = new byte[6];
uint macAddrLen = (uint)macAddr.Length;
if (SendARP(intAddress, 0, macAddr, ref macAddrLen) != 0)
return "(NO ARP result)";
string[] str = new string[(int)macAddrLen];
for (int i = 0; i < macAddrLen; i++)
str[i] = macAddr[i].ToString("x2");
return string.Join(":", str);
;
}
}
}
Why is the consule so much faster?
How can i speed up my windows forms application?
Hope you guys can give me some answers, thanks in advance!