From 9c92fe827e6a91140600ae8a88d779289e706c8a Mon Sep 17 00:00:00 2001 From: ZGGSONG Date: Wed, 22 Dec 2021 09:47:45 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20'TRI=5FTCP=5FClient/TRI=5F?= =?UTF-8?q?TCP=5FClient/Net.cs'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TRI_TCP_Client/TRI_TCP_Client/Net.cs | 89 +++++++++++++++++++++++++--- 1 file changed, 81 insertions(+), 8 deletions(-) diff --git a/TRI_TCP_Client/TRI_TCP_Client/Net.cs b/TRI_TCP_Client/TRI_TCP_Client/Net.cs index bae7a11..f1f4167 100644 --- a/TRI_TCP_Client/TRI_TCP_Client/Net.cs +++ b/TRI_TCP_Client/TRI_TCP_Client/Net.cs @@ -3,6 +3,8 @@ using System.Net; using System.Net.Sockets; using System.IO; using System.Text; +using System.Collections.Generic; +using System.Text.RegularExpressions; namespace TRI_TCP_Client { @@ -25,6 +27,7 @@ namespace TRI_TCP_Client /// public static int SendData(Socket socket, byte[] buffer, int outTime) { + int flag = 0; if (socket == null || socket.Connected == false) { throw new ArgumentException("参数socket 为null,或者未连接到远程计算机"); @@ -33,8 +36,6 @@ namespace TRI_TCP_Client { throw new ArgumentException("参数buffer 为null ,或者长度为 0"); } - - int flag = 0; try { int left = buffer.Length; @@ -42,7 +43,7 @@ namespace TRI_TCP_Client while (true) { - if ((socket.Poll(outTime * 100, SelectMode.SelectWrite) == true)) + if (socket.Poll(outTime * 100, SelectMode.SelectWrite) == true) { // 收集了足够多的传出数据后开始发送 sndLen = socket.Send(buffer, sndLen, left, SocketFlags.None); left -= sndLen; @@ -157,11 +158,21 @@ namespace TRI_TCP_Client /// public static string TCPSend(Socket socket, string path, int maxBufferLength, int outTime) { - string res = null; + string res = ""; + string eqpName = ""; int _flag; - //先发送一个标记:"1000"标记为传文件 - //D:\KSATProjects\2021-10-ASM\settings\SPI\Lanto_HTTP.INI - string eqpName = System.Text.RegularExpressions.Regex.IsMatch(path, "AOI") ? "AOI" : "SPI"; + List eqpNames = new List(); + //TODO: 根据需求修改具体的设备名 + eqpNames.Add("2DAOI"); + eqpNames.Add("3DAOI"); + eqpNames.Add("SPI"); + foreach (string item in eqpNames) + { + if (System.Text.RegularExpressions.Regex.IsMatch(path, item)) + { + eqpName = item; break; + } + } string _filename = Path.GetFileName(path); _flag = Net.SendData(socket, Encoding.Default.GetBytes("1000&" + eqpName + "&" + _filename), outTime); Console.WriteLine(_flag + " 文件发送 " + DateTime.Now.ToString("yyyy:MM:dd HH:mm:ss:ff")); @@ -185,9 +196,71 @@ namespace TRI_TCP_Client } catch (Exception ex) { - res ="[ERR]" + ex.Message.ToString(); + res = "[ERR]" + ex.Message.ToString(); } return res; } + /// + /// 接收数据 + /// + /// + /// size=25 + /// 数据 or null + public static string[] RecvData(Socket socket, byte[] buffer) + { + if (socket == null || socket.Connected == false) + { + throw new ArgumentException("参数socket 为null,或者未连接到远程计算机"); + } + if (buffer == null || buffer.Length == 0) + { + throw new ArgumentException("参数buffer 为null ,或者长度为 0"); + } + if (socket.Receive(buffer, 0, buffer.Length, SocketFlags.None) > 0) + { + string recv = Encoding.Default.GetString(buffer); + string[] array = Regex.Split(recv, "(&)"); + return array; + } + return null; + } + /// + /// 接收并在本地创建文件 + /// + /// + /// + /// + /// 返回文件接收时间和大小 + public static string RecvFile(Socket socket, byte[] buffer, string path) + { + int size = 0; + long len = 0; + DateTime oTimeBegin = DateTime.Now; + //创建文件流,然后让文件流来根据路径创建一个文件 + FileStream fs = new FileStream(path, FileMode.Create, FileAccess.Write); + //从终端不停的接受数据,然后写入文件里面,只到接受到的数据为0为止,则中断连接 + while ((size = socket.Receive(buffer, 0, buffer.Length, SocketFlags.None)) > 0) + { + string flag = Encoding.Default.GetString(buffer, size - 1, 1); + if (flag.Equals("`")) + { + fs.Write(buffer, 0, size - 1); + len += size; + break; + } + else + { + fs.Write(buffer, 0, size); + len += size; + } + } + DateTime oTimeEnd = DateTime.Now; + TimeSpan oTime = oTimeEnd.Subtract(oTimeBegin); + fs.Flush(); + fs.Close(); + len -= 1;//去除文件尾标记占据空间 + string store = (len / 1024.0) > 1.0 ? (len / 1024.0) + "kb" : len + "字节"; + return $"接收文件{path}用时: {oTime}, 文件大小:{store}"; + } } } \ No newline at end of file