浏览代码

TCP客户端

章政 8 年之前
父节点
当前提交
4fc9f38c30
共有 2 个文件被更改,包括 91 次插入8 次删除
  1. 36 1
      UAS-MES/Form1.Designer.cs
  2. 55 7
      UAS-MES/Form1.cs

+ 36 - 1
UAS-MES/Form1.Designer.cs

@@ -35,6 +35,9 @@
             this.progressBar1 = new System.Windows.Forms.ProgressBar();
             this.textBoxWithPlaceHolder1 = new UAS_MES.CustomControl.TextBoxWithIcon.TextBoxWithPlaceHolder();
             this.serialPortCombox1 = new UAS_MES.CustomControl.ComBoxWithFocus.SerialPortCombox();
+            this.txtMsg = new System.Windows.Forms.RichTextBox();
+            this.txtIP = new System.Windows.Forms.TextBox();
+            this.txtPORT = new System.Windows.Forms.TextBox();
             this.SuspendLayout();
             // 
             // button1
@@ -77,6 +80,7 @@
             this.button4.TabIndex = 2;
             this.button4.Text = "button4";
             this.button4.UseVisualStyleBackColor = true;
+            this.button4.Click += new System.EventHandler(this.button4_Click);
             // 
             // progressBar1
             // 
@@ -90,7 +94,7 @@
             // textBoxWithPlaceHolder1
             // 
             this.textBoxWithPlaceHolder1.BackColor = System.Drawing.Color.White;
-            this.textBoxWithPlaceHolder1.Location = new System.Drawing.Point(747, 292);
+            this.textBoxWithPlaceHolder1.Location = new System.Drawing.Point(697, 198);
             this.textBoxWithPlaceHolder1.Name = "textBoxWithPlaceHolder1";
             this.textBoxWithPlaceHolder1.PlaceHolder = "123123";
             this.textBoxWithPlaceHolder1.Size = new System.Drawing.Size(241, 25);
@@ -103,11 +107,38 @@
             this.serialPortCombox1.Size = new System.Drawing.Size(186, 25);
             this.serialPortCombox1.TabIndex = 6;
             // 
+            // txtMsg
+            // 
+            this.txtMsg.Location = new System.Drawing.Point(424, 320);
+            this.txtMsg.Name = "txtMsg";
+            this.txtMsg.Size = new System.Drawing.Size(283, 160);
+            this.txtMsg.TabIndex = 7;
+            this.txtMsg.Text = "";
+            // 
+            // txtIP
+            // 
+            this.txtIP.Location = new System.Drawing.Point(424, 270);
+            this.txtIP.Name = "txtIP";
+            this.txtIP.Size = new System.Drawing.Size(136, 25);
+            this.txtIP.TabIndex = 8;
+            this.txtIP.Text = "127.0.0.1";
+            // 
+            // txtPORT
+            // 
+            this.txtPORT.Location = new System.Drawing.Point(607, 270);
+            this.txtPORT.Name = "txtPORT";
+            this.txtPORT.Size = new System.Drawing.Size(100, 25);
+            this.txtPORT.TabIndex = 9;
+            this.txtPORT.Text = "502";
+            // 
             // Form1
             // 
             this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 15F);
             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
             this.ClientSize = new System.Drawing.Size(1239, 738);
+            this.Controls.Add(this.txtPORT);
+            this.Controls.Add(this.txtIP);
+            this.Controls.Add(this.txtMsg);
             this.Controls.Add(this.serialPortCombox1);
             this.Controls.Add(this.textBoxWithPlaceHolder1);
             this.Controls.Add(this.progressBar1);
@@ -119,6 +150,7 @@
             this.Text = "Form1";
             this.Load += new System.EventHandler(this.Form1_Load);
             this.ResumeLayout(false);
+            this.PerformLayout();
 
         }
 
@@ -134,5 +166,8 @@
         private System.Windows.Forms.ProgressBar progressBar1;
         private CustomControl.TextBoxWithIcon.TextBoxWithPlaceHolder textBoxWithPlaceHolder1;
         private CustomControl.ComBoxWithFocus.SerialPortCombox serialPortCombox1;
+        private System.Windows.Forms.RichTextBox txtMsg;
+        private System.Windows.Forms.TextBox txtIP;
+        private System.Windows.Forms.TextBox txtPORT;
     }
 }

+ 55 - 7
UAS-MES/Form1.cs

@@ -20,6 +20,7 @@ using System.Reflection;
 using System.Threading;
 using UAS_MES.CustomControl;
 using System.IO.Ports;
+using System.Net.Sockets;
 
 namespace UAS_MES
 {
@@ -30,15 +31,17 @@ namespace UAS_MES
             InitializeComponent();
         }
 
+      
+
         private void Form1_Load(object sender, EventArgs e)
         {
-            Com.PortName = "COM4";
-            Com.ReadTimeout = 5000;
-            Com.WriteTimeout = 5000;
-            Com.BaudRate = 9600;
-            Com.StopBits = StopBits.One;
-            Com.Parity = Parity.None;
-            Com.Open();
+            //Com.PortName = "COM4";
+            //Com.ReadTimeout = 5000;
+            //Com.WriteTimeout = 5000;
+            //Com.BaudRate = 9600;
+            //Com.StopBits = StopBits.One;
+            //Com.Parity = Parity.None;
+  
         }
 
         [DllImport("WinIo64.dll")]
@@ -167,5 +170,50 @@ namespace UAS_MES
             //Com.Write(byteArr, 0, byteArr.Length);
             //CloseWinIo();
         }
+
+        Thread threadWatch = null; //负责监听客户端的线程
+        Socket socketWatch = null; //负责监听客户端的套接字
+        //创建一个负责和客户端通信的套接字 
+        List<Socket> socConnections = new List<Socket>();
+        List<Thread> dictThread = new List<Thread>();
+        private void button4_Click(object sender, EventArgs e)
+        {
+            CheckForIllegalCrossThreadCalls = false;
+            //定义一个套接字用于监听客户端发来的信息  包含3个参数(IP4寻址协议,流式连接,TCP协议)
+            socketWatch = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
+            //服务端发送信息 需要1个IP地址和端口号
+            IPAddress ipaddress = IPAddress.Parse(txtIP.Text.Trim()); //获取文本框输入的IP地址
+            //将IP地址和端口号绑定到网络节点endpoint上 
+            IPEndPoint endpoint = new IPEndPoint(ipaddress, int.Parse(txtPORT.Text.Trim())); //获取文本框上输入的端口号
+            //监听绑定的网络节点
+            socketWatch.Bind(endpoint);
+            //将套接字的监听队列长度限制为20
+            socketWatch.Listen(20);
+            //创建一个监听线程 
+            threadWatch = new Thread(WatchConnecting);
+            //将窗体线程设置为与后台同步
+            threadWatch.IsBackground = true;
+            //启动线程
+            threadWatch.Start();
+            //启动线程后 txtMsg文本框显示相应提示
+            txtMsg.AppendText("开始监听客户端传来的信息!" + "\r\n");
+
+        }
+
+        private void WatchConnecting()
+        {
+            while (true)
+            {
+                byte[] data = new byte[1024];
+                socketWatch.Receive(data);
+                int length = data[5];
+                byte[] datashow = new byte[length + 6];
+                Console.WriteLine(data);
+                for (int i = 0; i <= length + 5; i++)
+                    datashow[i] = data[i];
+                string stringdata = BitConverter.ToString(datashow);//把数组转换成16进制字符串
+                Console.WriteLine(stringdata);
+            }
+        }
     }
 }