TcpClientState.cs 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. using System;
  2. using System.IO;
  3. using System.Net.Sockets;
  4. using System.Text;
  5. namespace Team.Communicate.State
  6. {
  7. public class TcpClientState
  8. {
  9. public TcpClient TcpClient { get; }
  10. public Encoding Encoding { get; set; }
  11. /// <summary>
  12. /// 获取缓冲区
  13. /// </summary>
  14. public byte[] Buffer { get; private set; }
  15. /// <summary>
  16. /// 缓存数据写入内存流
  17. /// </summary>
  18. public MemoryStream Memory { get; set; }
  19. /// <summary>
  20. /// 获取网络流
  21. /// </summary>
  22. public NetworkStream NetworkStream => TcpClient.GetStream();
  23. public TcpClientState(TcpClient tcpClient, byte[] buffer)
  24. {
  25. TcpClient = tcpClient ?? throw new ArgumentNullException(nameof(tcpClient));
  26. Buffer = buffer ?? throw new ArgumentNullException(nameof(buffer));
  27. Memory = new MemoryStream();
  28. Encoding=Encoding.Default;
  29. }
  30. /// <summary>
  31. /// 关闭
  32. /// </summary>
  33. public void Close()
  34. {
  35. //关闭数据的接受和发送
  36. TcpClient.Close();
  37. Memory.Close();
  38. Buffer = null;
  39. }
  40. }
  41. }