Click here to Skip to main content
15,897,187 members
Home / Discussions / C#
   

C#

 
GeneralRe: hash function Pin
Mark Churchill24-Jun-08 22:24
Mark Churchill24-Jun-08 22:24 
GeneralRe: hash function Pin
George_George24-Jun-08 22:41
George_George24-Jun-08 22:41 
GeneralRe: hash function Pin
George_George24-Jun-08 22:04
George_George24-Jun-08 22:04 
QuestionWhen maximize Form - how to save the control place ? Pin
Yanshof24-Jun-08 3:16
Yanshof24-Jun-08 3:16 
AnswerRe: When maximize Form - how to save the control place ? Pin
Christian Graus24-Jun-08 3:16
protectorChristian Graus24-Jun-08 3:16 
GeneralRe: When maximize Form - how to save the control place ? Pin
Yanshof24-Jun-08 3:27
Yanshof24-Jun-08 3:27 
AnswerRe: When maximize Form - how to save the control place ? Pin
Abhijit Jana24-Jun-08 3:20
professionalAbhijit Jana24-Jun-08 3:20 
QuestionPing. Pin
Member 154183824-Jun-08 3:14
Member 154183824-Jun-08 3:14 
Hi.

I've found a ping example which I've been tryig do expand with a database etc, but I have one question:

When using the code below, I cant seem to get it to continue pinging - it just stops when "a host is not there".

Can anyone see how I can make it continue and just add the data "DNS Error" to the database?



using System;

using System.Collections.Generic;

using System.Text;

using System.Net;

using System.Net.NetworkInformation;

using System.Runtime.InteropServices;

using System.Threading;

using System.Data;

using System.Data.SqlClient;

namespace printHosts

{

class Program

{

static void Main(string[] args)

{

if (args.Length == 0)

{


}


SqlConnection oconn;

SqlCommand ocomm;

string sSql = "";


int ID = 0;

string address = "";


oconn = new SqlConnection(@"server=server;database=database;user id=user; password=password");

sSql = "SELECT hostname FROM Hostinfo";

ocomm = new SqlCommand(sSql);

SqlDataAdapter myapdater = new SqlDataAdapter(sSql, oconn);

DataSet ds = new DataSet("Hostinfo");

DataTable dataTable = ds.Tables["Hostinfo"];

myapdater.Fill(ds, "Hostinfo");

foreach (DataRow row in ds.Tables[0].Rows)

{

address = row["hostname"].ToString();

Console.WriteLine(address);

try

{

sSql = "SELECT COUNT(*) FROM Hostinfo WHERE hostname ='" + address + "'";

ocomm = new SqlCommand(sSql, oconn);

if (Convert.ToInt32(ocomm.ExecuteScalar()) == 0)

{

sSql = "INSERT INTO Hostinfo (hostname, currentPingTime) values "

+ "('" + row["hostname"].ToString() + "', '"

+ DateTime.Now + "', '"

+ "')";

ocomm = new SqlCommand(sSql, oconn);

ocomm.ExecuteNonQuery();

sSql = "SELECT ID FROM Hostinfo WHERE HostName='" + address + "'";

ocomm = new SqlCommand(sSql, oconn);

ID = Convert.ToInt32(ocomm.ExecuteScalar());

ocomm = new SqlCommand(sSql, oconn);

ID = Convert.ToInt32(ocomm.ExecuteScalar());

}

else

{

sSql = "SELECT ID FROM Hostinfo WHERE HostName='" + address + "'";

ocomm = new SqlCommand(sSql, oconn);

ID = Convert.ToInt32(ocomm.ExecuteScalar());

sSql = "UPDATE Hostinfo SET "

+ "', LastPingTime ='" + ""

+ " WHERE id=" + ID;

ocomm = new SqlCommand(sSql, oconn);

ocomm.ExecuteNonQuery();

}

oconn.Close();

}

catch

{

oconn.Open();

DateTime LastUpdatedPingTime = DateTime.Now;

sSql = "SELECT COUNT(*) FROM Hostinfo WHERE hostname ='" + address + "'";

ocomm = new SqlCommand(sSql, oconn);

if (Convert.ToInt32(ocomm.ExecuteScalar()) == 0)

{

sSql = "INSERT INTO Hostinfo (hostname, currentPingTime, repley) values"

+ "('" + row["hostname"].ToString() + "', '"

+ DateTime.Now + "', '"

+ address + "', '"

+ "')";

ocomm = new SqlCommand(sSql, oconn);

ocomm.ExecuteNonQuery();

sSql = "SELECT ID FROM Hostinfo WHERE HostName='" + address + "'";

ocomm = new SqlCommand(sSql, oconn);

ID = Convert.ToInt32(ocomm.ExecuteScalar());



ocomm = new SqlCommand(sSql, oconn);

ID = Convert.ToInt32(ocomm.ExecuteScalar());

}

else

{

sSql = "SELECT ID FROM Hostinfo WHERE HostName='" + address + "'";

ocomm = new SqlCommand(sSql, oconn);

ID = Convert.ToInt32(ocomm.ExecuteScalar());



sSql = "UPDATE Hostinfo SET "

+ " currentPingTime ='" + DateTime.Now

+ "', repley ='" + address

+ "' WHERE id= '" + ID + "'"; ;

ocomm = new SqlCommand(sSql, oconn);

ocomm.ExecuteNonQuery();

}

oconn.Close();

}

if (IsOffline())

{

return;

}

IPAddress ip = null;

try

{

ip = Dns.GetHostEntry(address).AddressList[0];

}

catch (System.Net.Sockets.SocketException ex)

{


Console.WriteLine("DNS Error: {0}", ex.Message);

Console.WriteLine("Press any key to exit.");

Console.ReadLine();

return;

}


//Console.WriteLine("Pinging {0} [{1}] with 32 bytes of data:", address, ip.ToString());

Console.WriteLine();

Thread pingThread = new Thread(new ParameterizedThreadStart(StartPing));

pingThread.Start(ip);

pingThread.Join();

}


}

[Flags]

enum ConnectionState : int

{

INTERNET_CONNECTION_MODEM = 0x1,

INTERNET_CONNECTION_LAN = 0x2,

INTERNET_CONNECTION_PROXY = 0x4,

INTERNET_RAS_INSTALLED = 0x10,

INTERNET_CONNECTION_OFFLINE = 0x20,

INTERNET_CONNECTION_CONFIGURED = 0x40

}

[DllImport("wininet", CharSet = CharSet.Auto)]

static extern bool InternetGetConnectedState(ref ConnectionState lpdwFlags, int dwReserved);

static bool IsOffline()

{

ConnectionState state = 0;

InternetGetConnectedState(ref state, 0);

if (((int)ConnectionState.INTERNET_CONNECTION_OFFLINE & (int)state) != 0)

{

return true;

}

return false;

}


static void StartPing(object argument)

{

SqlConnection oconn;

SqlCommand ocomm;

string sSql = "";

int ID = 0;

string address = "";


try

{

oconn = new SqlConnection("server=sqlserver;database=audit_data;uid=user;pwd=password;");

oconn.Open();

sSql = "SELECT Hostname from hostinfo WHERE HostName='" + address + "'";

ocomm = new SqlCommand(sSql, oconn);

ocomm.ExecuteNonQuery();

{

ocomm = new SqlCommand(sSql, oconn);

ocomm.ExecuteNonQuery();

}

oconn.Close();

}

catch (Exception e)

{


}

IPAddress ip = (IPAddress)argument;

//set options ttl=128 and no fragmentation

PingOptions options = new PingOptions(128, true);

//create a Ping object

Ping ping = new Ping();

//32 empty bytes buffer

byte[] data = new byte[32];

int received = 0;

List<long> responseTimes = new List<long>();

//ping 4 times

for (int i = 0; i < 1; i++)

{

PingReply reply = ping.Send(ip, 1000, data, options);

if (reply != null)

{

switch (reply.Status)

{

case IPStatus.Success:


Console.WriteLine("Reply from {0}",

reply.Address, reply.Buffer.Length, reply.RoundtripTime, reply.Options.Ttl);

received++;

responseTimes.Add(reply.RoundtripTime);

break;

case IPStatus.TimedOut:

Console.WriteLine("Request timed out.");

break;


default:

Console.WriteLine("Ping failed {0}", reply.Status.ToString());

break;

}

}

else

{

Console.WriteLine("Ping failed for an unknown reason");

}

}

}

}

}





Thanks Jan.
GeneralRe: Ping. Pin
Abhijit Jana24-Jun-08 3:22
professionalAbhijit Jana24-Jun-08 3:22 
AnswerRe: Ping. Pin
leppie24-Jun-08 5:00
leppie24-Jun-08 5:00 
GeneralRe: Ping. Pin
Member 154183824-Jun-08 18:13
Member 154183824-Jun-08 18:13 
QuestionHow to display excel sheet in html page using C# Pin
Murugan k24-Jun-08 3:00
Murugan k24-Jun-08 3:00 
AnswerRe: How to display excel sheet in html page using C# Pin
Abhijit Jana24-Jun-08 3:02
professionalAbhijit Jana24-Jun-08 3:02 
Questionaccess network pc Pin
Rinki Mukheraji24-Jun-08 2:26
Rinki Mukheraji24-Jun-08 2:26 
AnswerRe: access network pc Pin
Abhijit Jana24-Jun-08 2:46
professionalAbhijit Jana24-Jun-08 2:46 
GeneralRe: access network pc Pin
Rinki Mukheraji24-Jun-08 18:34
Rinki Mukheraji24-Jun-08 18:34 
Questionmicrosoft word document 2003 Pin
Miss_hacker24-Jun-08 1:52
Miss_hacker24-Jun-08 1:52 
AnswerRe: microsoft word document 2003 Pin
Christian Graus24-Jun-08 1:59
protectorChristian Graus24-Jun-08 1:59 
GeneralRe: microsoft word document 2003 Pin
Miss_hacker24-Jun-08 2:06
Miss_hacker24-Jun-08 2:06 
GeneralRe: microsoft word document 2003 Pin
Pete O'Hanlon24-Jun-08 2:09
mvePete O'Hanlon24-Jun-08 2:09 
GeneralRe: microsoft word document 2003 Pin
Miss_hacker24-Jun-08 3:10
Miss_hacker24-Jun-08 3:10 
GeneralRe: microsoft word document 2003 Pin
DaveyM6924-Jun-08 3:53
professionalDaveyM6924-Jun-08 3:53 
GeneralRe: microsoft word document 2003 Pin
Paul Conrad2-Jul-08 18:30
professionalPaul Conrad2-Jul-08 18:30 
JokeRe: microsoft word document 2003 Pin
Abhijit Jana24-Jun-08 2:09
professionalAbhijit Jana24-Jun-08 2:09 
AnswerRe: microsoft word document 2003 Pin
Abhijit Jana24-Jun-08 2:07
professionalAbhijit Jana24-Jun-08 2:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.