在MonitorWnd.cs类中:
事件处理函数:
//tabControl1是Form窗体中创建的控件,也就是UI线程中创建的控件
//要在事件处理函数中动态的创建TabPage,这个事件处理函数是被另一个read线程调用的
private void MonitorWnd_Load(object sender, EventArgs e)
{
monAge = new MonitorAgency(curNet);
monAge.parser.UserAction += new UserActionHandler(parser_UserAction);
monAge.Connect();
}
void parser_UserAction(object sender, UserActionEventArgs args)
{
#region
if (tabControl1.InvokeRequired)
{
tabControl1.Invoke(new MethodInvoker(delegate { AddPageToTabControl(tabControl1, args); }));
}
else
{
AddPageToTabControl(tabControl1, args);
}
#endregion
}
void AddPageToTabControl(TabControl ctrl, UserActionEventArgs args)
{
//TODO:取得一个用户行为信息的时候引发这个事件
//动态创建tab页
string tabTitle = args.UserName + " " + args.ProcID;
string addTxt = args.Time + "\r\n" + args.IP + "\r\n" + args.Remark;
bool isFind = false;
foreach (TabPage tpUser in this.tabControl1.Controls)
{
if (tpUser.Text == tabTitle)
{
isFind = true;
string strtxt = ((TextBox)tpUser.Controls[0]).Text;
((TextBox)tpUser.Controls[0]).Text = strtxt + "\r\n" + addTxt;
ctrl.SelectedTab = tpUser;
break;
}
}
if (isFind == false)
{
TabPage newTpUser = new TabPage();
newTpUser.Text = tabTitle;
TextBox tb = new TextBox();
tb.Dock = DockStyle.Fill;
tb.Multiline = true;
tb.ScrollBars = ScrollBars.Both;
tb.Text = addTxt;
newTpUser.Controls.Add(tb);
newTpUser.Select();
ctrl.TabPages.Add(newTpUser);
ctrl.SelectedTab = newTpUser;
}
}
MonitorAgency.cs中:
1using System;
2using System.Collections.Generic;
3using System.Text;
4using System.Net;
5using System.Net.Sockets;
6using System.Diagnostics;
7using System.Windows.Forms;
8using System.IO;
9using System.Xml;
10using System.Threading;
11
12namespace BurEmluator
13{
14 public class MonitorAgency
15 {
16 memberVar#region memberVar
17 代理相关信息#region 代理相关信息
18 private string m_proxyIP = null;
19 private int m_proxyPort = 0;
20 #endregion
21 网元相关信息#region 网元相关信息
22 private string m_curNet = null;
23 #endregion
24 private TcpClient client = new TcpClient();
25 private NetworkStream stream;
26 private String sendMsg = null;
27 private StreamReader sr;
28 private StreamWriter sw;
29 public Thread readThread;
30 private string allUsersInfo = null;
31
32 private string StrMonInfo = "";
33 代理相关#region 代理相关
34 public static Socket ClientSocket = null;
35 public IPEndPoint EPServer;
36 #endregion
37 public MonitorResultParser parser;
38 #endregion
39
40 property#region property
41 public string AllUsersInfo
42 {
43 get { return allUsersInfo; }
44 set { allUsersInfo = value; }
45 }
46
47 #endregion
48 construct#region construct
49 public MonitorAgency(string curNet)
50 {
51
52 //读配置文件取出代理相关信息
53 ReadMonitorXML();
54 EPServer = new IPEndPoint(IPAddress.Parse(m_proxyIP), m_proxyPort);
55 m_curNet = curNet;
56
57 parser = new MonitorResultParser();
58
59 }
60 #endregion
61 public bool Connect()
62 {
63
64
65 try
66 {
67 //连接互助SOCK
68 ClientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
69
70 ClientSocket.Connect(EPServer);
71 //连接互助SOCK
72 client.Connect(IPAddress.Parse(m_proxyIP), Convert.ToInt32(m_proxyPort));
73
74 //发送TELNET IP PORT
75 //sendMsg = string.Format("telnet %s %s\r\n",m_proxyIP, m_proxyPort);
76 //DispatchMessage(sendMsg);
77 readThread = new Thread(new ThreadStart(Read));
78 readThread.IsBackground = true;
79 readThread.Start();
80
81 return true;
82 }
83 catch (Exception ex)
84 {
85 MessageBox.Show(ex.Message);
86 return false;
87 }
88
89 }
90 private void Close()
91 {
92 if (ClientSocket.Connected)
93 {
94 ClientSocket.Close();
95 }
96 }
97 protected void Read()
98 {
99 while (true)
100 {
101 try
102 {
103 Byte[] Rec = new byte[1024];
104 NetworkStream netstream = new NetworkStream(ClientSocket);
105 int iRev = netstream.Read(Rec, 0, Rec.Length);//读取客户发送来的信息。
106 string strRev = System.Text.Encoding.GetEncoding("gb2312").GetString(Rec, 0, iRev);
107 //输入需要监控的网元
108 if (strRev.Length >= 0)
109 {
110 if (strRev.IndexOf("请输入命令") != -1)
111 {
112 sendMsg = "track " + m_curNet + "\r\n";
113
114 DispatchMessage(sendMsg);
115 }
116
117 else
118 {
119 int idxend = strRev.LastIndexOf("END");
120 int idxj = strRev.LastIndexOf("<");
121 int idxn = strRev.LastIndexOf("\n");
122
123 if (idxn == strRev.Length-1 || idxend != -1 || idxj != -1)
124 {
125 StrMonInfo = StrMonInfo + strRev;
126 parser.ProcessResult(StrMonInfo);
127 StrMonInfo = "";
128 }
129 else
130 {
131 StrMonInfo = StrMonInfo + strRev;
132 }
133 }
134 }
135
136
137 }
138 catch (Exception ex)
139 {
140 MessageBox.Show(ex.Message + ":" + ex.StackTrace.ToString());
141 }
142 Thread.Sleep(100);
143 }
144 }
145 public void CloseConnect(bool p)
146 {
147 if (client != null)
148 {
149 client.Close();
150 }
151 if (readThread.IsAlive == true)
152 {
153 readThread.Abort();
154 }
155 }
156 private void ReadMonitorXML()
157 {
158 string strFilePath = Application.StartupPath + "\\" + "MonitorConfig.xml";
159 XmlDocument xmlDoc = new XmlDocument();
160 xmlDoc.Load(strFilePath);
161 XmlNode xn = xmlDoc.SelectSingleNode("Monitor");
162 foreach (XmlNode cxn in xn.ChildNodes)
163 {
164 if (cxn.Name.Equals("IP"))
165 {
166 m_proxyIP = cxn.InnerText;
167 }
168 else if (cxn.Name.Equals("Port"))
169 {
170 m_proxyPort = Convert.ToInt32(cxn.InnerText);
171 }
172 }
173 }
174 private void DispatchMessage(string sendMsg)
175 {
176 NetworkStream netstream = new NetworkStream(ClientSocket);
177 try
178 {
179 Byte[] sendbyte = new Byte[1024];
180
181 sendbyte = System.Text.Encoding.GetEncoding("gb2312").GetBytes(sendMsg.ToCharArray());
182 netstream.Write(sendbyte, 0, sendbyte.Length);//向socket服务器发送信息。
183 netstream.Flush();
184 }
185 catch
186 {
187 System.Windows.Forms.MessageBox.Show("发送失败");
188 Close();
189 }
190 }
191 }
192
193 public class UserActionEventArgs : EventArgs
194 {
195 public string Time;
196 public string ProcID;
197 public string UserName;
198 public string IP;
199 public string Remark;
200 }
201
202 public delegate void UserActionHandler(object sender, UserActionEventArgs args);
203 public class MonitorResultParser
204 {
205 string result;
206
207 public event UserActionHandler UserAction;
208
209 public void ProcessResult(string strResult)
210 {
211 result = strResult;
212 string strUserAction = "";
213 int idxBegin = 0, idxEnd = 0;
214 while ((idxBegin = result.IndexOf("时间:")) >= 0)
215 {
216 Debug.Write(result.Length + "\r\n");
217 idxEnd = result.IndexOf("时间:", idxBegin + 3);
218 if (idxEnd > idxBegin)
219 {
220 strUserAction = result.Substring(idxBegin, idxEnd - idxBegin);
221 Debug.Write(result.Length);
222 result = result.Substring(idxEnd);
223 Debug.Write(result.Length + "\r\n");
224 }
225 else if (idxEnd == -1)
226 {
227 Debug.Write(result.Length);
228 strUserAction = result.Substring(idxBegin);
229 result = "";
230 }
231 Debug.Write(strUserAction.Length + "\r\n");
232 UserActionEventArgs arg = ParseResult(strUserAction);
233 UserAction.Invoke(this, arg);
234 }
235 }
236
237 protected UserActionEventArgs ParseResult(string strUserAction)
238 {
239 UserActionEventArgs arg = new UserActionEventArgs();
240 //解析strUserAction
241 int itime = strUserAction.IndexOf("时间:");
242 int ipid = strUserAction.IndexOf("进程号:");
243 int iuser = strUserAction.IndexOf("用户:");
244 int iip = strUserAction.IndexOf("IP:");
245
246 int iremark1 = strUserAction.IndexOf("命令:");
247 int iremark2 = strUserAction.IndexOf("返回信息:");
248 int iremark3 = strUserAction.IndexOf("提示信息:");
249 if (itime >= 0 && ipid >= 0)
250 {
251 arg.Time = strUserAction.Substring(itime, ipid - itime);
252 }
253 if (iuser >= 0)
254 {
255 arg.ProcID = strUserAction.Substring(ipid,iuser - ipid);
256 }
257 if (iip >= 0)
258 {
259 arg.UserName = strUserAction.Substring(iuser,iip - iuser);
260 }
261
262 int iremark = 0;
263 if(iremark1!=-1)
264 {
265 iremark = iremark1;
266 }
267 else if(iremark2 != -1)
268 {
269 iremark = iremark2;
270 }
271 else if(iremark3 != -1)
272 {
273 iremark = iremark3;
274 }
275 arg.IP = strUserAction.Substring(iip,iremark - iip);
276 arg.Remark = strUserAction.Substring(iremark);
277
278 return arg;
279 }
280
281 }
282
283
284
285}
286
posted on 2008-11-19 09:25
天书 阅读(2081)
评论(0) 编辑 收藏 引用