12345678910111213141516171819202122232425262728293031323334353637383940414243 |
- using System;
- using System.IO;
- using System.Net.Sockets;
- using System.Text;
- namespace Team.Communicate.State
- {
- public class TcpClientState
- {
- public TcpClient TcpClient { get; }
- public Encoding Encoding { get; set; }
- /// <summary>
- /// 获取缓冲区
- /// </summary>
- public byte[] Buffer { get; private set; }
- /// <summary>
- /// 缓存数据写入内存流
- /// </summary>
- public MemoryStream Memory { get; set; }
- /// <summary>
- /// 获取网络流
- /// </summary>
- public NetworkStream NetworkStream => TcpClient.GetStream();
- public TcpClientState(TcpClient tcpClient, byte[] buffer)
- {
- TcpClient = tcpClient ?? throw new ArgumentNullException(nameof(tcpClient));
- Buffer = buffer ?? throw new ArgumentNullException(nameof(buffer));
- Memory = new MemoryStream();
- Encoding=Encoding.Default;
- }
- /// <summary>
- /// 关闭
- /// </summary>
- public void Close()
- {
- //关闭数据的接受和发送
- TcpClient.Close();
- Memory.Close();
- Buffer = null;
- }
- }
- }
|