DllSocket.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Net;
  5. using System.Net.Configuration;
  6. using System.Net.Sockets;
  7. using System.Text;
  8. using System.Threading;
  9. namespace DllFileSoc
  10. {
  11. public class DllSocket
  12. {
  13. //public bool UploadFiles(string ip,string port, string clientfilename, string clientpath,ref string msg)
  14. //{
  15. // Thread threadClient = null; // 创建用于接收服务端消息的 线程;
  16. // IPEndPoint endPoint = null;
  17. // Socket sockClient = null;
  18. // try
  19. // {
  20. // sockClient = RefSocket(ip, port,ref endPoint, ref msg);
  21. // if (sockClient==null)
  22. // {
  23. // LogBLL.Err(msg);
  24. // return false;
  25. // }
  26. // else//与服务器建立通信
  27. // {
  28. // try
  29. // {
  30. // sockClient.Connect(endPoint);
  31. // }
  32. // catch (SocketException se)
  33. // {
  34. // LogBLL.Err(sockClient.LocalEndPoint.ToString(), se);
  35. // return false;
  36. // }
  37. // LogBLL.Err("与服务器连接成功!" + sockClient.LocalEndPoint.ToString());
  38. // //threadClient = new Thread(RecMsg);
  39. // //threadClient.IsBackground = true;
  40. // //threadClient.Start();
  41. // return true;
  42. // }
  43. // }
  44. // catch (Exception ex)
  45. // {
  46. // LogBLL.Err(msg, ex);
  47. // return false;
  48. // }
  49. //}
  50. /// <summary>
  51. /// 上传文件保存到服务器
  52. /// </summary>
  53. /// <param name="IP">服务监听IP地址</param>
  54. /// <param name="Port">服务监听端口</param>
  55. /// <param name="path">上传文件的文件夹目录</param>
  56. /// <param name="filename">需要上传的文件名</param>
  57. /// <param name="msg">消息</param>
  58. /// <returns></returns>
  59. public static bool SendFile(string IP, int Port,string path, string filename ,string macid,string userid, ref string msg)
  60. {
  61. //string serverfile = "D:\\SocketServerFile\\WeChatSetup.exe";
  62. //创建一个文件对象
  63. FileInfo EzoneFile = new FileInfo(path+filename);
  64. //打开文件流
  65. FileStream EzoneStream = EzoneFile.OpenRead();
  66. long size = EzoneStream.Length;
  67. byte[] array = new byte[size];//文件
  68. EzoneStream.Read(array, 0, array.Length);
  69. string md5 = GetMD5HashFromFile(array);
  70. //包的大小
  71. int PacketSize = 1024*1024*2;
  72. //包的数量
  73. int PacketCount = (int)(EzoneStream.Length / ((long)PacketSize));
  74. //最后一个包的大小
  75. int LastDataPacket = (int)(EzoneStream.Length - ((long)(PacketSize * PacketCount)));
  76. //指向远程服务端节点
  77. IPEndPoint ipep = new IPEndPoint(IPAddress.Parse(IP), Port);
  78. //创建套接字
  79. Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  80. //连接到发送端
  81. try
  82. {
  83. client.Connect(ipep);
  84. }
  85. catch
  86. {
  87. msg ="服务器连接失败!:"+ IP+":"+Port;
  88. return false;
  89. }
  90. //获得客户端节点对象
  91. IPEndPoint clientep = (IPEndPoint)client.RemoteEndPoint;
  92. //发送[文件名]
  93. //TransferFiles.SendVarData(client, System.Text.Encoding.UTF8.GetBytes(EzoneFile.Name));
  94. TransferFiles.SendVarData(client, System.Text.Encoding.UTF8.GetBytes("<--上传-->"));
  95. TransferFiles.SendVarData(client, System.Text.Encoding.UTF8.GetBytes(filename));
  96. //发送macid\userid\md5
  97. TransferFiles.SendVarData(client, System.Text.Encoding.UTF8.GetBytes(macid));
  98. TransferFiles.SendVarData(client, System.Text.Encoding.UTF8.GetBytes(userid));
  99. TransferFiles.SendVarData(client, System.Text.Encoding.UTF8.GetBytes(md5));
  100. //发送[包的大小]
  101. TransferFiles.SendVarData(client, System.Text.Encoding.UTF8.GetBytes(PacketSize.ToString()));
  102. //发送[包的总数量]
  103. TransferFiles.SendVarData(client, System.Text.Encoding.UTF8.GetBytes(PacketCount.ToString()));
  104. //发送[最后一个包的大小]
  105. TransferFiles.SendVarData(client, System.Text.Encoding.UTF8.GetBytes(LastDataPacket.ToString()));
  106. bool isCut = false;
  107. //数据包
  108. byte[] data = new byte[PacketSize];
  109. //开始循环发送数据包
  110. for (int i = 0; i < PacketCount; i++)
  111. {
  112. //从文件流读取数据并填充数据包
  113. EzoneStream.Read(data, 0, data.Length);
  114. //发送数据包
  115. if (TransferFiles.SendVarData(client, data) == 3)
  116. {
  117. isCut = true;
  118. return false;
  119. //break;
  120. }
  121. }
  122. //如果还有多余的数据包,则应该发送完毕!
  123. if (LastDataPacket != 0)
  124. {
  125. data = new byte[LastDataPacket];
  126. EzoneStream.Read(data, 0, data.Length);
  127. TransferFiles.SendVarData(client, data);
  128. }
  129. //关闭文件流
  130. EzoneStream.Close();
  131. //关闭套接字
  132. client.Close();
  133. if (!isCut)
  134. {
  135. msg = "文件[" + filename + "]上传成功!";
  136. return true;
  137. }
  138. return false;
  139. }
  140. /// <summary>
  141. /// 从文件服务下载文件
  142. /// </summary>
  143. /// <param name="IP">服务监听IP地址</param>
  144. /// <param name="Port">服务监听端口</param>
  145. /// <param name="path">下载文件保存到本地的目录</param>
  146. /// <param name="filename">服务器被下载的文件的完整路径</param>
  147. /// <param name="msg">消息</param>
  148. /// <returns></returns>
  149. public static bool DownFile(string IP, int Port, string path, string filename, ref string msg)
  150. {
  151. //指向远程服务端节点
  152. IPEndPoint ipep = new IPEndPoint(IPAddress.Parse(IP), Port);
  153. //创建套接字
  154. Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  155. //连接到发送端
  156. try
  157. {
  158. client.Connect(ipep);
  159. }
  160. catch
  161. {
  162. msg = "服务器连接失败!:" + IP + ":" + Port;
  163. return false;
  164. }
  165. //获得客户端节点对象
  166. IPEndPoint clientep = (IPEndPoint)client.RemoteEndPoint;
  167. //发送[下载指令]
  168. TransferFiles.SendVarData(client, System.Text.Encoding.UTF8.GetBytes("<--下载-->"));
  169. TransferFiles.SendVarData(client, System.Text.Encoding.UTF8.GetBytes(filename));
  170. //获得[文件名]
  171. string SendFileName = System.Text.Encoding.UTF8.GetString(TransferFiles.ReceiveVarData(client));
  172. //获得[包的大小]
  173. string bagSize = System.Text.Encoding.UTF8.GetString(TransferFiles.ReceiveVarData(client));
  174. //获得[包的总数量]
  175. int bagCount = int.Parse(System.Text.Encoding.UTF8.GetString(TransferFiles.ReceiveVarData(client)));
  176. //获得[最后一个包的大小]
  177. string bagLast = System.Text.Encoding.UTF8.GetString(TransferFiles.ReceiveVarData(client));
  178. string fullPath = Path.Combine(Environment.CurrentDirectory, path + SendFileName);
  179. try
  180. {
  181. //创建一个新文件
  182. FileStream MyFileStream = new FileStream(fullPath, FileMode.Create, FileAccess.Write);
  183. //已发送包的个数
  184. int SendedCount = 0;
  185. while (true)
  186. {
  187. byte[] data = TransferFiles.ReceiveVarData(client);
  188. if (data.Length == 0)
  189. {
  190. break;
  191. }
  192. else
  193. {
  194. SendedCount++;
  195. //将接收到的数据包写入到文件流对象
  196. MyFileStream.Write(data, 0, data.Length);
  197. }
  198. }
  199. //关闭文件流
  200. MyFileStream.Close();
  201. //关闭套接字
  202. client.Close();
  203. msg = "文件[" + filename + "]下载成功!";
  204. return true;
  205. }
  206. catch (Exception ex)
  207. {
  208. msg = "文件[" + filename + "]下载失败!"+ex.Message;
  209. return false;
  210. }
  211. }
  212. //Socket RefSocket(string ip,string port,ref IPEndPoint endPoint, ref string msg)
  213. //{
  214. // try
  215. // {
  216. // IPAddress endip = IPAddress.Parse(ip);
  217. // endPoint = new IPEndPoint(endip, int.Parse(port));
  218. // return new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  219. // }
  220. // catch (Exception ex)
  221. // {
  222. // msg = ex.Message;
  223. // return null;
  224. // }
  225. //}
  226. public static string GetMD5HashFromFile(byte[] filedatas)
  227. {
  228. try
  229. {
  230. System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
  231. byte[] retVal = md5.ComputeHash(filedatas);
  232. StringBuilder sb = new StringBuilder();
  233. for (int i = 0; i < retVal.Length; i++)
  234. {
  235. sb.Append(retVal[i].ToString("x2"));
  236. }
  237. return sb.ToString();
  238. }
  239. catch (Exception ex)
  240. {
  241. //errorinfo = ex.Message.ToString();
  242. return "";
  243. }
  244. }
  245. }
  246. }