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

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 }

(二) 客户端窗体

  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 }

(三) 服务器端

  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 }

时间: 2024-11-12 16:18:39

Socket测试工具包的开发(TCP UDP)的相关文章

Socket(套接字) IP TCP UDP HTTP

Socket(套接字) (转)什么是套接字(Socket)? 应用层通过传输层进行数据通信时,TCP和UDP会遇到同时为多个应用程序进程提供并发服务的问题.多个TCP连接或多个应用程序进程可能需要 通过同一个TCP协议端口传输数据.为了区别不同的应用程序进程和连接,许多计算机操作系统为应用程序与TCP/IP协议交互提供了称为套接字 (Socket)(socket是操作系统提供出来的接口)的接口,区分不同应用程序进程间的网络通信和连接.生成套接字,主要有3个参数:通信的目的IP地址.使用的传输 层

【转】Windows Socket TCP/UDP

Windows Socket编程,发现这篇文章不错,就拿过来分享下,转载地址:http://www.cnblogs.com/fantasy-blog/archive/2013/04/21/3033935.html SOCKET网络编程 (WINDOWS SOCKET) 1.前言 网上看了很多Socket的资料,将理解的知识总结下,详细介绍下VC下windows sockets编程,并结合服务器和客户端的两个实例(TCP/UDP)讲解下. 2.SOCKET相关原理 在网络编程中最常用的方案便是Cl

Photon服务器引擎(二)socket/TCP/UDP基础及Unity聊天室的实现

Photon服务器引擎(二)socket/TCP/UDP基础及Unity聊天室的实现 我们平时说的最多的socket是什么呢,实际上socket是对TCP/IP协议的封装,Socket本身并不是协议,而是一个调用接口(API). 通过Socket,我们才能使用TCP/IP协议.实际上,Socket跟TCP/IP协议没有必然的联系.Socket编程接口在设计的时候,就希望也能适应其他的网络协议.所以说,Socket的出现只是使得程序员更方便地使用TCP/IP协议栈而已,是对TCP/IP协议的抽象,

PCATTCP使用笔记——TCP UDP速度测试工具

0.PCATTCP简介 PCATTCP的前身为Test TCP,Test TCP是BSD操作系统的原生工具,该工具通过控制台输入参数,用于测试TCP或者UDP的通信速度.该项目于1984年启动,现在该工具的源代码早已开放.PCATTCP是Test TCP的windows移植版本,是一个用于测试TCP和UDP通信速度的windows控制台程序. 1.PCTATCP的安装 [1]下载可执行文件和源代码包,下载网址 [2]解压可执行文件到某文件件,例如D:\tools [3]把D:\tools加入环境

How To: Perl TCP / UDP Socket Programming using IO::Socket::INET

http://www.thegeekstuff.com/2010/07/perl-tcp-udp-socket-programming/ In this article, let us discuss how to write Perl socket programming using the inbuilt socket modules in Perl. Perl socket modules provides an object interface that makes it easier

TCP,UDP,HTTP,IP,SOCKET

近日对各网络协议进行了一番学习,宏观认识上有收获. 网络由下往上分为物理层.数据链路层.网络层.传输层.会话层.表示层和应用层.(引用)IP 协议对应于网络层,TCP/UDP协议对应于传输层, HTTP协议对应于应用层, SOCKET则是对TCP/IP协议的封装和应用. TCP连接的三次握手:第一次握手:客户端发送syn包(syn=j)到服务器,并进入SYN_SEND状态,等待服务器确认: 第二次握手:服务器收到syn包,必须确认客户的SYN(ack=j+1),同时自己也发送一个SYN包(syn

Java TCP/UDP socket 编程流程总结

最近正好学习了一点用java socket编程的东西.感觉整体的流程虽然不是很繁琐,但是也值得好好总结一下. Socket Socket可以说是一种针对网络的抽象,应用通过它可以来针对网络读写数据.就像通过一个文件的file handler就可以都写数据到存储设备上一样.根据TCP协议和UDP协议的不同,在网络编程方面就有面向两个协议的不同socket,一个是面向字节流的一个是面向报文的. 对socket的本身组成倒是比较好理解.既然是应用通过socket通信,肯定就有一个服务器端和一个客户端.

HTTP,FTP,TCP,UDP及SOCKET

一.TCP/IP协议简析TCP/IP是个协议组,可分为三个层次:网络层.传输层和应用层:网络层:IP协议.ICMP协议.ARP协议.RARP协议和BOOTP协议传输层:TCP协议与UDP协议应用层:FTP.HTTP.TELNET.SMTP.DNS等协议 HTTP是应用层协议,其传输都是被包装成TCP协议传输.可以用SOCKET实现HTTP.SOCKET是实现传输层协议的一种编程API,可以是TCP,也可以是UDP. 二.Socket连接与HTTP连接区别[Socket]由于通常情况下Socket

Socket 通信原理(Android客户端和服务器以TCP&amp;&amp;UDP方式互通)

ZERO.前言 有关通信原理内容是在网上或百科整理得到,代码部分为本人所写,如果不当,还望指教. 一.Socket通信简介 Android与服务器的通信方式主要有两种,一是Http通信,一是Socket通信.两者的最大差异在于,http连接使用的是“请求—响应方式”,即在请求时建立连接通道,当客户端向服务器发送请求后,服务器端才能向客户端返回数据.而Socket通信则是在双方建立起连接后就可以直接进行数据的传输,在连接时可实现信息的主动推送,而不需要每次由客户端想服务器发送请求. 那么,什么是s