更新 'TRI_TCP_Client/TRI_TCP_Client/Net.cs'

master
ZGGSONG 3 years ago
parent 203389d97c
commit 9c92fe827e

@ -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
/// </remarks>
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
/// <param name="outTime"></param>
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<string> eqpNames = new List<string>();
//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"));
@ -189,5 +200,67 @@ namespace TRI_TCP_Client
}
return res;
}
/// <summary>
/// 接收数据
/// </summary>
/// <param name="socket"></param>
/// <param name="buffer">size=25</param>
/// <returns>数据 or null</returns>
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;
}
/// <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)
{
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}";
}
}
}
Loading…
Cancel
Save