⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cfile_trans.cs

📁 自定义协议
💻 CS
📖 第 1 页 / 共 2 页
字号:
using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Text;
using  System.Security.Cryptography;

namespace CodeManager
{
	/// <summary>
	/// CFile_Trans 的摘要说明。
	/// </summary>
	public class CFile_Trans
	{
		/// <summary>
		/// 连接参数
		/// </summary>
		private MainForm theApp;
		private String m_sIP;
		private String m_sPort;
		private String m_sAccount;
		private String m_sPwd;
		/// <summary>
		/// 文件参数
		/// </summary>
		private String m_shPath;
		private String m_slPath;
		private String m_sFile;
		/// <summary>
		/// 传输开头
		/// </summary>
		private byte m_bSwitch;
		public bool m_Enable;
		public Thread threadproc;

		private TcpClient m_sock;

		private FileStream fs;
		private BinaryReader r;
		private BinaryWriter w;
		private int nfindex;


		/// <summary>
		/// 创建文件传输类
		/// </summary>
		/// <param name="mf">主窗体的引用</param>
		/// <param name="shFile">服务器文件名</param>
		/// <param name="slFile">本地文件名</param>
		/// <param name="bSwitch">传输开头</param>
		public CFile_Trans(MainForm mf, String shPath, String slPath, String sFile, byte bSwitch)
		{
			//
			// TODO: 在此处添加构造函数逻辑
			//
			theApp = mf;

			m_sIP = theApp.m_sIP;
			m_sPort = theApp.m_sPort2;
			m_sAccount = theApp.m_sAccount;
			m_sPwd = theApp.m_sPwd;
			m_shPath = shPath;
			m_slPath = slPath;
			m_sFile = sFile;
			m_bSwitch = bSwitch;
			m_Enable = false;
			m_sock = null;
		}

		public void close()
		{
			try
			{
				m_Enable = false;
				if (null != fs)
				{
					try{fs.Close();}
					catch{}
					fs = null;
				}
				m_sock.Close();
				threadproc.Abort();
			}
			finally
			{System.GC.Collect();}
		}

		public void DoTrans()
		{
			//login host
			IPAddress hostIPAddress = IPAddress.Parse(m_sIP);
			try
			{
				NetworkStream stream = null;
				for ( short n = 0; n < 10; n++)
				{
					try
					{
						if (m_sock == null)
						{
							m_sock = new TcpClient();
							m_sock.Connect(hostIPAddress, Int32.Parse(m_sPort));
						}
						stream = m_sock.GetStream();
						if (null != stream)
							break;
					}
					catch
					{Thread.Sleep(1000);continue;}
				}

				if (null == stream)
					return;
				
				if (!ParseCMSG_SendPacket(stream, defcmd.CONNECT, null))
					return;
				ThreadRecvCMSG_FilePacket(stream);
				if (!m_Enable)
					return;
				if (!ParseCMSG_SendPacket(stream, defcmd.CWD, Encoding.GetEncoding(936).GetBytes(m_shPath+"\n")))
					return;
				if (!ThreadRecvCMSG_FilePacket(stream))
					return;
				if (0x1 == m_bSwitch)		//上传文件请求
				{
					ParseCMSG_SendPacket(stream, defcmd.UPF, Encoding.GetEncoding(936).GetBytes(m_sFile+"\n"));
					if (!ThreadRecvCMSG_FilePacket(stream))
						return;
					fs = new FileStream(m_slPath+"//"+m_sFile, FileMode.Open, FileAccess.Read);
					r = new BinaryReader(fs);
				}
				else						//下载文件请求
				{
					ParseCMSG_SendPacket(stream, defcmd.DOF, Encoding.GetEncoding(936).GetBytes(m_sFile+"\n"));
					if (!ThreadRecvCMSG_FilePacket(stream))
						return;			
					fs = new FileStream(m_slPath+"//"+m_sFile, FileMode.CreateNew);
					w = new BinaryWriter(fs);
				}

				nfindex = 1;
				//init param
				Int32 i;
				int nTotal;
				int nCmd;
				int nSeq;

				byte []tmp = null;

				MemoryStream ms = null;
				while (m_Enable)
				{

					Thread.Sleep(50);
					try
					{
						if (ms != null)
						{
							ms.Position = 0;
							ms.Read(tmp, 0, 4);
							nTotal = BitConverter.ToInt32(tmp, 0);
							if (ms.Length == nTotal && ms.Length > 0)
							{
								tmp = new byte[nTotal-4];
								if ((i = ms.Read(tmp, 0, tmp.Length)) == tmp.Length)
								{
									nCmd = BitConverter.ToInt32(tmp, 0);
									nSeq = BitConverter.ToInt32(tmp, 4);

									ParseCMSG_FileCmd(nCmd, nSeq, defcmd.b2b(tmp, 8, tmp.Length-8));
									ms.Close();
									ms = null;
								}
							}
						}

						if (0x1 == m_bSwitch)		//上传文件
							ParseCMSG_SendPacket(stream, defcmd.UPF_BODY, null);
						else						//下载文件
							ParseCMSG_SendPacket(stream, defcmd.DOF_BODY, null);

						if (!stream.DataAvailable)
							continue;

						MainForm.logs("recv data!");

						tmp = new byte[4];

						if ((i = stream.Read(tmp, 0, tmp.Length)) == 4)
						{
							if (null == ms)
								ms = new MemoryStream();
							else
								ms.Position = ms.Length;
							ms.Write(tmp, 0, i);
						}
						stream.Flush();
						tmp = new byte[BitConverter.ToInt32(tmp, 0)-4];
						if ((i = stream.Read(tmp, 0, tmp.Length)) == tmp.Length)
							ms.Write(tmp, 0, tmp.Length);
						else
							ms.Write(tmp, 0, i);


					}
					catch(ArgumentOutOfRangeException aore)
					{
						MainForm.logs(String.Format("(ThreadRecvCMSG_Packet)Source:{0},TargetSite{1},Message{2}", aore.Source, aore.TargetSite, aore.Message));
						aore = null;
						break;
					}
					catch(IOException ioe)
					{
						MainForm.logs(String.Format("(ThreadRecvCMSG_Packet)Source:{0},TargetSite{1},Message{2}", ioe.Source, ioe.TargetSite, ioe.Message));
						ioe = null;
						break;
					}			
					catch(Exception ep)
					{
						MainForm.logs(String.Format("(ThreadRecvCMSG_Packet)Source:{0},TargetSite{1},Message{2}", ep.Source, ep.TargetSite, ep.Message));
						ep = null;
						break;
					}
				}
			}
			catch
			{}
			return;
		}

		/// <summary>
		/// 向服务器发送数据包
		/// </summary>
		/// <param name="stream"></param>
		/// <param name="cmd"></param>
		/// <param name="Packet"></param>
		/// <returns></returns>
		public bool ParseCMSG_SendPacket(NetworkStream stream, int cmd, byte [] Packet)
		{
			try
			{
				if (null == stream)
					return false;

				MemoryStream ms = null;
				byte [] bTmp = null;
				switch(cmd)
				{
					case defcmd.CONNECT:
						ms = new MemoryStream();
						//打包帐号
						bTmp = Encoding.GetEncoding(936).GetBytes(String.Format("{0}\n", m_sAccount));
						ms.Write(bTmp, 0, bTmp.Length);
						String szTimestamp = DateTime.Now.ToString("MMddHHmmss");
						//打包AuthenticatorSource
						bTmp = Encoding.GetEncoding(936).GetBytes(String.Format("{0}000000000{1}000000000{2}", m_sAccount, m_sPwd, szTimestamp));
						// This is one implementation of the abstract class MD5.
						MD5 md5 = new MD5CryptoServiceProvider();
						bTmp = md5.ComputeHash(bTmp);
						ms.Write(bTmp, 0, bTmp.Length);

						ms.WriteByte(10);
						//打包版本号
						ms.WriteByte(defcmd.Version);
						//打包时间
						bTmp = Encoding.GetEncoding(936).GetBytes(szTimestamp);
						ms.Write(bTmp, 0, bTmp.Length);
						///组装包头
						bTmp = new byte[12+ms.Length];
						Buffer.BlockCopy(BitConverter.GetBytes(bTmp.Length), 0, bTmp, 0, 4);
						//命令类型
						Buffer.BlockCopy(BitConverter.GetBytes(defcmd.CONNECT), 0, bTmp, 4, 4);
						//seq
						Buffer.BlockCopy(BitConverter.GetBytes(0), 0, bTmp, 8, 4);
						//请求认证
						Buffer.BlockCopy(ms.ToArray(), 0, bTmp, 12, (int)ms.Length);
						stream.Write(bTmp, 0, bTmp.Length);
						ms.Close();

						//等待回复信息
						stream.Flush();
						MainForm.logs("send data!CONNECT");
						return true;
					case defcmd.CWD:
						bTmp = new byte[20+Packet.Length];
						Buffer.BlockCopy(BitConverter.GetBytes(bTmp.Length), 0, bTmp, 0, 4);
						//命令类型
						Buffer.BlockCopy(BitConverter.GetBytes(defcmd.CWD), 0, bTmp, 4, 4);
						//seq
						Buffer.BlockCopy(BitConverter.GetBytes(0), 0, bTmp, 8, 4);
						//时间
						Buffer.BlockCopy(Encoding.GetEncoding(936).GetBytes(DateTime.Now.ToString("yyyyMMdd")), 0, bTmp, 12, 8);
						//申请改变当前路径

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -