SocketClient.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  1. using System;
  2. using System.IO;
  3. using System.Net;
  4. using System.Net.Sockets;
  5. using System.Runtime.Serialization;
  6. using System.Runtime.Serialization.Formatters.Binary;
  7. using System.Text;
  8. using System.Threading;
  9. namespace Masuit.Tools.Net
  10. {
  11. /// <summary>
  12. /// Socket客户端操作类
  13. /// </summary>
  14. public static class SocketClient
  15. {
  16. #region 私有字段
  17. /// <summary>
  18. /// 设置数据缓冲区大小 默认1024
  19. /// </summary>
  20. private static readonly int m_maxpacket = 1024 * 4;
  21. #endregion
  22. #region 服务器侦听
  23. /// <summary>
  24. /// 服务器侦听方法 返回null则说明没有链接上
  25. /// </summary>
  26. /// <returns>返回一个套接字(Socket)</returns>
  27. public static Socket ListenerSocket(this TcpListener listener)
  28. {
  29. try
  30. {
  31. return listener.AcceptSocket();
  32. }
  33. catch (Exception)
  34. {
  35. return null;
  36. }
  37. }
  38. /// <summary>
  39. /// 服务器侦听方法 返回null则说明没有链接上
  40. /// </summary>
  41. /// <param name="listener">TCP监听对象</param>
  42. /// <returns>返回一个网络流</returns>
  43. public static NetworkStream ListenerStream(this TcpListener listener)
  44. {
  45. try
  46. {
  47. return listener.AcceptTcpClient().GetStream();
  48. }
  49. catch (Exception)
  50. {
  51. return null;
  52. }
  53. }
  54. #endregion
  55. #region 客户端连接
  56. /// <summary>
  57. /// 从客户端连接获取socket对象
  58. /// </summary>
  59. /// <param name="tcpclient">TCP客户端</param>
  60. /// <param name="ipendpoint">客户端节点</param>
  61. /// <returns>客户端socket</returns>
  62. public static Socket ConnectSocket(this TcpClient tcpclient, IPEndPoint ipendpoint)
  63. {
  64. try
  65. {
  66. tcpclient.Connect(ipendpoint);
  67. return tcpclient.Client;
  68. }
  69. catch (Exception)
  70. {
  71. return null;
  72. }
  73. }
  74. /// <summary>
  75. /// 从客户端连接获取socket对象
  76. /// </summary>
  77. /// <param name="tcpclient">TCP客户端</param>
  78. /// <param name="ipadd">IP地址</param>
  79. /// <param name="port">端口号</param>
  80. /// <returns>客户端socket</returns>
  81. public static Socket ConnectSocket(this TcpClient tcpclient, IPAddress ipadd, int port)
  82. {
  83. try
  84. {
  85. tcpclient.Connect(ipadd, port);
  86. return tcpclient.Client;
  87. }
  88. catch (Exception)
  89. {
  90. return null;
  91. }
  92. }
  93. /// <summary>
  94. /// 从客户端获取网络流对象
  95. /// </summary>
  96. /// <param name="tcpclient">TCP客户端</param>
  97. /// <param name="ipendpoint">客户端节点</param>
  98. /// <returns>客户端的网络流</returns>
  99. public static NetworkStream ConnectStream(this TcpClient tcpclient, IPEndPoint ipendpoint)
  100. {
  101. try
  102. {
  103. tcpclient.Connect(ipendpoint);
  104. return tcpclient.GetStream();
  105. }
  106. catch (Exception)
  107. {
  108. return null;
  109. }
  110. }
  111. /// <summary>
  112. /// 从客户端获取网络流对象
  113. /// </summary>
  114. /// <param name="tcpclient">TCP客户端</param>
  115. /// <param name="ipadd">IP地址</param>
  116. /// <param name="port">端口号</param>
  117. /// <returns>客户端网络流对象</returns>
  118. public static NetworkStream ConnectStream(this TcpClient tcpclient, IPAddress ipadd, int port)
  119. {
  120. try
  121. {
  122. tcpclient.Connect(ipadd, port);
  123. return tcpclient.GetStream();
  124. }
  125. catch (Exception)
  126. {
  127. return null;
  128. }
  129. }
  130. #endregion
  131. #region Socket接收数据
  132. /// <summary>
  133. /// 接受固定长度字符串
  134. /// </summary>
  135. /// <param name="socket">socket对象</param>
  136. /// <param name="size">字符串长度</param>
  137. /// <returns>字节数据</returns>
  138. public static byte[] ReceiveFixData(this Socket socket, int size)
  139. {
  140. int offset = 0;
  141. int dataleft = size;
  142. byte[] msg = new byte[size];
  143. while (dataleft > 0)
  144. {
  145. var recv = socket.Receive(msg, offset, dataleft, 0);
  146. if (recv == 0)
  147. {
  148. break;
  149. }
  150. offset += recv;
  151. dataleft -= recv;
  152. }
  153. return msg;
  154. }
  155. /// <summary>
  156. /// 接收变长字符串
  157. /// 为了处理粘包问题 ,每次发送数据时 包头(数据字节长度) + 正文
  158. /// 这个发送小数据
  159. /// 设置包头的字节为8,不能超过8位数的字节数组
  160. /// </summary>
  161. /// <param name="socket">客户端socket</param>
  162. /// <returns>byte[]数组</returns>
  163. public static byte[] ReceiveVarData(this Socket socket)
  164. {
  165. //每次接受数据时,接收固定长度的包头,包头长度为8
  166. byte[] lengthbyte = ReceiveFixData(socket, 8);
  167. //length得到字符长度 然后加工处理得到数字
  168. int length = GetPacketLength(lengthbyte);
  169. //得到正文
  170. return ReceiveFixData(socket, length);
  171. }
  172. /// <summary>
  173. /// 接收T类对象,反序列化
  174. /// </summary>
  175. /// <typeparam name="T">接收T类对象,T类必须是一个可序列化类</typeparam>
  176. /// <param name="socket">客户端socket</param>
  177. /// <returns>强类型对象</returns>
  178. public static T ReceiveVarData<T>(this Socket socket)
  179. {
  180. //先接收包头长度 固定8个字节
  181. byte[] lengthbyte = ReceiveFixData(socket, 8);
  182. //得到字节长度
  183. int length = GetPacketLength(lengthbyte);
  184. byte[] bytecoll = new byte[m_maxpacket];
  185. IFormatter format = new BinaryFormatter();
  186. MemoryStream stream = new MemoryStream();
  187. int offset = 0; //接收字节个数
  188. int lastdata = length; //还剩下多少没有接收,初始大小等于实际大小
  189. int receivedata = m_maxpacket; //每次接收大小
  190. //循环接收
  191. int mark = 0; //标记几次接收到的数据为0长度
  192. while (true)
  193. {
  194. //剩下的字节数是否小于缓存大小
  195. if (lastdata < m_maxpacket)
  196. {
  197. receivedata = lastdata; //就只接收剩下的字节数
  198. }
  199. int count = socket.Receive(bytecoll, 0, receivedata, 0);
  200. if (count > 0)
  201. {
  202. stream.Write(bytecoll, 0, count);
  203. offset += count;
  204. lastdata -= count;
  205. mark = 0;
  206. }
  207. else
  208. {
  209. mark++;
  210. if (mark == 10)
  211. {
  212. break;
  213. }
  214. }
  215. if (offset == length)
  216. {
  217. break;
  218. }
  219. }
  220. stream.Seek(0, SeekOrigin.Begin); //必须要这个 或者stream.Position = 0;
  221. T t = (T)format.Deserialize(stream);
  222. return t;
  223. }
  224. /// <summary>
  225. /// 在预先得到文件的文件名和大小
  226. /// 调用此方法接收文件
  227. /// </summary>
  228. /// <param name="socket">socket服务端</param>
  229. /// <param name="path">路径必须存在</param>
  230. /// <param name="filename">文件名</param>
  231. /// <param name="size">预先知道的文件大小</param>
  232. /// <param name="progress">处理过程</param>
  233. public static bool ReceiveFile(this Socket socket, string path, string filename, long size, Action<int> progress)
  234. {
  235. if (!Directory.Exists(path))
  236. {
  237. return false;
  238. }
  239. //主要是防止有重名文件
  240. string savepath = GetPath(path, filename); //得到文件路径
  241. //缓冲区
  242. byte[] file = new byte[m_maxpacket];
  243. int receivedata = m_maxpacket; //每次要接收的长度
  244. long offset = 0; //循环接收的总长度
  245. long lastdata = size; //剩余多少还没接收
  246. int mark = 0;
  247. using var fs = new FileStream(savepath, FileMode.OpenOrCreate, FileAccess.Write);
  248. if (size <= 0)
  249. {
  250. return false;
  251. }
  252. bool ret = false;
  253. while (true)
  254. {
  255. if (lastdata < receivedata)
  256. {
  257. receivedata = Convert.ToInt32(lastdata);
  258. }
  259. var count = socket.Receive(file, 0, receivedata, SocketFlags.None); //每次接收的实际长度
  260. if (count > 0)
  261. {
  262. fs.Write(file, 0, count);
  263. offset += count;
  264. lastdata -= count;
  265. mark = 0;
  266. }
  267. else
  268. {
  269. mark++; //连续5次接收为0字节 则跳出循环
  270. if (mark == 10)
  271. {
  272. break;
  273. }
  274. }
  275. //接收进度
  276. progress(Convert.ToInt32(Convert.ToDouble(offset) / Convert.ToDouble(size) * 100));
  277. //接收完毕
  278. if (offset == size)
  279. {
  280. ret = true;
  281. break;
  282. }
  283. }
  284. return ret;
  285. }
  286. /// <summary>
  287. /// 从socket服务端接收文件
  288. /// </summary>
  289. /// <param name="socket">socket服务端</param>
  290. /// <param name="path">文件保存路径(必须存在)</param>
  291. /// <param name="filename">文件名</param>
  292. /// <param name="size">预先知道的文件大小</param>
  293. /// <returns>处理结果</returns>
  294. public static bool ReceiveFile(this Socket socket, string path, string filename, long size)
  295. {
  296. return ReceiveFile(socket, path, filename, size, null);
  297. }
  298. /// <summary>
  299. /// 预先不知道文件名和文件大小 用此方法接收
  300. /// 此方法对于的发送方法是SendFile()
  301. /// </summary>
  302. /// <param name="socket">socket服务端</param>
  303. /// <param name="path">要保存的目录</param>
  304. public static void ReceiveFile(this Socket socket, string path)
  305. {
  306. //得到包头信息字节数组 (文件名 + 文件大小 的字符串长度)
  307. //取前8位
  308. byte[] info_bt = ReceiveFixData(socket, 8);
  309. //得到包头信息字符长度
  310. int info_length = GetPacketLength(info_bt);
  311. //提取包头信息,(文件名 + 文件大小 的字符串长度)
  312. byte[] info = ReceiveFixData(socket, info_length);
  313. //得到文件信息字符串 (文件名 + 文件大小)
  314. string info_str = Encoding.UTF8.GetString(info);
  315. string[] strs = info_str.Split('|');
  316. string filename = strs[0]; //文件名
  317. long length = Convert.ToInt64(strs[1]); //文件大小
  318. //开始接收文件
  319. ReceiveFile(socket, path, filename, length);
  320. }
  321. private static int GetPacketLength(byte[] length)
  322. {
  323. string str = Encoding.UTF8.GetString(length);
  324. str = str.TrimEnd('*');
  325. return int.TryParse(str, out var _length) ? _length : 0;
  326. }
  327. private static int i;
  328. private static string markPath = string.Empty;
  329. /// <summary>
  330. /// 得到文件路径(防止有文件名重复)
  331. /// 如:aaa.txt已经在directory目录下存在,则会得到文件aaa(1).txt
  332. /// </summary>
  333. /// <param name="directory">目录名</param>
  334. /// <param name="file">文件名</param>
  335. /// <returns>文件路径</returns>
  336. public static string GetPath(string directory, string file)
  337. {
  338. if (markPath == string.Empty)
  339. {
  340. markPath = Path.Combine(directory, file);
  341. }
  342. string path = Path.Combine(directory, file);
  343. if (File.Exists(path))
  344. {
  345. i++;
  346. string filename = Path.GetFileNameWithoutExtension(markPath) + "(" + i + ")";
  347. string extension = Path.GetExtension(markPath);
  348. return GetPath(directory, filename + extension);
  349. }
  350. i = 0;
  351. markPath = string.Empty;
  352. return path;
  353. }
  354. #endregion
  355. #region Socket发送数据
  356. /// <summary>
  357. /// 发送固定长度消息
  358. /// 发送字节数不能大于int型最大值
  359. /// </summary>
  360. /// <param name="socket">源socket</param>
  361. /// <param name="msg">消息的字节数组</param>
  362. /// <returns>返回发送字节个数</returns>
  363. public static int SendFixData(this Socket socket, byte[] msg)
  364. {
  365. int size = msg.Length; //要发送字节长度
  366. int offset = 0; //已经发送长度
  367. int dataleft = size; //剩下字符
  368. int senddata = m_maxpacket; //每次发送大小
  369. while (true)
  370. {
  371. //如过剩下的字节数 小于 每次发送字节数
  372. if (dataleft < senddata)
  373. {
  374. senddata = dataleft;
  375. }
  376. int count = socket.Send(msg, offset, senddata, SocketFlags.None);
  377. offset += count;
  378. dataleft -= count;
  379. if (offset == size)
  380. {
  381. break;
  382. }
  383. }
  384. return offset;
  385. }
  386. /// <summary>
  387. /// 发送变长信息 格式 包头(包头占8位) + 正文
  388. /// </summary>
  389. /// <param name="socket">发送方socket对象</param>
  390. /// <param name="contact">发送文本</param>
  391. /// <returns>发送的数据内容长度</returns>
  392. public static int SendVarData(this Socket socket, string contact)
  393. {
  394. //得到字符长度
  395. int size = Encoding.UTF8.GetBytes(contact).Length;
  396. //包头字符
  397. string length = GetSendPacketLengthStr(size);
  398. //包头 + 正文
  399. byte[] sendbyte = Encoding.UTF8.GetBytes(length + contact);
  400. //发送
  401. return SendFixData(socket, sendbyte);
  402. }
  403. /// <summary>
  404. /// 发送变成信息
  405. /// </summary>
  406. /// <param name="socket">发送方socket对象</param>
  407. /// <param name="bytes">消息的 字节数组</param>
  408. /// <returns>消息长度</returns>
  409. public static int SendVarData(this Socket socket, byte[] bytes)
  410. {
  411. //得到包头字节
  412. int size = bytes.Length;
  413. string length = GetSendPacketLengthStr(size);
  414. byte[] lengthbyte = Encoding.UTF8.GetBytes(length);
  415. //发送包头
  416. SendFixData(socket, lengthbyte); //因为不知道正文是什么编码所以没有合并
  417. //发送正文
  418. return SendFixData(socket, bytes);
  419. }
  420. /// <summary>
  421. /// 发送T类型对象,序列化
  422. /// </summary>
  423. /// <typeparam name="T">T类型</typeparam>
  424. /// <param name="socket">发送方的socket对象</param>
  425. /// <param name="obj">T类型对象,必须是可序列化的</param>
  426. /// <returns>消息长度</returns>
  427. public static int SendSerializeObject<T>(this Socket socket, T obj)
  428. {
  429. byte[] bytes = SerializeObject(obj);
  430. return SendVarData(socket, bytes);
  431. }
  432. /// <summary>
  433. /// 发送文件
  434. /// </summary>
  435. /// <param name="socket">socket对象</param>
  436. /// <param name="path">文件路径</param>
  437. /// <param name="issend">是否发送文件(头)信息,如果当前知道文件[大小,名称]则为false</param>
  438. /// <param name="progress">处理过程</param>
  439. /// <returns>处理结果</returns>
  440. public static bool SendFile(this Socket socket, string path, bool issend, Action<int> progress)
  441. {
  442. if (!File.Exists(path))
  443. {
  444. return false;
  445. }
  446. var fileinfo = new FileInfo(path);
  447. string filename = fileinfo.Name;
  448. long length = fileinfo.Length;
  449. //发送文件信息
  450. if (issend)
  451. {
  452. SendVarData(socket, filename + "|" + length);
  453. }
  454. //发送文件
  455. long offset = 0;
  456. byte[] b = new byte[m_maxpacket];
  457. int mark = 0;
  458. using var fs = new FileStream(path, FileMode.Open, FileAccess.Read);
  459. int senddata = b.Length;
  460. //循环读取发送
  461. while (true)
  462. {
  463. int count = fs.Read(b, 0, senddata);
  464. if (count > 0)
  465. {
  466. socket.Send(b, 0, count, SocketFlags.None);
  467. offset += count;
  468. mark = 0;
  469. }
  470. else
  471. {
  472. mark++;
  473. if (mark == 10)
  474. {
  475. break;
  476. }
  477. }
  478. progress(Convert.ToInt32(Convert.ToDouble(offset) / Convert.ToDouble(length) * 100));
  479. if (offset == length)
  480. {
  481. return true;
  482. }
  483. Thread.Sleep(50); //设置等待时间,以免粘包
  484. }
  485. return false;
  486. }
  487. /// <summary>
  488. /// 发送文件,不需要进度信息
  489. /// </summary>
  490. /// <param name="socket">socket对象</param>
  491. /// <param name="path">文件路径</param>
  492. /// <param name="issend">是否发生(头)信息</param>
  493. /// <returns>处理结果</returns>
  494. public static bool SendFile(this Socket socket, string path, bool issend)
  495. {
  496. return SendFile(socket, path, issend, null);
  497. }
  498. /// <summary>
  499. /// 发送文件,不需要进度信息和(头)信息
  500. /// </summary>
  501. /// <param name="socket">socket对象</param>
  502. /// <param name="path">文件路径</param>
  503. /// <returns>处理结果</returns>
  504. public static bool SendFile(this Socket socket, string path)
  505. {
  506. return SendFile(socket, path, false, null);
  507. }
  508. private static byte[] SerializeObject(object obj)
  509. {
  510. IFormatter format = new BinaryFormatter();
  511. using var stream = new MemoryStream();
  512. format.Serialize(stream, obj);
  513. return stream.ToArray();
  514. }
  515. private static string GetSendPacketLengthStr(int size)
  516. {
  517. string length = size + "********"; //得到size的长度
  518. return length.Substring(0, 8); //截取前前8位
  519. }
  520. #endregion
  521. }
  522. }