一、Ping类
引用命名空间
using System.Net.NetworkInformation
控制台版
using System; using System.Collections.Generic; using System.Net.NetworkInformation; using System.Text; using System.Threading; namespace ping { class Program { static void Main(string[] args) { //允许应用程序确定远程计算机是否可访问网络的类 Ping pingSender = new Ping(); //用于控制如何 Ping 传输数据包 PingOptions options = new PingOptions(); // options.DontFragment = true; //创建要传输的N字节数据的缓冲区 string data=string.Empty; for (int i = 0; i < 5; i++) { //32个字节 data += "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; } // byte[] buffer = Encoding.ASCII.GetBytes(data); // int timeout = 120; //PingReply提供有关的状态和生成的数据的信息的类 // int a = 1; while (a == 1) { PingReply reply = pingSender.Send("61.135.169.121", timeout, buffer, options); // if (reply.Status == IPStatus.Success) { Console.WriteLine("答复的主机地址: {0}", reply.Address.ToString()); Console.WriteLine("往返时间: {0}ms", reply.RoundtripTime); Console.WriteLine("生存时间(TTL): {0}ms", reply.Options.Ttl); Console.WriteLine("是否控制数据包的分段: {0}", reply.Options.DontFragment); Console.WriteLine("缓冲区大小: {0}", reply.Buffer.Length); Console.WriteLine(" "); //等待1秒 Thread.Sleep(1000); } else { Console.WriteLine(" "); Console.WriteLine("请求超时"); Console.WriteLine(" "); Thread.Sleep(1000); } } Console.ReadLine(); } } }
winfrom版
from1
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Threading; using System.Net.NetworkInformation; using System.Net; namespace pingFrom { public partial class Form1 : Form { System.Timers.Timer t = new System.Timers.Timer(1000); public Form1() { InitializeComponent(); //?????????? System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false; } private void button1_Click(object sender, EventArgs e) { IPAddress ip; if (IPAddress.TryParse(textBox1.Text, out ip) == false) { MessageBox.Show("非法的IP地址"); return; } t.Elapsed += new System.Timers.ElapsedEventHandler(changeShifts); t.Enabled = true; t.AutoReset = true; //每到指定时间Elapsed事件是触发一次(false),还是一直触发(true) } private void button2_Click(object sender, EventArgs e) { t.Enabled = false; } //timer public void changeShifts(object source, System.Timers.ElapsedEventArgs e) { DateTime dtime = DateTime.Now; //textBox2.Text += "abc"; //允许应用程序确定远程计算机是否可访问网络的类 Ping pingSender = new Ping(); //用于控制如何 Ping 传输数据包 PingOptions options = new PingOptions(); // options.DontFragment = true; int timeout = 120; //创建要传输的N字节数据的缓冲区 string data = string.Empty; for (int i = 0; i < 5; i++) { //32个字节 data += "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; } byte[] buffer = Encoding.ASCII.GetBytes(data); // //PingReply提供有关的状态和生成的数据的信息的类 PingReply reply = pingSender.Send(textBox1.Text, timeout, buffer, options); // if (reply.Status == IPStatus.Success) { string str = "目标地址" + textBox1.Text + ",往返时间" + reply.RoundtripTime + "ms ,生存时间(TTL)" + reply.Options.Ttl + "ms, 发送数据大小" + reply.Buffer.Length + "字节"; textBox2.AppendText(str); textBox2.AppendText("\r\n"); textBox2.AppendText("\r\n"); oracleDB.sqlSuccess("mengpc", textBox1.Text, Convert.ToString(reply.RoundtripTime), Convert.ToString(reply.Options.Ttl), Convert.ToString(reply.Buffer.Length), dtime); } else { textBox2.AppendText("请求超时"); textBox2.AppendText("\r\n"); textBox2.AppendText("\r\n"); string remark = "请求超时"; oracleDB.sqlFail("mengpc", textBox1.Text, dtime, remark); } this.textBox2.SelectionStart = this.textBox2.Text.Length; this.textBox2.SelectionLength = 0; this.textBox2.ScrollToCaret(); } private void button3_Click(object sender, EventArgs e) { textBox2.Text = ""; } private void button4_Click(object sender, EventArgs e) { Form2 frm = new Form2(); frm.ShowDialog(); } } }
from2
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Data; namespace pingFrom { public partial class Form2 : Form { public Form2() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { DateTime dtime1 = Convert.ToDateTime(dateTimePicker1.Text); DateTime dtime2 = Convert.ToDateTime(dateTimePicker2.Text); if (dtime1.CompareTo(dtime2) == 1) { MessageBox.Show("开始节点要小于结束节点"); } DataTable dt = oracleDB.query(dtime1, dtime2).Tables[0]; int num = dt.Rows.Count; System.TimeSpan t3 = dtime2 - dtime1; double getMillisecond = t3.TotalMilliseconds; label3.Text = "厉时" + getMillisecond + "秒,一共" + num + "条数据"; int count = 0; int ttl = 0; foreach (DataRow dr in dt.Rows) { if (Convert.ToString(dr["REMARKS"]) == "请求超时") { count += 1; } string a = Convert.ToString(dr["ROUNDTRIPTIME"]); if (Convert.ToInt32(dr["ROUNDTRIPTIME"]) > 100) { ttl += 1; } } label4.Text = "请求超时的数据共有 " + count + " 条"; label5.Text = "ping值大于100的共有 " + ttl + " 条"; } } }
oracleDB.cs
using System; using System.Collections.Generic; using System.Text; using System.Data.OleDb; using System.Data; namespace pingFrom { class oracleDB { /// <summary> /// PING通 /// </summary> /// <param name="loclaName"></param> /// <param name="targetIp"></param> /// <param name="roundtripTime"></param> /// <param name="ttl"></param> /// <param name="bufferSize"></param> /// <param name="dtime"></param> public static void sqlSuccess(string loclaName, string targetIp, string roundtripTime,string ttl,string bufferSize,DateTime dtime) { string str = "Provider=OraOLEDB.Oracle.1;User ID=MES;Password=zidonghua;Data Source=(DESCRIPTION = (ADDRESS_LIST= (ADDRESS = (PROTOCOL = TCP)(HOST = 127.0.0.1)(PORT = 1521))) (CONNECT_DATA = (SERVICE_NAME = ORCL)))"; //测试用sql: insert into PINGTABLE (LOCALNAME,TARGETIP,ROUNDTRIPTIME,TTLTIME,BUFFERSIZE,PINGTIME,INSERTTIME) VALUES('mengpc','192.166.208.60','8ms','50ms','320byte',sysdate,to_date('2017-2-1 23:59:59','yyyy-mm-dd hh24-mi-ss')) string sql = string.Format(@"insert into PINGTABLE (LOCALNAME,TARGETIP,ROUNDTRIPTIME,TTLTIME,BUFFERSIZE,PINGTIME,INSERTTIME) VALUES('{0}','{1}','{2}','{3}','{4}',to_date('{5}','yyyy-mm-dd hh24-mi-ss'),sysdate)", loclaName, targetIp, roundtripTime, ttl, bufferSize, dtime); using (OleDbConnection conn = new OleDbConnection(str)) { conn.Open(); OleDbCommand command = new OleDbCommand(sql, conn); command.ExecuteNonQuery(); } } /// <summary> /// 网络不通 /// </summary> /// <param name="loclaName"></param> /// <param name="targetIp"></param> /// <param name="dtime"></param> /// <param name="remark"></param> public static void sqlFail(string loclaName, string targetIp, DateTime dtime,string remark) { string str = "Provider=OraOLEDB.Oracle.1;User ID=MES;Password=zidonghua;Data Source=(DESCRIPTION = (ADDRESS_LIST= (ADDRESS = (PROTOCOL = TCP)(HOST = 127.0.0.1)(PORT = 1521))) (CONNECT_DATA = (SERVICE_NAME = ORCL)))"; string sql = string.Format(@"insert into PINGTABLE (LOCALNAME,TARGETIP,ROUNDTRIPTIME,PINGTIME,INSERTTIME,REMARKS) VALUES('{0}','{1}',0,to_date('{2}','yyyy-mm-dd hh24-mi-ss'),sysdate,'{3}')", loclaName, targetIp, dtime, remark); using (OleDbConnection conn = new OleDbConnection(str)) { conn.Open(); OleDbCommand command = new OleDbCommand(sql, conn); command.ExecuteNonQuery(); } } public static DataSet query(DateTime dtime1, DateTime dtime2) { string str = "Provider=OraOLEDB.Oracle.1;User ID=MES;Password=zidonghua;Data Source=(DESCRIPTION = (ADDRESS_LIST= (ADDRESS = (PROTOCOL = TCP)(HOST = 127.0.0.1)(PORT = 1521))) (CONNECT_DATA = (SERVICE_NAME = ORCL)))"; string sql = string.Format("select * from PINGTABLE where PINGTIME>= to_date('{0}','yyyy-mm-dd hh24-mi-ss') and PINGTIME< to_date('{1}','yyyy-mm-dd hh24-mi-ss')", dtime1, dtime2); using (OleDbConnection conn = new OleDbConnection(str)) { conn.Open(); OleDbCommand command = new OleDbCommand(sql, conn); OleDbDataAdapter adapter = new OleDbDataAdapter(command); DataSet ds = new DataSet(); adapter.Fill(ds); return ds; } } } }
建表
#创建用户命令 create user MES identified by zidonghua #授予dba权限 grant connect,resource,dba to MES; #创建表 create table pingTable(id int,localName varchar2(80),targetIP varchar2(50),roundtripTime varchar2(50),ttlTime varchar2(50),bufferSize varchar2(50),pingTime date,insertTime date,remarks varchar2(200)); # COMMENT ON COLUMN pingTable.localName IS '本地主机名' / COMMENT ON COLUMN pingTable.targetIP IS '目标IP地址' / COMMENT ON COLUMN pingTable.roundtripTime IS '返回时间' / COMMENT ON COLUMN pingTable.ttlTime IS '生存时间' / COMMENT ON COLUMN pingTable.bufferSize IS '数据大小' / COMMENT ON COLUMN pingTable.pingTime IS 'ping的当时时间' / COMMENT ON COLUMN pingTable.insertTime IS '插入数据库时间' / COMMENT ON COLUMN pingTable.remarks IS '备注' / ####### 序列,触发器 CREATE SEQUENCE SEQ_pingTable START WITH 1 INCREMENT BY 1 NOMINVALUE NOMAXVALUE CACHE 20 NOORDER; -- -- TRIGGER: TR_BD_APPLY_MATERIAL -- create or replace trigger TR_pingTable before insert on pingTable for each row begin select SEQ_pingTable.nextval into :new.ID from dual; end; / #####