Ver código fonte

基本已完成!!!

Kerry 8 anos atrás
pai
commit
a60af1d50a

BIN
.vs/GWSocketClient/v15/.suo Ver arquivo


+ 6 - 4
GWSocketClient/Form1.Designer.cs Ver arquivo

199
             this.btnPressureTest.TabIndex = 10;
199
             this.btnPressureTest.TabIndex = 10;
200
             this.btnPressureTest.Text = "压力测试";
200
             this.btnPressureTest.Text = "压力测试";
201
             this.btnPressureTest.UseVisualStyleBackColor = true;
201
             this.btnPressureTest.UseVisualStyleBackColor = true;
202
+            this.btnPressureTest.Visible = false;
202
             this.btnPressureTest.Click += new System.EventHandler(this.btnPressureTest_Click);
203
             this.btnPressureTest.Click += new System.EventHandler(this.btnPressureTest_Click);
203
             // 
204
             // 
204
             // label4
205
             // label4
209
             this.label4.Size = new System.Drawing.Size(56, 17);
210
             this.label4.Size = new System.Drawing.Size(56, 17);
210
             this.label4.TabIndex = 9;
211
             this.label4.TabIndex = 9;
211
             this.label4.Text = "压力测试";
212
             this.label4.Text = "压力测试";
213
+            this.label4.Visible = false;
212
             // 
214
             // 
213
             // tbPresureTestCount
215
             // tbPresureTestCount
214
             // 
216
             // 
219
             this.tbPresureTestCount.Size = new System.Drawing.Size(65, 23);
221
             this.tbPresureTestCount.Size = new System.Drawing.Size(65, 23);
220
             this.tbPresureTestCount.TabIndex = 8;
222
             this.tbPresureTestCount.TabIndex = 8;
221
             this.tbPresureTestCount.Text = "100";
223
             this.tbPresureTestCount.Text = "100";
224
+            this.tbPresureTestCount.Visible = false;
222
             // 
225
             // 
223
             // btnSendData
226
             // btnSendData
224
             // 
227
             // 
228
+            this.btnSendData.Enabled = false;
225
             this.btnSendData.Location = new System.Drawing.Point(422, 61);
229
             this.btnSendData.Location = new System.Drawing.Point(422, 61);
226
             this.btnSendData.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
230
             this.btnSendData.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
227
             this.btnSendData.Name = "btnSendData";
231
             this.btnSendData.Name = "btnSendData";
233
             // 
237
             // 
234
             // btnQuickLogout
238
             // btnQuickLogout
235
             // 
239
             // 
240
+            this.btnQuickLogout.Enabled = false;
236
             this.btnQuickLogout.Location = new System.Drawing.Point(103, 61);
241
             this.btnQuickLogout.Location = new System.Drawing.Point(103, 61);
237
             this.btnQuickLogout.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
242
             this.btnQuickLogout.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
238
             this.btnQuickLogout.Name = "btnQuickLogout";
243
             this.btnQuickLogout.Name = "btnQuickLogout";
300
             this.tbIpaddress.Name = "tbIpaddress";
305
             this.tbIpaddress.Name = "tbIpaddress";
301
             this.tbIpaddress.Size = new System.Drawing.Size(252, 23);
306
             this.tbIpaddress.Size = new System.Drawing.Size(252, 23);
302
             this.tbIpaddress.TabIndex = 1;
307
             this.tbIpaddress.TabIndex = 1;
303
-            this.tbIpaddress.Text = "127.0.0.1";
308
+            this.tbIpaddress.Text = "120.77.50.131";
304
             // 
309
             // 
305
             // label1
310
             // label1
306
             // 
311
             // 
354
             // 
359
             // 
355
             // ToolStripMenuItemHbStart
360
             // ToolStripMenuItemHbStart
356
             // 
361
             // 
357
-            this.ToolStripMenuItemHbStart.Checked = true;
358
-            this.ToolStripMenuItemHbStart.CheckState = System.Windows.Forms.CheckState.Checked;
359
             this.ToolStripMenuItemHbStart.Name = "ToolStripMenuItemHbStart";
362
             this.ToolStripMenuItemHbStart.Name = "ToolStripMenuItemHbStart";
360
             this.ToolStripMenuItemHbStart.Size = new System.Drawing.Size(172, 22);
363
             this.ToolStripMenuItemHbStart.Size = new System.Drawing.Size(172, 22);
361
             this.ToolStripMenuItemHbStart.Text = "登录成功就开启";
364
             this.ToolStripMenuItemHbStart.Text = "登录成功就开启";
362
-            this.ToolStripMenuItemHbStart.Click += new System.EventHandler(this.ToolStripMenuItemHbStart_Click);
363
             // 
365
             // 
364
             // toolStripSeparator1
366
             // toolStripSeparator1
365
             // 
367
             // 

+ 104 - 56
GWSocketClient/Form1.cs Ver arquivo

23
 
23
 
24
 namespace GWSocketClient
24
 namespace GWSocketClient
25
 {
25
 {
26
+    delegate void ThreadFormDelegate(object msg);
27
+
26
     public partial class Form1 : Form
28
     public partial class Form1 : Form
27
     {
29
     {
28
         public Form1()
30
         public Form1()
34
         private string port;
36
         private string port;
35
         private SocketHelper.TcpClients sockeTcpClients;
37
         private SocketHelper.TcpClients sockeTcpClients;
36
         private VcarecityPD vcarecityProtocolBean = null;
38
         private VcarecityPD vcarecityProtocolBean = null;
39
+        private volatile bool hbRunFlag = false; //心跳线程标志
40
+        private ThreadFormDelegate uiDelegate;
37
 
41
 
38
-        private int heartBeatInterval = 5; //Default 心跳间隔
39
-        private bool isStartHeartbeat = true;
40
 
42
 
41
         private void Form1_Load(object sender, EventArgs e)
43
         private void Form1_Load(object sender, EventArgs e)
42
         {
44
         {
52
             sockeTcpClients = new SocketHelper.TcpClients();
54
             sockeTcpClients = new SocketHelper.TcpClients();
53
             ip = tbIpaddress.Text.Trim();
55
             ip = tbIpaddress.Text.Trim();
54
             port = tbPort.Text.Trim();
56
             port = tbPort.Text.Trim();
55
-            isStartHeartbeat = ToolStripMenuItemHbStart.Checked;
57
+
58
+            uiDelegate = addToListboxItem;
56
         }
59
         }
57
 
60
 
58
 
61
 
59
         #region 处理心跳相关的
62
         #region 处理心跳相关的
60
 
63
 
61
-        private void startHeartBeat()
64
+        //开启心跳连接
65
+        private void startHeartBeatThread()
62
         {
66
         {
63
             Thread thread = new Thread(heartBeatThread);
67
             Thread thread = new Thread(heartBeatThread);
64
-            thread.Start();
65
-            
68
+            thread.Start(this);
66
         }
69
         }
67
 
70
 
68
-        private void heartBeatThread()
71
+        private void heartBeatThread(object o)
69
         {
72
         {
70
-            while (isStartHeartbeat)
73
+            while (hbRunFlag)
71
             {
74
             {
72
-                int interval = int.Parse(toolStripTextBoxHbtime.Text.Trim());
75
+            
76
+                int interval = 5;
77
+                if (!toolStripTextBoxHbtime.Text.Trim().Equals(""))
78
+                {
79
+                    interval = int.Parse(toolStripTextBoxHbtime.Text.Trim());
80
+                }
73
                 Thread.Sleep(interval * 1000);
81
                 Thread.Sleep(interval * 1000);
74
-                string hbCmd = vcarecityProtocolBean.ProtocolStartSymbol;
75
-                hbCmd += tbEquipmentAddress.Text.Trim() + "840000";
76
-                hbCmd = Utils.calcCrcCode(hbCmd);
77
-                byte[] ts = HexUtil.hexToBytes(hbCmd);
78
-                sockeTcpClients.SendData(ts);
79
 
82
 
80
-                if (ToolStripMenuItemLogHb.Checked)
83
+          
84
+                if (hbRunFlag)
81
                 {
85
                 {
82
-                    Console.WriteLine("发送到服务端-心跳:[{0}]", hbCmd);
83
-                    addToListboxItem(string.Format("发送到服务端-心跳:[{0}]", hbCmd));
86
+                    string hbCmd = vcarecityProtocolBean.ProtocolStartSymbol;
87
+                    hbCmd += tbEquipmentAddress.Text.Trim() + "840000";
88
+                    hbCmd = Utils.calcCrcCode(hbCmd);
89
+                    byte[] ts = HexUtil.hexToBytes(hbCmd);
90
+
91
+                    sockeTcpClients.SendData(ts);
92
+
93
+                    if (ToolStripMenuItemLogHb.Checked)
94
+                    {
95
+                        this.Invoke(this.uiDelegate, new object[] {string.Format("发送到服务端-心跳:[{0}]", hbCmd)});
96
+                    }
84
                 }
97
                 }
85
             }
98
             }
86
-            addToListboxItem("心跳线程结束!");
99
+            this.Invoke(this.uiDelegate, new object[] {"心跳线程结束!"});
87
         }
100
         }
88
 
101
 
89
         #endregion
102
         #endregion
107
                     switch (sks.ErrorCode)
120
                     switch (sks.ErrorCode)
108
                     {
121
                     {
109
                         case SocketHelper.Sockets.ErrorCodes.objectNull:
122
                         case SocketHelper.Sockets.ErrorCodes.objectNull:
123
+                            addToListboxItem("状态信息:服务器连接已断开");
124
+                            hbRunFlag = false;
125
+                            checkLogin(false);
110
                             break;
126
                             break;
111
                         case SocketHelper.Sockets.ErrorCodes.ConnectError:
127
                         case SocketHelper.Sockets.ErrorCodes.ConnectError:
128
+                            //连接不成功
129
+                            addToListboxItem("状态信息:连接服务器失败,可能是服务器未开启!");
112
                             break;
130
                             break;
113
                         case SocketHelper.Sockets.ErrorCodes.ConnectSuccess:
131
                         case SocketHelper.Sockets.ErrorCodes.ConnectSuccess:
114
-                            addToListboxItem("状态信息:连接成功.!");
132
+                            //连接成功
133
+                            addToListboxItem("状态信息:与服务器连接成功");
134
+                            addToListboxItem(string.Format("客户端信息{0}", sks.ex));
135
+                            checkLogin(true);
136
+                            sendLoginCmd();
137
+                            hbRunFlag = true;
138
+                            startHeartBeatThread();
115
                             break;
139
                             break;
116
                         case SocketHelper.Sockets.ErrorCodes.TrySendData:
140
                         case SocketHelper.Sockets.ErrorCodes.TrySendData:
141
+                            addToListboxItem("状态信息:ErrorCodes");
117
                             break;
142
                             break;
118
                         default:
143
                         default:
144
+                            addToListboxItem(string.Format("客户端信息{0}", sks.ex));
119
                             break;
145
                             break;
120
                     }
146
                     }
121
-                    addToListboxItem(string.Format("客户端信息{0}", sks.ex));
122
-                    //连接成功,启动心跳线程
123
-                    //startHeartBeat();
124
                 }
147
                 }
125
                 else
148
                 else
126
                 {
149
                 {
127
                     byte[] buffer = new byte[sks.Offset];
150
                     byte[] buffer = new byte[sks.Offset];
128
                     Array.Copy(sks.RecBuffer, buffer, sks.Offset);
151
                     Array.Copy(sks.RecBuffer, buffer, sks.Offset);
129
                     string byteToHexStr = HexUtil.byteToHexStr(buffer);
152
                     string byteToHexStr = HexUtil.byteToHexStr(buffer);
130
-                    if (byteToHexStr.Equals("ServerOff"))
153
+
154
+                    if (!byteToHexStr.Equals(""))
131
                     {
155
                     {
132
-                        addToListboxItem("状态信息:服务端主动关闭!");
156
+                        handleResponse(sks, byteToHexStr);
133
                     }
157
                     }
134
                     else
158
                     else
135
                     {
159
                     {
136
-                        if (!byteToHexStr.Equals(""))
137
-                        {
138
-                            addToListboxItem(string.Format("收到服务端{0}发来消息:[{1}]", sks.Ip, byteToHexStr));
139
-                            handleResponse(sks, byteToHexStr);
140
-                        }
141
-                        else
142
-                        {
143
-                            isStartHeartbeat = false;
144
-                            addToListboxItem("状态信息:服务端主动关闭!");
145
-                            sockeTcpClients.Stop();
146
-                        }
147
-                        //                    listBoxShow.Text += "\r\n";
148
-                        //                    listBoxShow.Text += string.Format("服务端{0}发来消息:{1}", sks.Ip, byteToHexStr);
160
+                        addToListboxItem("状态信息:服务端主动关闭! position");
161
+                        sockeTcpClients.Stop();
149
                     }
162
                     }
163
+                    //                    listBoxShow.Text += "\r\n";
164
+                    //                    listBoxShow.Text += string.Format("服务端{0}发来消息:{1}", sks.Ip, byteToHexStr);
150
                 }
165
                 }
151
             }));
166
             }));
152
         }
167
         }
153
 
168
 
154
-        //处理回复信息
169
+        /// <summary>
170
+        /// 处理回复信息
171
+        /// </summary>
172
+        /// <param name="sks">Socket对象</param>
173
+        /// <param name="reqData">服务器发过来的信息</param>
155
         private void handleResponse(SocketHelper.Sockets sks, string reqData)
174
         private void handleResponse(SocketHelper.Sockets sks, string reqData)
156
         {
175
         {
157
             //取出原数据功能码
176
             //取出原数据功能码
158
             string functionWord = reqData.Substring(18, 2);
177
             string functionWord = reqData.Substring(18, 2);
159
             int funcode = Utils.hexToTen(functionWord);
178
             int funcode = Utils.hexToTen(functionWord);
160
 
179
 
180
+
181
+            //处理心跳的回复
182
+            if (funcode == 4)
183
+            {
184
+                string hbLen = reqData.Substring(20, 4);
185
+
186
+                if (ToolStripMenuItemLogHb.Checked && hbLen.Equals("0000"))
187
+                {
188
+                    addToListboxItem(string.Format("收到服务端{0}发来心跳:[{1}]", sks.Ip, reqData));
189
+                }
190
+            }
191
+            else
192
+            {
193
+                addToListboxItem(string.Format("收到服务端{0}发来消息:[{1}]", sks.Ip, reqData));
194
+            }
195
+
161
             if (!(funcode == 16 || funcode == 32))
196
             if (!(funcode == 16 || funcode == 32))
162
             {
197
             {
163
                 Console.WriteLine("功能码10进制=" + funcode + ",所以不用回复!");
198
                 Console.WriteLine("功能码10进制=" + funcode + ",所以不用回复!");
256
             tsslEquipmentAddress.Text = equipmentAddress.Length / 2.0 + "";
291
             tsslEquipmentAddress.Text = equipmentAddress.Length / 2.0 + "";
257
         }
292
         }
258
 
293
 
294
+
295
+        private void sendLoginCmd()
296
+        {
297
+            //                string loginCmd = "7e7e7e4442832e833c8200143839383630324231313931353530343739383737f7";
298
+            string loginCmd = vcarecityProtocolBean.ProtocolStartSymbol;
299
+            loginCmd += tbEquipmentAddress.Text.Trim() + "82" +
300
+                        Utils.stringWith0(Utils.tenToHex(tbCCID.Text.Trim().Length / 2), 4);
301
+            loginCmd += tbCCID.Text.Trim();
302
+            loginCmd = Utils.calcCrcCode(loginCmd);
303
+            //发送登录命令
304
+            byte[] ts = HexUtil.hexToBytes(loginCmd);
305
+
306
+            addToListboxItem(string.Format("发送到服务端:[{0}]", loginCmd));
307
+
308
+            sockeTcpClients.SendData(ts);
309
+        }
310
+
259
         private void btnLogin_Click(object sender, EventArgs e)
311
         private void btnLogin_Click(object sender, EventArgs e)
260
         {
312
         {
261
             try
313
             try
268
 
320
 
269
                 sockeTcpClients.InitSocket(ip, int.Parse(port));
321
                 sockeTcpClients.InitSocket(ip, int.Parse(port));
270
                 sockeTcpClients.Start();
322
                 sockeTcpClients.Start();
271
-
272
-                //                string loginCmd = "7e7e7e4442832e833c8200143839383630324231313931353530343739383737f7";
273
-                string loginCmd = vcarecityProtocolBean.ProtocolStartSymbol;
274
-                loginCmd += tbEquipmentAddress.Text.Trim() + "82" +
275
-                            Utils.stringWith0(Utils.tenToHex(tbCCID.Text.Trim().Length / 2), 4);
276
-                loginCmd += tbCCID.Text.Trim();
277
-                loginCmd = Utils.calcCrcCode(loginCmd);
278
-                //发送登录命令
279
-                byte[] ts = HexUtil.hexToBytes(loginCmd);
280
-
281
-                addToListboxItem(string.Format("发送到服务端:[{0}]", loginCmd));
282
-
283
-                sockeTcpClients.SendData(ts);
284
             }
323
             }
285
             catch (Exception ex)
324
             catch (Exception ex)
286
             {
325
             {
299
 
338
 
300
         private void btnQuickLogout_Click(object sender, EventArgs e)
339
         private void btnQuickLogout_Click(object sender, EventArgs e)
301
         {
340
         {
302
-            isStartHeartbeat = false;
303
             try
341
             try
304
             {
342
             {
305
                 sockeTcpClients.Stop();
343
                 sockeTcpClients.Stop();
525
             ToolStripMenuItemLogHb.Checked = !ToolStripMenuItemLogHb.Checked;
563
             ToolStripMenuItemLogHb.Checked = !ToolStripMenuItemLogHb.Checked;
526
         }
564
         }
527
 
565
 
528
-        private void ToolStripMenuItemHbStart_Click(object sender, EventArgs e)
566
+
567
+        private void checkLogin(bool isLogin)
529
         {
568
         {
530
-            ToolStripMenuItemHbStart.Checked = !ToolStripMenuItemHbStart.Checked;
569
+            if (isLogin)
570
+            {
571
+                btnLogin.Enabled = false;
572
+                btnQuickLogout.Enabled = true;
573
+                btnSendData.Enabled = true;
574
+            }
575
+            else
576
+            {
577
+                btnLogin.Enabled = true;
578
+                btnQuickLogout.Enabled = false;
579
+                btnSendData.Enabled = false;
580
+            }
531
         }
581
         }
532
-
533
-       
534
     }
582
     }
535
 }
583
 }

+ 46 - 0
GWSocketClient/InfoEditor.Designer.cs Ver arquivo

40
             this.btnLoadFromExcel = new System.Windows.Forms.Button();
40
             this.btnLoadFromExcel = new System.Windows.Forms.Button();
41
             this.InfoEditorTip = new System.Windows.Forms.RichTextBox();
41
             this.InfoEditorTip = new System.Windows.Forms.RichTextBox();
42
             this.btnClearAll = new System.Windows.Forms.Button();
42
             this.btnClearAll = new System.Windows.Forms.Button();
43
+            this.cbItemType = new System.Windows.Forms.ComboBox();
44
+            this.tbInputData = new System.Windows.Forms.TextBox();
45
+            this.btnQueryItem = new System.Windows.Forms.Button();
43
             ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
46
             ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
44
             this.contextMenuStrip1.SuspendLayout();
47
             this.contextMenuStrip1.SuspendLayout();
45
             this.SuspendLayout();
48
             this.SuspendLayout();
169
             this.btnClearAll.UseVisualStyleBackColor = true;
172
             this.btnClearAll.UseVisualStyleBackColor = true;
170
             this.btnClearAll.Click += new System.EventHandler(this.btnClearAll_Click);
173
             this.btnClearAll.Click += new System.EventHandler(this.btnClearAll_Click);
171
             // 
174
             // 
175
+            // cbItemType
176
+            // 
177
+            this.cbItemType.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
178
+            this.cbItemType.Font = new System.Drawing.Font("宋体", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
179
+            this.cbItemType.FormattingEnabled = true;
180
+            this.cbItemType.ItemHeight = 13;
181
+            this.cbItemType.Items.AddRange(new object[] {
182
+            "ID-10进制",
183
+            "ID-16进制",
184
+            "名字-IDInfo",
185
+            "描述-Desp"});
186
+            this.cbItemType.Location = new System.Drawing.Point(657, 39);
187
+            this.cbItemType.Name = "cbItemType";
188
+            this.cbItemType.Size = new System.Drawing.Size(92, 21);
189
+            this.cbItemType.TabIndex = 12;
190
+            // 
191
+            // tbInputData
192
+            // 
193
+            this.tbInputData.Font = new System.Drawing.Font("宋体", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
194
+            this.tbInputData.Location = new System.Drawing.Point(755, 38);
195
+            this.tbInputData.Name = "tbInputData";
196
+            this.tbInputData.Size = new System.Drawing.Size(100, 22);
197
+            this.tbInputData.TabIndex = 13;
198
+            // 
199
+            // btnQueryItem
200
+            // 
201
+            this.btnQueryItem.DialogResult = System.Windows.Forms.DialogResult.Cancel;
202
+            this.btnQueryItem.Font = new System.Drawing.Font("微软雅黑", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
203
+            this.btnQueryItem.Location = new System.Drawing.Point(861, 34);
204
+            this.btnQueryItem.Name = "btnQueryItem";
205
+            this.btnQueryItem.Size = new System.Drawing.Size(75, 30);
206
+            this.btnQueryItem.TabIndex = 14;
207
+            this.btnQueryItem.Text = "查找数据";
208
+            this.btnQueryItem.UseVisualStyleBackColor = true;
209
+            this.btnQueryItem.Click += new System.EventHandler(this.btnQueryItem_Click);
210
+            // 
172
             // InfoEditor
211
             // InfoEditor
173
             // 
212
             // 
174
             this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
213
             this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
175
             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
214
             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
176
             this.CancelButton = this.btnClosedThis;
215
             this.CancelButton = this.btnClosedThis;
177
             this.ClientSize = new System.Drawing.Size(1241, 761);
216
             this.ClientSize = new System.Drawing.Size(1241, 761);
217
+            this.Controls.Add(this.btnQueryItem);
218
+            this.Controls.Add(this.tbInputData);
219
+            this.Controls.Add(this.cbItemType);
178
             this.Controls.Add(this.btnClearAll);
220
             this.Controls.Add(this.btnClearAll);
179
             this.Controls.Add(this.InfoEditorTip);
221
             this.Controls.Add(this.InfoEditorTip);
180
             this.Controls.Add(this.btnLoadFromExcel);
222
             this.Controls.Add(this.btnLoadFromExcel);
192
             ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
234
             ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
193
             this.contextMenuStrip1.ResumeLayout(false);
235
             this.contextMenuStrip1.ResumeLayout(false);
194
             this.ResumeLayout(false);
236
             this.ResumeLayout(false);
237
+            this.PerformLayout();
195
 
238
 
196
         }
239
         }
197
 
240
 
208
         private System.Windows.Forms.Button btnLoadFromExcel;
251
         private System.Windows.Forms.Button btnLoadFromExcel;
209
         private System.Windows.Forms.RichTextBox InfoEditorTip;
252
         private System.Windows.Forms.RichTextBox InfoEditorTip;
210
         private System.Windows.Forms.Button btnClearAll;
253
         private System.Windows.Forms.Button btnClearAll;
254
+        private System.Windows.Forms.ComboBox cbItemType;
255
+        private System.Windows.Forms.TextBox tbInputData;
256
+        private System.Windows.Forms.Button btnQueryItem;
211
     }
257
     }
212
 }
258
 }

+ 10 - 0
GWSocketClient/InfoEditor.cs Ver arquivo

26
         private void InfoEditor_Load(object sender, EventArgs e)
26
         private void InfoEditor_Load(object sender, EventArgs e)
27
         {
27
         {
28
             dataGridView1.DataSource = AccsessDbLoader.getInstance().getDataTable();
28
             dataGridView1.DataSource = AccsessDbLoader.getInstance().getDataTable();
29
+            cbItemType.SelectedIndex = 0;
29
         }
30
         }
30
 
31
 
31
 
32
 
157
             }
158
             }
158
             this.DialogResult = DialogResult.None;
159
             this.DialogResult = DialogResult.None;
159
         }
160
         }
161
+
162
+        private void btnQueryItem_Click(object sender, EventArgs e)
163
+        {
164
+            int mType = cbItemType.SelectedIndex;
165
+            dataGridView1.DataSource = AccsessDbLoader.getInstance().findDataByType(mType, tbInputData.Text.Trim())
166
+                .getDataTable();
167
+
168
+            this.DialogResult = DialogResult.None;
169
+        }
160
     }
170
     }
161
 }
171
 }

BIN
GWSocketClient/bin/Debug/dbfile/ID_INFO.accdb Ver arquivo


+ 88 - 51
GWSocketClient/db/AccsessDbLoader.cs Ver arquivo

75
             objConnection.Close();
75
             objConnection.Close();
76
         }
76
         }
77
 
77
 
78
-        public AccsessDbLoader reloadAccessDb()
78
+        public AccsessDbLoader reloadAccessDb(string sql = @"select * from fw")
79
         {
79
         {
80
+            Console.WriteLine(sql);
80
             try
81
             try
81
             {
82
             {
82
-                OleDbCommand sqlcmd = new OleDbCommand(@"select * from fw", objConnection);
83
+//                OleDbCommand sqlcmd = new OleDbCommand(@"select * from fw", objConnection);
84
+                OleDbCommand sqlcmd = new OleDbCommand(sql, objConnection);
83
 
85
 
84
                 dataAdapter = new OleDbDataAdapter(sqlcmd);
86
                 dataAdapter = new OleDbDataAdapter(sqlcmd);
85
                 dataTable.Clear();
87
                 dataTable.Clear();
87
 //                wvBeans.Clear();
89
 //                wvBeans.Clear();
88
                 dataAdapter.Fill(dataTable);
90
                 dataAdapter.Fill(dataTable);
89
 
91
 
90
-                using (OleDbDataReader reader = sqlcmd.ExecuteReader())
91
-                {
92
-                    while (reader.Read()) //这个read调用很重要!不写的话运行时将提示找不到数据  
93
-                    {
94
-                        WvBean wb = new WvBean();
95
-                        int id = 0;
96
-                        int index = 0;
97
-                        string myId = "";
98
-                        string idInfo = "";
99
-                        string len = "";
100
-                        string mode = "";
101
-                        string desp = "";
102
-                        string defalutValue = "";
103
-                        string userValue = "";
104
-                        string remark = "";
105
-                        /*try
106
-                        {*/
107
-                        id = reader.GetInt32(0);
108
-                        index = reader.GetInt32(1);
109
-                        myId = reader.GetString(2);
110
-                        idInfo = calcNull(reader.GetString(3));
111
-                        len = calcNull(reader.GetString(4));
112
-                        mode = calcNull(reader.GetString(5));
113
-                        desp = calcNull(reader.GetString(6));
114
-                        defalutValue = calcNull(reader.GetString(7));
115
-                        userValue = calcNull(reader.GetString(8));
116
-                        remark = calcNull(reader.GetString(9));
117
-                        /* }
118
-                         catch (Exception exception)
119
-                         {
120
-                         }*/
121
-
122
-                        wb.id = id;
123
-                        wb.index = index;
124
-                        wb.myId = myId;
125
-                        wb.idInfo = idInfo;
126
-                        wb.len = len;
127
-                        wb.mode = mode;
128
-                        wb.desp = desp;
129
-                        wb.defalutValue = defalutValue;
130
-                        wb.userValue = userValue;
131
-                        wb.remark = remark;
132
-
133
-                        ID_INFO.Add(myId, userValue);
134
-
135
-//                        wvBeans.Add(wb);
136
-                    }
137
-                }
92
+                fillData(sqlcmd);
138
             }
93
             }
139
             catch (OleDbException oe)
94
             catch (OleDbException oe)
140
             {
95
             {
149
             return this;
104
             return this;
150
         }
105
         }
151
 
106
 
107
+        public AccsessDbLoader findDataByType(int mType, string query)
108
+        {
109
+            /*
110
+            ID - 10进制
111
+           ID - 16进制
112
+           名字 - IDInfo
113
+           描述 - Desp
114
+           */
115
+            string sql = "select * from fw ";
116
+
117
+            switch (mType)
118
+            {
119
+                case 0:
120
+                    sql += " where my_index = " + query;
121
+                    break;
122
+                case 1:
123
+                    sql += " where MY_ID = '" + query + "'";
124
+                    break;
125
+                case 2:
126
+                    sql += " where idInfo like '%" + query + "%'";
127
+                    break;
128
+                case 3:
129
+                    sql += " where desp like '%" + query + "%'";
130
+                    break;
131
+                default:
132
+                    return reloadAccessDb();
133
+            }
134
+            return reloadAccessDb(sql);
135
+        }
136
+
137
+        private void fillData(OleDbCommand sqlcmd)
138
+        {
139
+            using (OleDbDataReader reader = sqlcmd.ExecuteReader())
140
+            {
141
+                while (reader.Read()) //这个read调用很重要!不写的话运行时将提示找不到数据  
142
+                {
143
+                    WvBean wb = new WvBean();
144
+                    int id = 0;
145
+                    int index = 0;
146
+                    string myId = "";
147
+                    string idInfo = "";
148
+                    string len = "";
149
+                    string mode = "";
150
+                    string desp = "";
151
+                    string defalutValue = "";
152
+                    string userValue = "";
153
+                    string remark = "";
154
+                    /*try
155
+                    {*/
156
+                    id = reader.GetInt32(0);
157
+                    index = reader.GetInt32(1);
158
+                    myId = reader.GetString(2);
159
+                    idInfo = calcNull(reader.GetString(3));
160
+                    len = calcNull(reader.GetString(4));
161
+                    mode = calcNull(reader.GetString(5));
162
+                    desp = calcNull(reader.GetString(6));
163
+                    defalutValue = calcNull(reader.GetString(7));
164
+                    userValue = calcNull(reader.GetString(8));
165
+                    remark = calcNull(reader.GetString(9));
166
+                    /* }
167
+                     catch (Exception exception)
168
+                     {
169
+                     }*/
170
+
171
+                    wb.id = id;
172
+                    wb.index = index;
173
+                    wb.myId = myId;
174
+                    wb.idInfo = idInfo;
175
+                    wb.len = len;
176
+                    wb.mode = mode;
177
+                    wb.desp = desp;
178
+                    wb.defalutValue = defalutValue;
179
+                    wb.userValue = userValue;
180
+                    wb.remark = remark;
181
+
182
+                    ID_INFO.Add(myId, userValue);
183
+
184
+                    //                        wvBeans.Add(wb);
185
+                }
186
+            }
187
+        }
188
+
152
 
189
 
153
         public int insert2Db(WvBean wb)
190
         public int insert2Db(WvBean wb)
154
         {
191
         {
172
             }
209
             }
173
             catch (Exception e)
210
             catch (Exception e)
174
             {
211
             {
175
-                
176
                 Console.WriteLine(e);
212
                 Console.WriteLine(e);
177
             }
213
             }
178
             Console.WriteLine(wb.ToString());
214
             Console.WriteLine(wb.ToString());
233
             return inst.ExecuteNonQuery();
269
             return inst.ExecuteNonQuery();
234
         }
270
         }
235
 
271
 
272
+
236
         private string calcNull(string ojb)
273
         private string calcNull(string ojb)
237
         {
274
         {
238
             if (ojb.Trim().Equals("") || ojb == null)
275
             if (ojb.Trim().Equals("") || ojb == null)

+ 13 - 1
README.md Ver arquivo

1
 
1
 
2
 # CSharp
2
 # CSharp
3
 >git remote add origin http://gitea.vcarecity.com/kerryzhang/GWSocketClient.git
3
 >git remote add origin http://gitea.vcarecity.com/kerryzhang/GWSocketClient.git
4
->git push -u origin master
4
+>git push -u origin master
5
+
6
+
7
+
8
+
9
+
10
+## 要解决问题
11
+
12
+1. 服务器没开
13
+2. 超时
14
+3. 连接成功了,但服务器主动断开
15
+
16
+