您的位置:首页 > 理论基础 > 计算机网络

实习第12天 多线程监控网络,解析XML(未完善完)

2015-08-13 09:23 417 查看
早上把java三大框架搭建了起来

今天做了个关于多线程,网络,xml。

检测所给的ip地址,连接是否正常,不正常的写入数据库日志文件。

数据利用XML进行传输,以及存储。

利用winform,C# 解析XML。

效果图(本来想写控制台程序,柱哥要求有界面)





Form.cs 主界面逻辑

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Xml;
using System.Xml.Linq;
using networkListener;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
//循坏秒数
static int scanSecond = 20;

//存储ip的数组
public static String[] ips = null;

//每个线程处理ip数目
public static int dealNum = 10;

//ip信息DataTable
DataTable infoDt = null;

DateTime beginTime ;

DateTime endTime ;

LogDAL logDAL ;

List<NetThread> threadList;

public Form1()
{
InitializeComponent();

logDAL = new LogDAL();

threadList = new List<NetThread>();

//ip信息表初始化
infoDt = new DataTable();
infoDt.Columns.Add(new DataColumn("名字"));
infoDt.Columns.Add(new DataColumn("ip地址"));

//数据初始化
threadNumText.Text = dealNum + "";
scanSecondText.Text = scanSecond + "";

//timer初始化
timer1.Interval = scanSecond * 1000;
timer1.Stop();
}

/// <summary>
/// 准备需要迭代ip数据
/// </summary>
private  void preparaData()
{
showMsg("读取ip.xml文件中!");
//读取ip.xml文件
XElement xe = XElement.Load("ip.xml");
//@"..\..\ip.xml" 对于输出目录

//直接找到元素为ip的这个结点,然后遍历读取所有的结果.
IEnumerable<XElement> elements = from adress in xe.Elements("ip")
select adress;
showInfo(elements);

}

/// <summary>
/// 迭代XML中所有ip节点
/// </summary>
/// <param name="elements">ip节点集合</param>
private  void showInfo(IEnumerable<XElement> elements)
{
String name;
String ip;
int size = elements.Count();
ips = new String[size];

infoDt.Clear();

//迭代出节点的数据
int i = 0;
foreach (var ele in elements)
{
name = ele.Element("name").Value;
ip = ele.Element("adress").Value;
infoDt.Rows.Add(new object[] { name, ip });
ips[i] = ip;
i++;
}

//数据绑定到表
infoDataGrid.DataSource = infoDt;

}

/// <summary>
/// 刷新日志文件读取
/// </summary>
private void refreshLog()
{

DataSet ds = logDAL.GetList("", "LOG_NETLISTEN", "ROWNUM <= 200", "CREATE_TIME DESC");
DataTable dt=ds.Tables[0];
//数据绑定到表
logDataGrid.DataSource = dt;
logDataGrid.Columns[3].DefaultCellStyle.Format = "yyyy-MM-dd HH:mm:ss ";
logDataGrid.Refresh();
}

/// <summary>
/// 启动按钮时间
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void startButton_Click(object sender, EventArgs e)
{
showMsg("程序成功启动!");
startButton.Enabled = false;
modifyButton.Enabled = false;

//准备ip数据
preparaData();
showMsg("ip数初始化完毕!");

netScan();
timer1.Start();
}

/// <summary>
/// 添加消息
/// </summary>
/// <param name="message">消息</param>
public  void showMsg(String message) {
this.msgTextBox.AppendText(message + System.Environment.NewLine);
}

/// <summary>
/// timer每隔一段时间自动调用
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void timer1_Tick(object sender, EventArgs e)
{
netScan();
}

/// <summary>
///  多线程监控网络
/// </summary>
private void netScan() {
showMsg("多线程监控网络开始");
beginTime = System.DateTime.Now;
//启动网络监听多线程
for (int i = 0; i < ips.Length; i += dealNum)
{
new NetThread(i).start();
}

endTime = System.DateTime.Now;

showMsg("耗时:"+(endTime-beginTime));
showMsg("多线程监控网络结束");

refreshLog();//刷新日志文件
showMsg("更新日志文件");
showMsg("=====================");
}

private void modifyButton_Click(object sender, EventArgs e)
{
threadNumText.Text = dealNum + "";
scanSecondText.Text = scanSecond + "";
dealNum = Convert.ToInt16(threadNumText.Text);
scanSecond = Convert.ToInt16(scanSecondText.Text);
}

}
}


LogDAL.cs 操作数据库

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SysDev.Utility.Data;
using SysDev.Utility.Security;
using System.Configuration;
using System.Data;

namespace WindowsFormsApplication1
{
class LogDAL
{
/// <summary>
/// 返回本项目默认数据库连接串
/// </summary>
/// <returns></returns>
public string GetDefaultConn()
{
return Security.Decrypt(ConfigurationManager.AppSettings["NetListenConn"]);
}

/// <summary>
/// 构造主列表SQL语句
/// </summary>
/// <param name="columnString">显示列</param>
/// <param name="filterString">筛选条件</param>
/// <param name="orderbyString">排序条件</param>
/// <returns>SQL执行语句</returns>
private string CreateSqlList(string columnString, string tableName, string filterString, string orderbyString)
{
StringBuilder cmdText = new StringBuilder();
if (columnString == "")
cmdText.Append("select * from " + tableName);     //默认全显示 sys_binding
else
cmdText.Append("select " + columnString + " from " + tableName + " ");

if (filterString.Trim() != "")
cmdText.Append(" where " + filterString);

if (orderbyString.Trim() != "")
cmdText.Append(" order by " + orderbyString);
else
cmdText.Append(" order by ID");       //默认按主键倒序

return cmdText.ToString();
}

/// <summary>
/// 返回数据列表
/// </summary>
/// <param name="columnString">显示列</param>
/// <param name="filterString">筛选条件</param>
/// <param name="orderbyString">排序条件</param>
/// <returns></returns>
public DataSet GetList(string columnString, string tableName, string filterString, string orderbyString)
{
string sqlString = this.CreateSqlList(columnString, tableName, filterString, orderbyString);
return ORAHelper.Query(sqlString, GetDefaultConn());
}

/// <summary>
/// 添加绑定信息
/// </summary>
/// <param name="sys_id">系统编号</param>
/// <param name="oper_no">主工号</param>
/// <param name="oper_no_binding">绑定的分工号</param>
public void Add(string log_ip, string log_msg)
{
ORAHelper.ExecuteSql("insert into LOG_NETLISTEN (LOG_ID,SERVER_IP,ERROR_MSG) " +
"values(seq_log.nextval,'" + log_ip + "','" + log_msg + "')", GetDefaultConn());
}

}
}


NetThread.cs 线程逻辑

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Net.NetworkInformation;
using WindowsFormsApplication1;
using System.Data;
using System.Xml;
using System.Xml.Linq;

namespace networkListener
{
class NetThread
{
//线程
Thread thread = null;

//ping工具
Ping ping = new Ping();

//ping响应结果
PingReply pingReply = null;

//扫描的起始下标 (包含)
int beginIndex ;

//扫描的终止下标(不包含)
int endIndex;

LogDAL logDAL;

/// <summary>
/// NetThread构造方法,用于传入beginIndex
/// </summary>
/// <param name="beginIndex">线程处理的起始下表</param>
public NetThread(int beginIndex) {
this.beginIndex = beginIndex;
this.endIndex = beginIndex + Form1.dealNum;
logDAL = new LogDAL();
}

/// <summary>
/// 线程的主逻辑业务
/// </summary>
public void run() {

//endIndex 大于ip数目时候
if (endIndex > Form1.ips.Length)
{
endIndex = Form1.ips.Length;
}

for (int i = beginIndex; i < endIndex; i++)
{
//ip响应结果
pingReply = ping.Send(Form1.ips[i]);

//ping的通
if (pingReply.Status == IPStatus.Success)
{
//success
}
else
{   //fail
logDAL.Add(Form1.ips[i], "" + pingReply.Status);
}
}

}

/// <summary>
/// 用于启动线程
/// </summary>
public  void start() {
if (thread == null) {
thread = new Thread(run);
}
thread.Start();
}

public void disposeTest() {
thread.DisableComObjectEagerCleanup();
if (thread!=null)
{
thread = null;
}
}

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: