1
0
mirror of https://github.com/zhaopeiym/IoTClient synced 2025-10-26 22:15:44 +08:00

OtherControl TCP

This commit is contained in:
BennyZhao 2019-12-31 10:03:01 +08:00
parent 4b38e1c44b
commit e848465846
2 changed files with 355 additions and 14 deletions

View File

@ -34,11 +34,35 @@
this.txt_crcstr = new System.Windows.Forms.TextBox();
this.txt_content = new System.Windows.Forms.TextBox();
this.but_crc16validation = new System.Windows.Forms.Button();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.groupBox2 = new System.Windows.Forms.GroupBox();
this.label3 = new System.Windows.Forms.Label();
this.txt_tcpmsg = new System.Windows.Forms.TextBox();
this.txt_tcpport = new System.Windows.Forms.TextBox();
this.label2 = new System.Windows.Forms.Label();
this.label1 = new System.Windows.Forms.Label();
this.txt_tcpip = new System.Windows.Forms.TextBox();
this.but_tcpsend = new System.Windows.Forms.Button();
this.but_tcpclose = new System.Windows.Forms.Button();
this.but_tcpopen = new System.Windows.Forms.Button();
this.groupBox3 = new System.Windows.Forms.GroupBox();
this.label4 = new System.Windows.Forms.Label();
this.textBox4 = new System.Windows.Forms.TextBox();
this.textBox5 = new System.Windows.Forms.TextBox();
this.label5 = new System.Windows.Forms.Label();
this.label6 = new System.Windows.Forms.Label();
this.textBox6 = new System.Windows.Forms.TextBox();
this.button4 = new System.Windows.Forms.Button();
this.button5 = new System.Windows.Forms.Button();
this.button6 = new System.Windows.Forms.Button();
this.groupBox1.SuspendLayout();
this.groupBox2.SuspendLayout();
this.groupBox3.SuspendLayout();
this.SuspendLayout();
//
// but_crc16calculate
//
this.but_crc16calculate.Location = new System.Drawing.Point(183, 11);
this.but_crc16calculate.Location = new System.Drawing.Point(177, 14);
this.but_crc16calculate.Name = "but_crc16calculate";
this.but_crc16calculate.Size = new System.Drawing.Size(75, 23);
this.but_crc16calculate.TabIndex = 0;
@ -48,7 +72,7 @@
//
// txt_crcstr
//
this.txt_crcstr.Location = new System.Drawing.Point(12, 12);
this.txt_crcstr.Location = new System.Drawing.Point(6, 15);
this.txt_crcstr.Name = "txt_crcstr";
this.txt_crcstr.Size = new System.Drawing.Size(165, 21);
this.txt_crcstr.TabIndex = 1;
@ -63,7 +87,7 @@
//
// but_crc16validation
//
this.but_crc16validation.Location = new System.Drawing.Point(264, 12);
this.but_crc16validation.Location = new System.Drawing.Point(258, 15);
this.but_crc16validation.Name = "but_crc16validation";
this.but_crc16validation.Size = new System.Drawing.Size(75, 23);
this.but_crc16validation.TabIndex = 3;
@ -71,16 +95,228 @@
this.but_crc16validation.UseVisualStyleBackColor = true;
this.but_crc16validation.Click += new System.EventHandler(this.but_crc16validation_Click);
//
// groupBox1
//
this.groupBox1.Controls.Add(this.but_crc16calculate);
this.groupBox1.Controls.Add(this.but_crc16validation);
this.groupBox1.Controls.Add(this.txt_crcstr);
this.groupBox1.Location = new System.Drawing.Point(3, 3);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(346, 44);
this.groupBox1.TabIndex = 4;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "CRC";
//
// groupBox2
//
this.groupBox2.Controls.Add(this.label3);
this.groupBox2.Controls.Add(this.txt_tcpmsg);
this.groupBox2.Controls.Add(this.txt_tcpport);
this.groupBox2.Controls.Add(this.label2);
this.groupBox2.Controls.Add(this.label1);
this.groupBox2.Controls.Add(this.txt_tcpip);
this.groupBox2.Controls.Add(this.but_tcpsend);
this.groupBox2.Controls.Add(this.but_tcpclose);
this.groupBox2.Controls.Add(this.but_tcpopen);
this.groupBox2.Location = new System.Drawing.Point(356, 3);
this.groupBox2.Name = "groupBox2";
this.groupBox2.Size = new System.Drawing.Size(253, 110);
this.groupBox2.TabIndex = 5;
this.groupBox2.TabStop = false;
this.groupBox2.Text = "TCP";
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(4, 52);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(29, 12);
this.label3.TabIndex = 8;
this.label3.Text = "消息";
//
// txt_tcpmsg
//
this.txt_tcpmsg.Location = new System.Drawing.Point(33, 41);
this.txt_tcpmsg.Multiline = true;
this.txt_tcpmsg.Name = "txt_tcpmsg";
this.txt_tcpmsg.Size = new System.Drawing.Size(214, 34);
this.txt_tcpmsg.TabIndex = 7;
//
// txt_tcpport
//
this.txt_tcpport.Location = new System.Drawing.Point(193, 14);
this.txt_tcpport.Name = "txt_tcpport";
this.txt_tcpport.Size = new System.Drawing.Size(53, 21);
this.txt_tcpport.TabIndex = 6;
this.txt_tcpport.TextChanged += new System.EventHandler(this.textBox2_TextChanged);
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(164, 18);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(29, 12);
this.label2.TabIndex = 5;
this.label2.Text = "端口";
this.label2.Click += new System.EventHandler(this.label2_Click);
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(6, 19);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(17, 12);
this.label1.TabIndex = 4;
this.label1.Text = "IP";
//
// txt_tcpip
//
this.txt_tcpip.Location = new System.Drawing.Point(33, 14);
this.txt_tcpip.Name = "txt_tcpip";
this.txt_tcpip.Size = new System.Drawing.Size(128, 21);
this.txt_tcpip.TabIndex = 3;
//
// but_tcpsend
//
this.but_tcpsend.Location = new System.Drawing.Point(173, 81);
this.but_tcpsend.Name = "but_tcpsend";
this.but_tcpsend.Size = new System.Drawing.Size(75, 23);
this.but_tcpsend.TabIndex = 2;
this.but_tcpsend.Text = "发送";
this.but_tcpsend.UseVisualStyleBackColor = true;
this.but_tcpsend.Click += new System.EventHandler(this.but_tcpSend_Click);
//
// but_tcpclose
//
this.but_tcpclose.Location = new System.Drawing.Point(89, 81);
this.but_tcpclose.Name = "but_tcpclose";
this.but_tcpclose.Size = new System.Drawing.Size(75, 23);
this.but_tcpclose.TabIndex = 1;
this.but_tcpclose.Text = "关闭连接";
this.but_tcpclose.UseVisualStyleBackColor = true;
this.but_tcpclose.Click += new System.EventHandler(this.but_tcpClose_Click);
//
// but_tcpopen
//
this.but_tcpopen.Location = new System.Drawing.Point(5, 81);
this.but_tcpopen.Name = "but_tcpopen";
this.but_tcpopen.Size = new System.Drawing.Size(75, 23);
this.but_tcpopen.TabIndex = 0;
this.but_tcpopen.Text = "打开连接";
this.but_tcpopen.UseVisualStyleBackColor = true;
this.but_tcpopen.Click += new System.EventHandler(this.but_tcpOpen_Click);
//
// groupBox3
//
this.groupBox3.Controls.Add(this.label4);
this.groupBox3.Controls.Add(this.textBox4);
this.groupBox3.Controls.Add(this.textBox5);
this.groupBox3.Controls.Add(this.label5);
this.groupBox3.Controls.Add(this.label6);
this.groupBox3.Controls.Add(this.textBox6);
this.groupBox3.Controls.Add(this.button4);
this.groupBox3.Controls.Add(this.button5);
this.groupBox3.Controls.Add(this.button6);
this.groupBox3.Location = new System.Drawing.Point(616, 3);
this.groupBox3.Name = "groupBox3";
this.groupBox3.Size = new System.Drawing.Size(253, 110);
this.groupBox3.TabIndex = 9;
this.groupBox3.TabStop = false;
this.groupBox3.Text = "UDP";
//
// label4
//
this.label4.AutoSize = true;
this.label4.Location = new System.Drawing.Point(4, 52);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(29, 12);
this.label4.TabIndex = 8;
this.label4.Text = "消息";
//
// textBox4
//
this.textBox4.Location = new System.Drawing.Point(33, 41);
this.textBox4.Multiline = true;
this.textBox4.Name = "textBox4";
this.textBox4.Size = new System.Drawing.Size(214, 34);
this.textBox4.TabIndex = 7;
//
// textBox5
//
this.textBox5.Location = new System.Drawing.Point(193, 14);
this.textBox5.Name = "textBox5";
this.textBox5.Size = new System.Drawing.Size(53, 21);
this.textBox5.TabIndex = 6;
//
// label5
//
this.label5.AutoSize = true;
this.label5.Location = new System.Drawing.Point(164, 18);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(29, 12);
this.label5.TabIndex = 5;
this.label5.Text = "端口";
//
// label6
//
this.label6.AutoSize = true;
this.label6.Location = new System.Drawing.Point(6, 19);
this.label6.Name = "label6";
this.label6.Size = new System.Drawing.Size(17, 12);
this.label6.TabIndex = 4;
this.label6.Text = "IP";
//
// textBox6
//
this.textBox6.Location = new System.Drawing.Point(33, 14);
this.textBox6.Name = "textBox6";
this.textBox6.Size = new System.Drawing.Size(128, 21);
this.textBox6.TabIndex = 3;
//
// button4
//
this.button4.Location = new System.Drawing.Point(173, 81);
this.button4.Name = "button4";
this.button4.Size = new System.Drawing.Size(75, 23);
this.button4.TabIndex = 2;
this.button4.Text = "发送";
this.button4.UseVisualStyleBackColor = true;
//
// button5
//
this.button5.Location = new System.Drawing.Point(89, 81);
this.button5.Name = "button5";
this.button5.Size = new System.Drawing.Size(75, 23);
this.button5.TabIndex = 1;
this.button5.Text = "关闭连接";
this.button5.UseVisualStyleBackColor = true;
//
// button6
//
this.button6.Location = new System.Drawing.Point(5, 81);
this.button6.Name = "button6";
this.button6.Size = new System.Drawing.Size(75, 23);
this.button6.TabIndex = 0;
this.button6.Text = "打开连接";
this.button6.UseVisualStyleBackColor = true;
this.button6.Click += new System.EventHandler(this.button6_Click);
//
// OtherControl
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.Controls.Add(this.but_crc16validation);
this.Controls.Add(this.groupBox3);
this.Controls.Add(this.groupBox2);
this.Controls.Add(this.groupBox1);
this.Controls.Add(this.txt_content);
this.Controls.Add(this.txt_crcstr);
this.Controls.Add(this.but_crc16calculate);
this.Name = "OtherControl";
this.Size = new System.Drawing.Size(880, 450);
this.groupBox1.ResumeLayout(false);
this.groupBox1.PerformLayout();
this.groupBox2.ResumeLayout(false);
this.groupBox2.PerformLayout();
this.groupBox3.ResumeLayout(false);
this.groupBox3.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout();
@ -93,5 +329,26 @@
private System.Windows.Forms.TextBox txt_crcstr;
private System.Windows.Forms.TextBox txt_content;
private System.Windows.Forms.Button but_crc16validation;
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.GroupBox groupBox2;
private System.Windows.Forms.Button but_tcpsend;
private System.Windows.Forms.Button but_tcpclose;
private System.Windows.Forms.Button but_tcpopen;
private System.Windows.Forms.TextBox txt_tcpport;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox txt_tcpip;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.TextBox txt_tcpmsg;
private System.Windows.Forms.GroupBox groupBox3;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.TextBox textBox4;
private System.Windows.Forms.TextBox textBox5;
private System.Windows.Forms.Label label5;
private System.Windows.Forms.Label label6;
private System.Windows.Forms.TextBox textBox6;
private System.Windows.Forms.Button button4;
private System.Windows.Forms.Button button5;
private System.Windows.Forms.Button button6;
}
}

View File

@ -1,21 +1,20 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using IoTClient.Common.Helpers;
using System;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;
using System.Windows.Forms;
using IoTClient.Common.Helpers;
namespace IoTClient.Tool.Controls
{
public partial class OtherControl : UserControl
{
private Socket socket;
public OtherControl()
{
InitializeComponent();
but_tcpclose.Enabled = false;
but_tcpsend.Enabled = false;
}
private void but_crc16calculate_Click(object sender, EventArgs e)
@ -39,5 +38,90 @@ namespace IoTClient.Tool.Controls
var checkCrc16 = CRC16.CheckCRC16(byteArry);
AppendText($"CRC16验证结果{txt_crcstr.Text} {checkCrc16}");
}
private void label2_Click(object sender, EventArgs e)
{
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
private void but_tcpOpen_Click(object sender, EventArgs e)
{
socket?.Close();
socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
try
{
socket.SendTimeout = 1000 * 2;
socket.ReceiveTimeout = 1000 * 2;
socket.Connect(new IPEndPoint(IPAddress.Parse(txt_tcpip.Text?.Trim()), int.Parse(txt_tcpport.Text?.Trim())));
AppendText("打开连接成功");
but_tcpopen.Enabled = false;
but_tcpclose.Enabled = true;
but_tcpsend.Enabled = true;
}
catch (Exception ex)
{
}
}
private void but_tcpClose_Click(object sender, EventArgs e)
{
socket?.Close();
AppendText("关闭连接成功");
but_tcpopen.Enabled = true;
}
private void but_tcpSend_Click(object sender, EventArgs e)
{
try
{
var command = DataConvert.StringToByteArray(txt_tcpmsg.Text, false);
socket.Send(command);
var msg = SocketRead(socket, 4096);
AppendText(msg.ByteArrayToString());
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
//重新连接
socket?.Close();
socket.Connect(new IPEndPoint(IPAddress.Parse(txt_tcpip.Text?.Trim()), int.Parse(txt_tcpport.Text?.Trim())));
}
}
protected byte[] SocketRead(Socket socket, int receiveCount)
{
try
{
byte[] receiveBytes = new byte[receiveCount];
int receiveFinish = 0;
var readLeng = socket.Receive(receiveBytes, receiveFinish, receiveCount, SocketFlags.None);
if (readLeng == 0)
{
if (socket.Connected) socket.Shutdown(SocketShutdown.Both);
socket.Close();
throw new Exception("连接已断开");
}
return receiveBytes.Take(readLeng).ToArray();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
//重新连接
socket?.Close();
socket.Connect(new IPEndPoint(IPAddress.Parse(txt_tcpip.Text?.Trim()), int.Parse(txt_tcpport.Text?.Trim())));
return null;
}
}
private void button6_Click(object sender, EventArgs e)
{
}
}
}