更新 'TRI_TCP_Server/TRI_TCP_Server/Net.cs'

master
ZGGSONG 3 years ago
parent 64e68bdb0b
commit 935c759181

@ -4,6 +4,7 @@ using System.Net.Sockets;
using System.IO; using System.IO;
using System.Text; using System.Text;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using System.Collections.Generic;
namespace TRI_TCP_Server namespace TRI_TCP_Server
{ {
@ -158,11 +159,20 @@ namespace TRI_TCP_Server
/// <param name="outTime"></param> /// <param name="outTime"></param>
public static string TCPSend(Socket socket, string path, int maxBufferLength, int outTime) public static string TCPSend(Socket socket, string path, int maxBufferLength, int outTime)
{ {
string res = null; string res = "";
string eqpName = "";
int _flag; int _flag;
//先发送一个标记:"1000"标记为传文件 List<string> eqpNames = new List<string>();
//D:\KSATProjects\2021-10-ASM\settings\SPI\Lanto_HTTP.INI eqpNames.Add("2DAOI");
string eqpName = System.Text.RegularExpressions.Regex.IsMatch(path, "AOI") ? "AOI" : "SPI"; 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); string _filename = Path.GetFileName(path);
_flag = Net.SendData(socket, Encoding.Default.GetBytes("1000&" + eqpName + "&" + _filename), outTime); _flag = Net.SendData(socket, Encoding.Default.GetBytes("1000&" + eqpName + "&" + _filename), outTime);
Console.WriteLine(_flag + " 文件发送 " + DateTime.Now.ToString("yyyy:MM:dd HH:mm:ss:ff")); Console.WriteLine(_flag + " 文件发送 " + DateTime.Now.ToString("yyyy:MM:dd HH:mm:ss:ff"));
@ -190,49 +200,47 @@ namespace TRI_TCP_Server
} }
return res; return res;
} }
/// <summary> /// <summary>
/// 接收消息 /// 接收数据
/// </summary> /// </summary>
/// <param name="socketClientPara"></param> /// <param name="socket"></param>
private static void RecMsg(object socketClientPara) /// <param name="buffer">size=25</param>
{ /// <returns>数据 or null</returns>
Socket socketClient = socketClientPara as Socket; public static string[] RecvData(Socket socket, byte[] buffer)
while (true)
{
//定义一个接受用的缓存区100M字节数组
//byte[] arrMsgRec = new byte[1024 * 1024 * 100];
//将接收到的数据存入arrMsgRec数组,并返回真正接受到的数据的长度
if (socketClient.Connected)
{ {
try if (socket == null || socket.Connected == false)
{ {
//因为终端每次发送文件的最大缓冲区是512字节所以每次接收也是定义为512字节 throw new ArgumentException("参数socket 为null或者未连接到远程计算机");
byte[] _buf = new byte[25]; }
byte[] buffer = new byte[512]; if (buffer == null || buffer.Length == 0)
int size = 0;
long len = 0;
string fileSavePath = @"D:\ServerDir";//获得用户保存文件的路径
if (!Directory.Exists(fileSavePath))
{ {
Directory.CreateDirectory(fileSavePath); throw new ArgumentException("参数buffer 为null ,或者长度为 0");
} }
//有文件发送才创建新文件 if (socket.Receive(buffer, 0, buffer.Length, SocketFlags.None) > 0)
if (socketClient.Receive(_buf, 0, 25, SocketFlags.None) > 0)
{ {
//string recv = Encoding.Default.GetString(_buf); string recv = Encoding.Default.GetString(buffer);
//Console.WriteLine(socketClient.RemoteEndPoint + " 接收消息: " + DateTime.Now.ToString("yyyy:MM:dd HH:mm:ss:fff")); string[] array = Regex.Split(recv, "(&)");
string res = Encoding.Default.GetString(_buf); return array;
string[] str = Regex.Split(res, "(&)"); }
if (str[0] == "1000") return null;
}
/// <summary>
/// 接收并在本地创建文件
/// </summary>
/// <param name="socket"></param>
/// <param name="buffer"></param>
/// <param name="path"></param>
/// <returns>返回文件接收时间和大小</returns>
public static string RecvFile(Socket socket, byte[] buffer, string path)
{ {
string fileName = fileSavePath + "\\" + str[2] + "\\" + str[4].TrimEnd('\0'); int size = 0;
long len = 0;
DateTime oTimeBegin = DateTime.Now;
//创建文件流,然后让文件流来根据路径创建一个文件 //创建文件流,然后让文件流来根据路径创建一个文件
FileStream fs = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Write); FileStream fs = new FileStream(path, FileMode.Create, FileAccess.Write);
//从终端不停的接受数据然后写入文件里面只到接受到的数据为0为止则中断连接 //从终端不停的接受数据然后写入文件里面只到接受到的数据为0为止则中断连接
while ((size = socket.Receive(buffer, 0, buffer.Length, SocketFlags.None)) > 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); string flag = Encoding.Default.GetString(buffer, size - 1, 1);
if (flag.Equals("`")) if (flag.Equals("`"))
@ -251,27 +259,9 @@ namespace TRI_TCP_Server
TimeSpan oTime = oTimeEnd.Subtract(oTimeBegin); TimeSpan oTime = oTimeEnd.Subtract(oTimeBegin);
fs.Flush(); fs.Flush();
fs.Close(); fs.Close();
len -= 1; len -= 1;//去除文件尾标记占据空间
Console.WriteLine("文件保存成功:" + fileName);
string store = (len / 1024.0) > 1.0 ? (len / 1024.0) + "kb" : len + "字节"; string store = (len / 1024.0) > 1.0 ? (len / 1024.0) + "kb" : len + "字节";
Console.WriteLine("接收文件用时:" + oTime.ToString() + ",文件大小:" + store); return $"接收文件{path}用时: {oTime}, 文件大小:{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
{
}
}
} }
} }
}
} }
Loading…
Cancel
Save