I found a code to check the connectivity of multiple host that read the addresses from a list and ping them as an asynchronous manner and processes the result.
When I use a predefined list,for example:
public static List<string> addresses = new List<string> { "172.20.74.1", "192.168.1.103", "192.168.1.104", "192.168.1.105"};
everything works properly, but when I read this list from Database, the NullReferenceException occurs. Can someone help me solve this problem?
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.NetworkInformation;
using System.Data.SqlClient;
using System.Collections.Generic;
using System;
namespace asyncPing
{
class Program
{
public static List<string> addresses = getNetAddress();
//public static List<string> addresses = new List<string> { "172.20.74.1", "192.168.1.103", "192.168.1.104", "192.168.1.105"};
static void Main(string[] args)
{
List<Task<PingReply>> pingTasks = new List<Task<PingReply>>();
foreach (var address in addresses)
{
Console.WriteLine(address);
}
foreach (var address in addresses)
{
pingTasks.Add(PingAsync(address));
}
//Wait for all the tasks to complete
Task.WaitAll(pingTasks.ToArray());
//Now you can iterate over your list of pingTasks
foreach (var pingTask in pingTasks)
{
Console.WriteLine(pingTask.Result.Address "\t" pingTask.Result.Status);
if (pingTask.Result.Status != IPStatus.Success)
{
//Do Some Thing
}
}
Console.ReadLine();
}
public static List<string> getNetAddress()
{
List<string> addr = new List<string>();
string connetionString = @"Data Source=localhost;Initial Catalog=AlarmsDB;Persist Security Info=True;User ID=sa;Password=123456";
using (SqlConnection myConnection = new SqlConnection(connetionString))
{
//Console.WriteLine("Connection Open !");
string oString = "Select ip_address from Networks";
SqlCommand oCmd = new SqlCommand(oString, myConnection);
myConnection.Open();
using (SqlDataReader oReader = oCmd.ExecuteReader())
{
while (oReader.Read())
{
addr.Add(oReader["ip_address"].ToString());
}
myConnection.Close();
}
}
return addr;
}
static Task<PingReply> PingAsync(string address)
{
var tcs = new TaskCompletionSource<PingReply>();
Ping ping = new Ping();
ping.PingCompleted = (obj, sender) =>
{
tcs.SetResult(sender.Reply);
};
ping.SendAsync(address, new object());
return tcs.Task;
}
}
}
The exception occures in line:
Console.WriteLine(pingTask.Result.Address "\t" pingTask.Result.Status);
It seems the result of async pings method appears to null. I don't know why when the same input passed from database, the result the output changes?
CodePudding user response:
Thanks to all the guys for their suggestions to solve the problem
Finally i found the problem source after 2 day
After i triming the ip address before pass it to asyncPing method the problem resolved.
I saved the ip address as strings in the database but I do not know why I have to trim them after reading
I was able to solve the problem by changing the line below:
using (SqlDataReader oReader = oCmd.ExecuteReader())
{
while (oReader.Read())
{
addr.Add(oReader["ip_address"].ToString().Trim());
}
myConnection.Close();
}