diff --git a/TRI_TCP_Server/TRI_TCP_Server/Net.cs b/TRI_TCP_Server/TRI_TCP_Server/Net.cs
index 61652ea..90a6532 100644
--- a/TRI_TCP_Server/TRI_TCP_Server/Net.cs
+++ b/TRI_TCP_Server/TRI_TCP_Server/Net.cs
@@ -4,6 +4,7 @@ using System.Net.Sockets;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
+using System.Collections.Generic;
namespace TRI_TCP_Server
{
@@ -158,11 +159,20 @@ namespace TRI_TCP_Server
///
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();
+ 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"));
@@ -190,88 +200,68 @@ namespace TRI_TCP_Server
}
return res;
}
+
///
- /// 接收消息
+ /// 接收数据
///
- ///
- private static void RecMsg(object socketClientPara)
+ ///
+ /// size=25
+ /// 数据 or null
+ public static string[] RecvData(Socket socket, byte[] buffer)
{
- Socket socketClient = socketClientPara as Socket;
- while (true)
+ if (socket == null || socket.Connected == false)
+ {
+ throw new ArgumentException("参数socket 为null,或者未连接到远程计算机");
+ }
+ if (buffer == null || buffer.Length == 0)
{
- //定义一个接受用的缓存区(100M字节数组)
- //byte[] arrMsgRec = new byte[1024 * 1024 * 100];
- //将接收到的数据存入arrMsgRec数组,并返回真正接受到的数据的长度
- if (socketClient.Connected)
+ 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("`"))
{
- try
- {
- //因为终端每次发送文件的最大缓冲区是512字节,所以每次接收也是定义为512字节
- byte[] _buf = new byte[25];
- byte[] buffer = new byte[512];
- int size = 0;
- long len = 0;
- string fileSavePath = @"D:\ServerDir";//获得用户保存文件的路径
- if (!Directory.Exists(fileSavePath))
- {
- Directory.CreateDirectory(fileSavePath);
- }
- //有文件发送才创建新文件
- if (socketClient.Receive(_buf, 0, 25, SocketFlags.None) > 0)
- {
- //string recv = Encoding.Default.GetString(_buf);
- //Console.WriteLine(socketClient.RemoteEndPoint + " 接收消息: " + DateTime.Now.ToString("yyyy:MM:dd HH:mm:ss:fff"));
- string res = Encoding.Default.GetString(_buf);
- string[] str = Regex.Split(res, "(&)");
- if (str[0] == "1000")
- {
- string fileName = fileSavePath + "\\" + str[2] + "\\" + str[4].TrimEnd('\0');
- //创建文件流,然后让文件流来根据路径创建一个文件
- FileStream fs = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Write);
- //从终端不停的接受数据,然后写入文件里面,只到接受到的数据为0为止,则中断连接
-
- DateTime oTimeBegin = DateTime.Now;
-
- while ((size = socketClient.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;
- Console.WriteLine("文件保存成功:" + fileName);
- string store = (len / 1024.0) > 1.0 ? (len / 1024.0) + "kb" : len + "字节";
- Console.WriteLine("接收文件用时:" + oTime.ToString() + ",文件大小:" + store);
- //Console.WriteLine(socketClient.RemoteEndPoint + "断开连接");
- //dict.Remove(socketClient.RemoteEndPoint.ToString());
- //socketClient.Close();
- }
- else if (str[0].TrimEnd('\0') == "1001")
- {
- Net.SendData(socketClient, Encoding.Default.GetBytes("1001"), 1);
- }
- }
- }
- catch
- {
-
- }
+ 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