|
|
|
@ -0,0 +1,290 @@
|
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Net;
|
|
|
|
|
using System.Net.Sockets;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using TRI_TCP_Client.Log;
|
|
|
|
|
|
|
|
|
|
namespace TRI_TCP_Client
|
|
|
|
|
{
|
|
|
|
|
#region 心跳包
|
|
|
|
|
public class Ping
|
|
|
|
|
{
|
|
|
|
|
private int flag;
|
|
|
|
|
private Socket pingsocket;
|
|
|
|
|
public Ping(Socket so)
|
|
|
|
|
{
|
|
|
|
|
pingsocket = so;
|
|
|
|
|
}
|
|
|
|
|
public void PingProc()
|
|
|
|
|
{
|
|
|
|
|
byte[] _buf = new byte[4];
|
|
|
|
|
//发送心跳包
|
|
|
|
|
while (true)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
Thread.Sleep(3000);
|
|
|
|
|
flag = Net.SendData(pingsocket, Encoding.Default.GetBytes("1001"), 1);
|
|
|
|
|
Console.WriteLine(flag + " 心跳信号 " + DateTime.Now.ToString("yyyy:MM:dd HH:mm:ss:fff"));
|
|
|
|
|
//接受心跳放置在接受文件线程
|
|
|
|
|
//if (pingsocket.Receive(_buf, 0, 4, SocketFlags.None) > 0)
|
|
|
|
|
//{
|
|
|
|
|
// string recv = Encoding.Default.GetString(_buf);
|
|
|
|
|
// Console.WriteLine(recv + " 接收消息 " + DateTime.Now.ToString("yyyy:MM:dd HH:mm:ss:fff"));
|
|
|
|
|
//}
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("服务端: " + pingsocket.RemoteEndPoint + "掉线了");
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
public class Client
|
|
|
|
|
{
|
|
|
|
|
//服务端IP
|
|
|
|
|
private static string Ip = "172.17.208.203";
|
|
|
|
|
//TCP端口
|
|
|
|
|
private static string Port = "5001";
|
|
|
|
|
#region TCP连接
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 创建TCP客户端连接
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>返回TCP套接字</returns>
|
|
|
|
|
/// <remarks>
|
|
|
|
|
/// 创建连接的同时发送心跳包
|
|
|
|
|
/// </remarks>
|
|
|
|
|
public static Socket conn()
|
|
|
|
|
{
|
|
|
|
|
IPAddress ipaddress = IPAddress.Parse(Ip);
|
|
|
|
|
IPEndPoint endpoint = new IPEndPoint(ipaddress, int.Parse(Port));
|
|
|
|
|
//创建服务端负责监听的套接字,参数(使用IPV4协议,使用流式连接,使用TCO协议传输数据)
|
|
|
|
|
Socket socketClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
|
|
|
|
socketClient.Connect(endpoint);
|
|
|
|
|
if (socketClient.Connected)
|
|
|
|
|
{
|
|
|
|
|
//创建通信线程
|
|
|
|
|
ParameterizedThreadStart pts = new ParameterizedThreadStart(RecMsg);
|
|
|
|
|
Thread thradRecMsg = new Thread(pts);
|
|
|
|
|
thradRecMsg.IsBackground = true;
|
|
|
|
|
thradRecMsg.Start(socketClient);
|
|
|
|
|
Console.WriteLine(socketClient.RemoteEndPoint.ToString() + "连接成功");
|
|
|
|
|
}
|
|
|
|
|
// 开启心跳线程
|
|
|
|
|
Ping ping = new Ping(socketClient);
|
|
|
|
|
Thread t = new Thread(new ThreadStart(ping.PingProc));
|
|
|
|
|
t.IsBackground = true;
|
|
|
|
|
t.Start();
|
|
|
|
|
return socketClient;
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 接收消息
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="socketClientPara"></param>
|
|
|
|
|
private static void RecMsg(object socketClientPara)
|
|
|
|
|
{
|
|
|
|
|
Socket socketClient = socketClientPara as Socket;
|
|
|
|
|
|
|
|
|
|
while (true)
|
|
|
|
|
{
|
|
|
|
|
//定义一个接受用的缓存区(100M字节数组)
|
|
|
|
|
//byte[] arrMsgRec = new byte[1024 * 1024 * 100];
|
|
|
|
|
//将接收到的数据存入arrMsgRec数组,并返回真正接受到的数据的长度
|
|
|
|
|
if (socketClient.Connected)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
//因为终端每次发送文件的最大缓冲区是512字节,所以每次接收也是定义为512字节
|
|
|
|
|
byte[] _buf = new byte[25];
|
|
|
|
|
byte[] buffer = new byte[512];
|
|
|
|
|
int size = 0;
|
|
|
|
|
long len = 0;
|
|
|
|
|
string fileSavePath = @"D:\ClientDir";//获得用户保存文件的路径
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void Main()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
//连接
|
|
|
|
|
conn();
|
|
|
|
|
//FileChange.onFileChange(Path);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("连接服务器出错: " + ex.Message.ToString());
|
|
|
|
|
}
|
|
|
|
|
Console.ReadKey();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#region 弃用
|
|
|
|
|
public class Run
|
|
|
|
|
{
|
|
|
|
|
private static Socket socketClient = Client.conn();
|
|
|
|
|
public static void test()
|
|
|
|
|
{
|
|
|
|
|
using var watcher = new FileSystemWatcher(@"D:\ClientDir");
|
|
|
|
|
|
|
|
|
|
watcher.NotifyFilter = NotifyFilters.Attributes
|
|
|
|
|
| NotifyFilters.CreationTime
|
|
|
|
|
| NotifyFilters.DirectoryName
|
|
|
|
|
| NotifyFilters.FileName
|
|
|
|
|
| NotifyFilters.LastAccess
|
|
|
|
|
| NotifyFilters.LastWrite
|
|
|
|
|
| NotifyFilters.Security
|
|
|
|
|
| NotifyFilters.Size;
|
|
|
|
|
|
|
|
|
|
watcher.Changed += OnChanged;
|
|
|
|
|
watcher.Created += OnCreated;
|
|
|
|
|
watcher.Deleted += OnDeleted;
|
|
|
|
|
watcher.Renamed += OnRenamed;
|
|
|
|
|
watcher.Error += OnError;
|
|
|
|
|
|
|
|
|
|
watcher.Filter = "*.ini";
|
|
|
|
|
watcher.IncludeSubdirectories = true;
|
|
|
|
|
watcher.EnableRaisingEvents = true;
|
|
|
|
|
|
|
|
|
|
Console.WriteLine("Press enter to exit.");
|
|
|
|
|
Console.ReadLine();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void OnChanged(object sender, FileSystemEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (e.ChangeType != WatcherChangeTypes.Changed)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
Console.WriteLine($"Changed: {e.FullPath}");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void OnCreated(object sender, FileSystemEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
int _flag;
|
|
|
|
|
//先发送一个标记:"1000"标记为传文件
|
|
|
|
|
_flag = Net.SendData(socketClient, System.Text.Encoding.Default.GetBytes("1000"), 1);
|
|
|
|
|
Console.WriteLine(_flag + " 文件发送 " + DateTime.Now.ToString("yyyy:MM:dd HH:mm:ss:ff"));
|
|
|
|
|
string value = $"Created: {e.FullPath}";
|
|
|
|
|
Logger.Instance.WriteLog(value, LogType.Information);
|
|
|
|
|
//Socket socketClient = _socket.conn();
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
int flag = Net.SendFile(socketClient, e.FullPath, 512, 1);
|
|
|
|
|
if (flag == 0)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("文件发送成功");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("文件发送失败, flag=" + flag);
|
|
|
|
|
int flag2 = Net.SendFile(socketClient, e.FullPath, 512, 1);
|
|
|
|
|
if (flag2 == 0)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("文件重传成功");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("文件重传失败, flag=" + flag);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("[ERR]" + ex.Message.ToString());
|
|
|
|
|
Logger.Instance.WriteException(ex);
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
//socketClient.Close();
|
|
|
|
|
//Console.WriteLine("连接关闭");
|
|
|
|
|
//Logger.Instance.WriteLog("关闭连接", LogType.All);
|
|
|
|
|
}
|
|
|
|
|
Console.WriteLine(value);
|
|
|
|
|
Logger.Instance.WriteLog("end", LogType.Information);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void OnDeleted(object sender, FileSystemEventArgs e) =>
|
|
|
|
|
Console.WriteLine($"Deleted: {e.FullPath}");
|
|
|
|
|
|
|
|
|
|
private static void OnRenamed(object sender, RenamedEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine($"Renamed:");
|
|
|
|
|
Console.WriteLine($" Old: {e.OldFullPath}");
|
|
|
|
|
Console.WriteLine($" New: {e.FullPath}");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void OnError(object sender, ErrorEventArgs e) =>
|
|
|
|
|
PrintException(e.GetException());
|
|
|
|
|
|
|
|
|
|
private static void PrintException(Exception? ex)
|
|
|
|
|
{
|
|
|
|
|
if (ex != null)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine($"Message: {ex.Message}");
|
|
|
|
|
Console.WriteLine("Stacktrace:");
|
|
|
|
|
Console.WriteLine(ex.StackTrace);
|
|
|
|
|
Console.WriteLine();
|
|
|
|
|
PrintException(ex.InnerException);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
}
|