(39)C#Ping类
一、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 = ; i < ; i++)
{
//32个字节
data += "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
} //
byte[] buffer = Encoding.ASCII.GetBytes(data);
//
int timeout = ;
//PingReply提供有关的状态和生成的数据的信息的类
//
int a = ;
while (a == )
{
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(); }
else
{
Console.WriteLine(" ");
Console.WriteLine("请求超时");
Console.WriteLine(" ");
Thread.Sleep();
}
}
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();
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 = ;
//创建要传输的N字节数据的缓冲区
string data = string.Empty;
for (int i = ; i < ; 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 = ;
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) == )
{
MessageBox.Show("开始节点要小于结束节点");
}
DataTable dt = oracleDB.query(dtime1, dtime2).Tables[];
int num = dt.Rows.Count;
System.TimeSpan t3 = dtime2 - dtime1;
double getMillisecond = t3.TotalMilliseconds;
label3.Text = "厉时" + getMillisecond + "秒,一共" + num + "条数据";
int count = ;
int ttl = ;
foreach (DataRow dr in dt.Rows)
{ if (Convert.ToString(dr["REMARKS"]) == "请求超时")
{
count += ;
}
string a = Convert.ToString(dr["ROUNDTRIPTIME"]);
if (Convert.ToInt32(dr["ROUNDTRIPTIME"]) > )
{
ttl += ;
}
}
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;
/
#####
(39)C#Ping类的更多相关文章
- WebRequest与WebResponse抽象类,DNS静态类、Ping类
一.概述 1.WebRequest: 对统一资源标识符 (URI) 发出请求. 这是一个 abstract 类. WebRequest的派生类:PackWebRequest.FileWebReques ...
- Java基础知识强化39:StringBuffer类之StringBuffer的删除功能
1. StringBuffer的删除功能: public StringBuffer deleteCharAt(int index):删除指定位置的字符,并返回字符串缓冲区本身. public Str ...
- C# Ping的例子,可用于测试网络,延迟xx毫秒 C#编写网站测速
C#编写网站测速 WebClient wcl = new WebClient(); Stopwatch spwatch = new Stopwatch(); spwatch.Start(); byte ...
- C# 使用ping命令
方法一:调用cmd 的ping命令 private static string CmdPing(string strIp) { Process p = new Process(); p.StartIn ...
- C#.net干货,最全公共帮助类
比较全面的c#帮助类,日常工作收集,包括前面几家公司用到的,各式各样的几乎都能找到,所有功能性代码都是独立的类,类与类之间没有联系,可以单独引用至项目,分享出来,方便大家,几乎都有注释,喜欢的请点赞, ...
- 干货,比较全面的c#.net公共帮助类
比较全面的c#帮助类 比较全面的c#帮助类,日常工作收集,包括前面几家公司用到的,各式各样的几乎都能找到,所有功能性代码都是独立的类,类与类之间没有联系,可以单独引用至项目,分享出来,方便大家,几乎都 ...
- Ribbon Ping机制
在负载均衡器中,提供了 Ping 机制,每隔一段时间,会去 Ping 服务器,判断服务器是否存活,该工作由 com.netflix.loadbalancer.IPing 接口的实现类负责,如果单独使用 ...
- Java类的继承与多态特性-入门笔记
相信对于继承和多态的概念性我就不在怎么解释啦!不管你是.Net还是Java面向对象编程都是比不缺少一堂课~~Net如此Java亦也有同样的思想成分包含其中. 继承,多态,封装是Java面向对象的3大特 ...
- Java实现ping功能的三种方法及Linux的区分
前大半部份转自:https://blog.csdn.net/futudeniaodan/article/details/52317650 检测设备的运行状态,有的是使用ping的方式来检测的.所以需要 ...
随机推荐
- 【jquery】 form ajaxSubmit 问题
常见问题 这个插件跟哪些版本的jQuery兼容? 这个插件需要jQuery v1.0.3 或 以后的版本. 这个插件需要其它插件的支持吗? 不需要. 这个插件的运行效率高吗? 是的!请到 对比页面 查 ...
- PTA 7-1 银行业务队列简单模拟
用链表实现队列操作,代码如下: #include <iostream> #include <cstdio> #include <algorithm> #includ ...
- 新游发布:《Don't touch the color》
这是笨猫工作室最后一个Scratch 2.0游戏,经过笨猫工作室成员的不懈努力,游戏终于可以稳定运行.此次更新添加了最高分数显示,优化了系统流畅度.快来试玩吧!!! 卡搭蓝链:https://kada ...
- UVa 11695 树的直径 Flight Planning
题意: 给出一棵树,删除一条边再添加一条边,求新树的最短的直径. 分析: 因为n比较小(n ≤ 2500),所以可以枚举删除的边,分裂成两棵树,然后有这么一个结论: 合并两棵树后得到的新树的最短直径为 ...
- 下拉列表 Spinner
在Web开发中,HTML提供了下拉列表的实现,就是使用<select>元素实现一个下拉列表,在其中每个下拉列表项使用<option>表示即可.这是在Web开发中一个必不可少的交 ...
- 提交AppStore被拒原因总结
(1)Information Needed We began the review of your app but aren’t able to continue because we need ad ...
- apache 虚拟主机配置(根据不同的域名映射到不同网站)
最近弄了台香港服务器做测试,Web服务器软件用的是Apache2.2,机器只有一台,ip只有一个,但是想测试几个站点,于是尝试了下Apache的虚拟主机配置.之前已经写过一篇博文了——<Apac ...
- zuul sample
https://github.com/kakawait/uaa-behind-zuul-sample/ 1.参考资料 项目:https://github.com/kakawait/uaa-behind ...
- 大数据学习——KETTLE入门学习——kettle安装
https://blog.csdn.net/u012637358/article/details/82593492 下载的kettle是汉化的 改成英文的 工具——选项——选择英文
- PHP-7.1 源代码学习:字节码在 Zend 虚拟机中的解释执行 之 概述
本文简要介绍 zend 虚拟机解释执行字节码的基本逻辑以及相关的数据结构,关于 PHP 源代码的下载,编译,调试可以参考之前的系列文章 execute_ex 我们来看看执行一个简单的脚本 test.p ...