码迷,mamicode.com
首页 > 其他好文 > 详细

Socket测试工具包的开发(TCP UDP)

时间:2015-04-02 14:48:59      阅读:233      评论:0      收藏:0      [点我收藏+]

标签:

 

Socket测试工具包的开发

第一阶段,先开发客户端,第二阶段 开发服务端.

1.GPS坐标数据的解析与转化, 比如开始和结尾判断后,中间取字符串的方式. 使用什么样的编码.

2.Json数据的解析与转化,快速的发送类的一种方式,不用考虑那么多的字节转化方式

3.最简单的是发送字符串,这样的功能市面上都有。   我要能够发送字节串,可以写一个16进制串,然后发送出去. 可以定时发送.

4.多个客户端同时发送数据. 模拟数量,然后以列表的形式看发送和返回的数据.

5.添加的测试服务器和设置的参数能够保存下来,下次仍然能够使用.

(一)主窗体

技术分享
  1 using System;
  2 using System.Collections.Generic;
  3 using System.ComponentModel;
  4 using System.Data;
  5 using System.Drawing;
  6 using System.Text;
  7 using System.Windows.Forms;
  8 using SocketTool.Core;
  9 using System.Diagnostics;
 10 
 11 namespace SocketTool
 12 {
 13     public partial class MainForm : Form
 14     {
 15         public MainForm()
 16         {
 17             InitializeComponent();
 18         }
 19 
 20         private TreeNode rootNode1;
 21         private TreeNode rootNode2;
 22         private int index1 = 1;
 23         private int index2 = 1;
 24         private int pageIndex = 0;
 25 
 26         private List<Form> pageList = new List<Form>();
 27         private List<SocketInfo> socketInfoList = new List<SocketInfo>();
 28 
 29         private string XMLFileName = "socketinfo.xml";
 30 
 31         private void Form1_Load(object sender, EventArgs e)
 32         {
 33             rootNode1 = new TreeNode("客户终端", 5, 5);
 34             this.deviceTree.Nodes.Add(rootNode1);
 35             rootNode2 = new TreeNode("服务器终端", 6, 6);
 36             this.deviceTree.Nodes.Add(rootNode2);
 37             
 38             try
 39             {
 40                 //SocketInfo[] sis = MySerializer.DeSerialize(XMLFileName);
 41                 socketInfoList = MySerializer.Deserialize<List<SocketInfo>>(XMLFileName);
 42                 //SocketInfo[] sis = MySerializer.Deserialize<SocketInfo[]>(XMLFileName);
 43 
 44                 foreach(SocketInfo si in socketInfoList)
 45                 {
 46                     if(si.Type == "Server")
 47                     {
 48                         AddServerFormNode(si.Name, si);
 49                     }else{
 50                         AddClientFormNode(si.Name, si);
 51                     }
 52                 }
 53             }
 54             catch (System.Exception ex)
 55             {
 56                 Debug.WriteLine(ex.Message);
 57                 Debug.WriteLine(ex.StackTrace);
 58             }
 59         }
 60 
 61         private void AddClientFormNode(string name, SocketInfo si)
 62         {
 63             TreeNode ch = rootNode1.Nodes.Add(name, name, 7, 7);
 64             index1++;
 65             ClientForm form2 = new ClientForm();
 66             if(si != null)
 67                form2.SocketInfo = si;
 68             TabPage tp = addPage(name, form2);
 69             tp.ImageIndex = 2;
 70             ch.Tag = tp;
 71             rootNode1.ExpandAll();
 72             deviceTree.SelectedNode = ch;
 73         }
 74 
 75         private void AddServerFormNode(string name, SocketInfo si)
 76         {
 77             TreeNode ch = rootNode2.Nodes.Add(name, name, 8, 8);
 78             index2++;
 79 
 80             ServerForm form2 = new ServerForm();
 81             if (si != null)
 82                 form2.SocketInfo = si;
 83             TabPage tp = addPage(name, form2);
 84             ch.Tag = tp;
 85             tp.ImageIndex = 3;
 86             rootNode2.ExpandAll();
 87             deviceTree.SelectedNode = ch;
 88         }
 89 
 90 
 91         private void toolStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
 92         {
 93             if(e.ClickedItem.Name == "tsbAddClient")
 94             {
 95                 string name = "客户端" + index1;
 96                 AddClientFormNode(name, null);
 97             }
 98             else if (e.ClickedItem.Name == "tsbAddServer")
 99             {
100                 string name = "服务器端" + index2;
101                 AddServerFormNode(name, null);
102             }
103             else if (e.ClickedItem.Name == "tsbAbout")
104             {
105                 Process.Start("iexplore.exe", "www.ltmonitor.com");
106             }
107             else if (e.ClickedItem.Name == "tsbDelete")
108             {
109                 TreeNode tn = this.deviceTree.SelectedNode;
110 
111                 if (tn.Level < 1)
112                 {
113                     return;
114                 }
115 
116                 TabPage tp = (TabPage)tn.Tag;
117                 this.tabControl1.TabPages.Remove(tp);
118 
119                 tn.Parent.Nodes.Remove(tn);
120             }
121         }
122 
123         private TabPage addPage(string pageText, Form form)
124         {
125             form.TopLevel = false;
126             form.Dock = DockStyle.Fill;
127             TabPage Page = this.tabPage1;
128             if (pageIndex == 0)
129             {
130                 Page.Controls.Add(form);
131             }
132             else
133             {                
134                 Page = new TabPage();
135                 Page.ImageIndex = 3;
136                 Page.Name = "Page" + pageIndex.ToString();
137                 Page.TabIndex = pageIndex;
138                 this.tabControl1.Controls.Add(Page);
139                 Page.Controls.Add(form);
140                 this.tabControl1.SelectedTab = Page;
141 
142             }
143             Page.Text = pageText;
144             pageIndex++;
145 
146             form.Show();
147             pageList.Add(form);
148             return Page;
149         }
150 
151         private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
152         {
153             //SocketInfo si = new SocketInfo();
154             //si.Name = "服务器端";
155             //socketList.Add(si);
156             socketInfoList = new List<SocketInfo>();
157             foreach(TreeNode tn in this.deviceTree.Nodes)
158             {
159                 foreach (TreeNode ctn in tn.Nodes)
160                 {
161                     TabPage tp = (TabPage)ctn.Tag;
162                     if(tp != null)
163                         tp.Text = ctn.Text;
164                     foreach (Form f in tp.Controls)
165                     {
166                         ISocketInfo isi = (ISocketInfo)f;
167                         isi.SocketInfo.Name = tp.Text;
168                         f.Close();
169                         socketInfoList.Add(isi.SocketInfo);
170                     }
171                 }
172             }
173             
174 
175             MySerializer.Serialize(socketInfoList, XMLFileName);
176         }
177 
178         private void deviceTree_AfterSelect(object sender, TreeViewEventArgs e)
179         {
180             if (e.Node.Level == 1)
181             {
182                 TabPage tp = (TabPage)e.Node.Tag;
183 
184                 this.tabControl1.SelectedTab = tp;
185             }
186         }
187 
188        
189        
190     }
191 }
View Code

(二) 客户端窗体

技术分享
  1 using System;
  2 using System.Collections.Generic;
  3 using System.ComponentModel;
  4 using System.Data;
  5 using System.Drawing;
  6 using System.Text;
  7 using System.Windows.Forms;
  8 using SocketTool.Core;
  9 using System.Threading;
 10 using System.Diagnostics;
 11 
 12 namespace SocketTool
 13 {
 14     public partial class ClientForm : Form, ISocketInfo
 15     {
 16         public ClientForm()
 17         {
 18             InitializeComponent();
 19             SocketInfo = new SocketInfo();
 20 
 21         }
 22 
 23         //private static log4net.ILog logger = log4net.LogManager.GetLogger(typeof(ClientForm));  
 24         private IClient socketClient = new CommTcpClient();
 25         private Thread HeartOutgoingThread;
 26         private Thread SendOutgoingThread ;
 27 
 28         private int sendInterval = 0;
 29         private Boolean IsAutoSend;
 30 
 31         private Boolean continueSend = false;
 32 
 33         private string loginContent;
 34         private string heartContent;
 35         private string sendContent;
 36 
 37         private string errorMsg = "";
 38 
 39         public SocketInfo SocketInfo {get;set;}
 40 
 41         private void btnSend_Click(object sender, EventArgs e)
 42         {
 43             if (rbUdp.Checked) 
 44                 socketClient = new CommUdpClient();
 45 
 46             socketClient.OnDataReceived += new ReceivedHandler(ListenMessage);
 47             socketClient.OnSocketError += new SocketErrorHandler(ListenErrorMessage);
 48             string ServerIP = this.txtIP.Text;
 49             errorMsg = "";
 50             if (string.IsNullOrEmpty(ServerIP))
 51                 errorMsg += "请输入合法的IP地址";
 52             try
 53             {
 54                 int Port = int.Parse(this.txtPort.Text);
 55 
 56                 socketClient.Init(ServerIP, Port);
 57             }
 58             catch (Exception ex)
 59             {
 60                 errorMsg += "请输入合法的端口";
 61             }
 62             loginContent = this.rtLoginData.Text;
 63             heartContent = this.rtHeartData.Text;
 64             sendContent = this.rtSendData.Text;
 65             if (string.IsNullOrEmpty(sendContent))
 66                 errorMsg += "请输入要发送的内容";
 67             sendInterval = int.Parse(txtInterval.Text) * 1000;
 68             if (cbAutoSend.Checked)
 69             {
 70                 try
 71                 {
 72                     sendInterval = int.Parse(txtInterval.Text) * 1000;
 73                 }
 74                 catch (Exception ex)
 75                 {
 76                     errorMsg += "请输入整数的发送时间间隔";
 77                 }
 78                 
 79                 IsAutoSend = true;
 80             }
 81             if (string.IsNullOrEmpty(errorMsg) == false)
 82             {
 83                 MessageBox.Show(errorMsg);
 84                 return;
 85             }
 86             continueSend = true;
 87             btnDisconnect.Enabled = true;
 88             btnSend.Enabled = IsAutoSend == false;
 89 
 90             HeartOutgoingThread = new Thread(new ThreadStart(HeartThreadFunc));
 91             HeartOutgoingThread.Start();
 92 
 93             SendOutgoingThread = new Thread(new ThreadStart(SendThreadFunc));
 94             SendOutgoingThread.Start();
 95             /**
 96             else
 97             {
 98                 if (string.IsNullOrEmpty(errorMsg) == false)
 99                 {
100                     MessageBox.Show(errorMsg);
101                     return;
102                 }
103                 byte[] data = System.Text.Encoding.Default.GetBytes(sendContent);
104                 try
105                 {
106                     tcpClient.SendData(data);
107                     btnDisconnect.Enabled = true;
108                 }
109                 catch (Exception ex)
110                 {
111                     MessageBox.Show("发送数据出错,无法连接服务器");
112                 }
113             }
114              */
115             
116         }
117 
118         private void SendThreadFunc()
119         {
120             //发送登录数据包
121             byte[] loginData = System.Text.Encoding.Default.GetBytes(loginContent);
122 
123             if (rbHex.Checked)
124             {
125                 loginData = ParseUtil.ToByesByHex(loginContent);
126             }
127             socketClient.Send(loginData);
128 
129             //发送一般数据包
130             while (continueSend)
131             {
132                 byte[] data = System.Text.Encoding.Default.GetBytes(sendContent);
133 
134                 if (rbHex.Checked)
135                 {
136                     data = ParseUtil.ToByesByHex(sendContent);
137                 }
138 
139                 try
140                 {
141                     socketClient.Send(data);
142                 }
143                 catch (Exception ex)
144                 {
145                     ListenMessage(0, "", ex.Message);
146                     break;
147                 }
148                 if (IsAutoSend == false)
149                     break;
150                 Thread.Sleep(sendInterval);
151 
152             }
153         }
154 
155         private void HeartThreadFunc()
156         {
157             while (continueSend)
158             {
159                 byte[] data = System.Text.Encoding.Default.GetBytes(heartContent);
160 
161                 if (rbHex.Checked)
162                 {
163                     data = ParseUtil.ToByesByHex(heartContent);
164                 }
165 
166                 try
167                 {
168                     socketClient.Send(data);
169                 }
170                 catch (Exception ex)
171                 {
172                     ListenMessage(0, "", ex.Message);
173                     break;
174                 }
175                 if (IsAutoSend == false)
176                     break;
177                 Thread.Sleep(int.Parse(txtHeartTime.Text) * 1000);
178 
179             }
180         }
181         public void ListenErrorMessage(object o, SocketEventArgs e)
182         {
183             string errorMsg = "[" + e.ErrorCode + "]" + SocketUtil.DescrError(e.ErrorCode);
184 
185             ListenMessage((int)o, "Socket错误", errorMsg);
186 
187         }
188 
189         private void ListenMessage(object ID, string type, string msg)
190         {
191             if (PacketView.InvokeRequired)
192             {
193                 try
194                 {
195                     MsgHandler d = new MsgHandler(ListenMessage);
196                     this.Invoke(d, new object[] {0,type, msg});
197                 }
198                 catch (System.Exception ex)
199                 {
200                     //logger.Error(ex.Message);
201                     //logger.Error(ex.StackTrace);
202                 }
203             }
204             else
205             {
206                 if (type == "Socket错误")
207                 {
208                     continueSend = false;
209                     btnDisconnect.Enabled = false;
210                     btnSend.Enabled = true;
211                 }
212                 if (PacketView.Items.Count > 200)
213                     PacketView.Items.Clear();
214 
215 
216                 ListViewItem item = PacketView.Items.Insert(0, "" + PacketView.Items.Count);
217 
218                 //int length = e.Data.Length;
219                 string strDate = DateTime.Now.ToString("HH:mm:ss");
220                 item.SubItems.Add(strDate);
221                 item.SubItems.Add(msg);
222                 //item.SubItems.Add("" + length);
223             }
224 
225         }
226 
227         public void ListenMessage(object o, ReceivedEventArgs e)
228         {
229             if (PacketView.InvokeRequired)
230             {
231                 try
232                 {
233                     ReceivedHandler d = new ReceivedHandler(ListenMessage);
234                     this.Invoke(d, new object[] {o, e });
235                 }
236                 catch (System.Exception ex)
237                 {
238                     //logger.Error(ex.Message);
239                     //logger.Error(ex.StackTrace);
240                 }
241             }
242             else
243             {
244                 if (PacketView.Items.Count > 200)
245                     PacketView.Items.Clear();
246 
247                 ListViewItem item = PacketView.Items.Insert(0, "" + PacketView.Items.Count);
248 
249                 int length = e.Data.Length;
250                 string strDate = DateTime.Now.ToString("HH:mm:ss");
251                 item.SubItems.Add(strDate);
252                 string msg = ParseUtil.ParseString(e.Data, length);
253                 if (rbHex.Checked)
254                     msg = ParseUtil.ToHexString(e.Data, length);
255                 item.SubItems.Add(msg);
256                 item.SubItems.Add("" + length);
257 
258                 if (cbLog.Checked)
259                 {
260                     //logger.Info(e.RemoteHost.ToString() + " " + msg);
261                 }
262                 //item.SubItems.Add("" + msg.MsgContentDesc);
263             }
264         }
265 
266         private void cbAutoSend_CheckedChanged(object sender, EventArgs e)
267         {
268             this.txtInterval.Enabled = cbAutoSend.Checked;
269         }
270 
271         private void btnDisconnect_Click(object sender, EventArgs e)
272         {
273             continueSend = false;
274             try
275             {
276                 HeartOutgoingThread.Abort();
277                 SendOutgoingThread.Abort();
278                 if (socketClient != null)
279                     socketClient.Close();
280             }
281             catch (Exception ex)
282             {
283             }
284 
285             this.btnSend.Enabled = true;
286         }
287 
288         private void ClientForm_FormClosing(object sender, FormClosingEventArgs e)
289         {
290 
291             SocketInfo.ServerIp = this.txtIP.Text;
292             try
293             {
294                 SocketInfo.Port = int.Parse(this.txtPort.Text);
295             }
296             catch (System.Exception ex)
297             {
298 
299             }
300 
301             SocketInfo.Protocol = rbTcp.Checked ? "Tcp" : "Udp";
302             SocketInfo.Format = rbAscII.Checked ? "AscII" : "Hex";
303             SocketInfo.Type = "Client";
304             SocketInfo.ServerIp = this.txtIP.Text;
305             SocketInfo.Data = this.rtSendData.Text;
306 
307             SocketInfo.IsAuto = cbAutoSend.Checked;
308             try
309             {
310                 SocketInfo.Port = int.Parse(this.txtPort.Text);
311             }
312             catch (Exception ex)
313             {
314                 //errorMsg += "请输入合法的端口";
315             }
316             /*
317             continueSend = false;
318             try
319             {
320                 HeartOutgoingThread.Abort();
321                 SendOutgoingThread.Abort();
322                 if (socketClient != null)
323                     socketClient.Close();
324             }
325             catch (Exception ex)
326             {
327             }
328 
329             this.btnSend.Enabled = true;
330              */
331 
332         }
333 
334         private void btnClearLog_Click(object sender, EventArgs e)
335         {
336             this.PacketView.Clear();
337         }
338 
339         private void ClientForm_Load(object sender, EventArgs e)
340         {
341             this.txtIP.Text = SocketInfo.ServerIp;
342             rbTcp.Checked = SocketInfo.Protocol == "Tcp";
343             rbUdp.Checked = SocketInfo.Protocol != "Tcp";
344             rbAscII.Checked = SocketInfo.Format == "AscII";
345             rbHex.Checked = SocketInfo.Format != "AscII";
346             this.txtPort.Text = "" + SocketInfo.Port;
347             this.rtSendData.Text = SocketInfo.Data;
348 
349             cbAutoSend.Checked = SocketInfo.IsAuto;
350 
351         }
352 
353         private void btnOpenLog_Click(object sender, EventArgs e)
354         {
355             Process.Start("notepad.exe", "client.log");
356         }
357 
358         private void panel1_Paint(object sender, PaintEventArgs e)
359         {
360 
361         }
362     }
363 }
View Code

(三) 服务器端

技术分享
  1 using System;
  2 using System.Collections.Generic;
  3 using System.ComponentModel;
  4 using System.Data;
  5 using System.Drawing;
  6 using System.Text;
  7 using System.Windows.Forms;
  8 using SocketTool.Core;
  9 using System.Threading;
 10 using System.Diagnostics;
 11 
 12 namespace SocketTool
 13 {
 14     public partial class ServerForm : Form, ISocketInfo
 15     {
 16         public ServerForm()
 17         {
 18             InitializeComponent();
 19             SocketInfo = new SocketInfo();
 20 
 21         }
 22 
 23         //private static log4net.ILog logger = log4net.LogManager.GetLogger(typeof(ServerForm));  
 24         private IServer commServer = new CommTcpServer();
 25 
 26         private Thread refreshThread;
 27 
 28         private Boolean continueRefresh = true;
 29 
 30         private List<IConnection> conns = new List<IConnection>();
 31 
 32         public SocketInfo SocketInfo { get; set; }
 33 
 34         private void btnSend_Click(object sender, EventArgs e)
 35         {
 36         }
 37 
 38         private void btnListen_Click(object sender, EventArgs e)
 39         {
 40 
 41             if (rbUdp.Checked)
 42                 commServer = new CommUdpServer();
 43             int port = int.Parse(this.txtPort.Text);
 44 
 45             commServer.Init(null, port);
 46             commServer.OnDataReceived += new ReceivedHandler(ListenMessage);
 47             commServer.OnSocketError += new SocketErrorHandler(ListenErrorMessage);
 48 
 49             //refreshThread = new Thread(new ThreadStart(RefreshConnection));
 50             if(refreshConnectionWorker.IsBusy == false)
 51                refreshConnectionWorker.RunWorkerAsync();
 52             try
 53             {
 54                 commServer.Listen();
 55                 btnListen.Enabled = false;
 56                 btnStopListen.Enabled = true;
 57                 ListenMessage(0, "", "启动监听成功,端口:" + port);
 58             }
 59             catch (Exception ex)
 60             {
 61                 MessageBox.Show("监听失败,端口可能被占用");
 62             }
 63         }
 64 
 65         public void ListenErrorMessage(object o, SocketEventArgs e)
 66         {
 67             string errorMsg = "[" + e.ErrorCode + "]" + SocketUtil.DescrError(e.ErrorCode);
 68 
 69             ListenMessage(o, "Socket错误", errorMsg);
 70 
 71         }
 72         private void ListenMessage(object ID, string type, string msg)
 73         {
 74             if (PacketView.InvokeRequired)
 75             {
 76                 try
 77                 {
 78                     MsgHandler d = new MsgHandler(ListenMessage);
 79                     this.Invoke(d, new object[] { ID,type, msg });
 80                 }
 81                 catch (System.Exception ex)
 82                 {
 83                     //logger.Error(ex.Message);
 84                     //logger.Error(ex.StackTrace);
 85                 }
 86             }
 87             else
 88             {
 89                 if (PacketView.Items.Count > 200)
 90                     PacketView.Items.Clear();
 91 
 92 
 93                 ListViewItem item = PacketView.Items.Insert(0, "" + PacketView.Items.Count);
 94 
 95                 item.SubItems.Add("" + ID);
 96                 item.SubItems.Add("");
 97                 //int length = e.Data.Length;
 98                 string strDate = DateTime.Now.ToString("HH:mm:ss");
 99                 item.SubItems.Add(strDate);
100                 item.SubItems.Add(msg);
101                 //item.SubItems.Add("" + length);
102             }
103         }
104 
105         public void ListenMessage(object o, ReceivedEventArgs e)
106         {
107             if (PacketView.InvokeRequired)
108             {
109                 
110                     ReceivedHandler d = new ReceivedHandler(ListenMessage);
111                     this.Invoke(d, new object[] { o, e});
112             }
113             else
114             {
115                 byte[] data = e.Data;
116                 int length = data.Length;
117                 Boolean isEcho = this.cbAutoSend.Checked;
118                 string connId = "" + o;
119                 if (isEcho)
120                 {
121                     commServer.Send(connId, data, data.Length);
122                 }
123 
124                 
125                 if (PacketView.Items.Count > 200)
126                     PacketView.Items.Clear();
127 
128                 ListViewItem item = PacketView.Items.Insert(0, "" + PacketView.Items.Count);
129                 item.SubItems.Add("" + connId);
130                 item.SubItems.Add("" + e.RemoteHost.ToString());
131 
132                 string msg = ParseUtil.ParseString(data, length);
133                 if (rbHex.Checked)
134                 {
135                     msg = ParseUtil.ToHexString(data, length);
136                 }
137 
138                 string strDate = DateTime.Now.ToString("HH:mm:ss");
139                 item.SubItems.Add(strDate);
140 
141                 item.SubItems.Add(msg);
142                 item.SubItems.Add("" + length);
143                 if (cbLog.Checked)
144                 {
145                     //logger.Info(e.RemoteHost.ToString() + " " + msg);
146                 }
147                 //item.SubItems.Add("" + msg.MsgContentDesc);
148             }
149         }
150 
151         private void btnStopListen_Click(object sender, EventArgs e)
152         {
153             commServer.Close();
154 
155             btnListen.Enabled = true;
156             btnStopListen.Enabled = false;
157         }
158 
159         private void ServerForm_FormClosing(object sender, FormClosingEventArgs e)
160         {
161 
162             SocketInfo.ServerIp = this.txtIP.Text;
163             try
164             {
165                 SocketInfo.Port = int.Parse(this.txtPort.Text);
166             }
167             catch (System.Exception ex)
168             {
169                 
170             }
171             SocketInfo.Protocol = rbTcp.Checked ? "Tcp" : "Udp";
172             SocketInfo.Format = rbAscII.Checked ? "AscII" : "Hex";
173             SocketInfo.Type = "Server";
174             SocketInfo.Data = this.rtbData.Text;
175             SocketInfo.IsAuto = cbAutoSend.Checked;
176             refreshConnectionWorker.CancelAsync();
177             commServer.Close();
178         }
179 
180         private void btnClearLog_Click(object sender, EventArgs e)
181         {
182             this.PacketView.Items.Clear();
183         }
184 
185         private void refreshConnectionWorker_DoWork(object sender, DoWorkEventArgs e)
186         {
187              int m = 0;
188              while (continueRefresh)
189              {
190                  conns = commServer.GetConnectionList();
191                  refreshConnectionWorker.ReportProgress(m);
192 
193                  Thread.Sleep(1000);
194              }
195         }
196 
197         private void refreshConnectionWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
198         {
199             try
200             {
201 
202                 connectionView.Items.Clear();
203                 foreach (IConnection ic in conns)
204                 {
205 
206                     ListViewItem item = connectionView.Items.Insert(0, "" + connectionView.Items.Count);
207                     item.Tag = ic.ID;
208                     item.SubItems.Add("" + ic.ID);
209                     //int length = e.Data.Length;
210                     string strDate = DateTime.Now.ToString("dd HH:mm:ss");
211                     item.SubItems.Add(ic.CreateDate.ToString("dd HH:mm:ss"));
212                     item.SubItems.Add(ic.ClientIP.ToString());
213                     item.SubItems.Add(ic.OnlineDate.ToString("dd HH:mm:ss"));
214 
215                 }
216             }
217             catch (System.Exception ex)
218             {
219                 //logger.Error(ex.Message);
220                 //logger.Error(ex.StackTrace);
221             }
222         }
223 
224         private string selectedConnectionID;
225         private void connectionView_SelectedIndexChanged(object sender, EventArgs e)
226         {
227             foreach (ListViewItem item in connectionView.SelectedItems)
228             {
229                 selectedConnectionID = ""+item.Tag;
230 
231                 txtConn.Text = item.SubItems[3].Text;
232             }
233             
234         }
235 
236         private void PacketView_SelectedIndexChanged(object sender, EventArgs e)
237         {
238             foreach (ListViewItem item in PacketView.SelectedItems)
239             {
240                 selectedConnectionID = ""+item.Tag;
241                 rtbData.Text = item.SubItems[4].Text;
242             }
243         }
244 
245         private void ServerForm_Load(object sender, EventArgs e)
246         {
247             this.txtIP.Text = SocketInfo.ServerIp;
248             rbTcp.Checked = SocketInfo.Protocol == "Tcp";
249             rbUdp.Checked = SocketInfo.Protocol != "Tcp";
250             rbAscII.Checked = SocketInfo.Format == "AscII";
251             rbHex.Checked = SocketInfo.Format != "AscII";
252             this.txtPort.Text = "" + SocketInfo.Port;
253 
254             cbAutoSend.Checked = SocketInfo.IsAuto;
255             this.rtbData.Text = SocketInfo.Data;
256         }
257 
258         private void btnOpenLog_Click(object sender, EventArgs e)
259         {
260             Process.Start("notepad.exe", "server.log");
261         }
262 
263     }
264 }
View Code

 

Socket测试工具包的开发(TCP UDP)

标签:

原文地址:http://www.cnblogs.com/jiangyongyawen/p/4386781.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!