SocketClient.cs 20 KB

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