C# 处理TCP数据的类(服务端)
作者:互联网
1 using System; 2 using System.Collections.Generic; 3 using System.Net; 4 using System.Net.Sockets; 5 using System.Text; 6 using System.Threading; 7 8 namespace TestDemo 9 { 10 /// <summary> 11 /// 处理TCP数据的类 12 /// </summary> 13 public class TcpService 14 { 15 /// <summary> 16 /// TCP监听对象 17 /// </summary> 18 private TcpListener listener; 19 20 /// <summary> 21 /// 创建TCP监听 22 /// </summary> 23 public void CreateListener() 24 { 25 try 26 { 27 // 监听对象 28 listener = new TcpListener(IPAddress.Any, API.TcpPort); 29 listener.Start(); 30 // 独立线程监听 31 Thread thread = new Thread(StartListener); 32 thread.Start(); 33 } 34 catch (Exception) 35 { 36 throw; 37 } 38 } 39 40 /// <summary> 41 /// 开始监听 42 /// </summary> 43 private void StartListener() 44 { 45 while (true) 46 { 47 try 48 { 49 // TCP监听 50 TcpClient tcpClient = listener.AcceptTcpClient(); 51 // 多线程处理数据 52 Thread thread = new Thread(new ParameterizedThreadStart(delegate { DealTcpData(tcpClient); })); 53 thread.Start(); 54 } 55 catch (Exception ex) 56 { 57 Console.WriteLine(ex); 58 } 59 } 60 } 61 62 /// <summary> 63 /// 处理TCP数据 64 /// <para>lv结构:数据的前4个字节(int),记录了数据区的长度</para> 65 /// <para>注意:数据结构根据实际使用情况而定</para> 66 /// </summary> 67 private void DealTcpData(TcpClient tcpClient) 68 { 69 try 70 { 71 if (tcpClient.Connected) 72 { 73 // 取得流 74 NetworkStream networkStream = tcpClient.GetStream(); 75 networkStream.ReadTimeout = 500; 76 networkStream.WriteTimeout = 500; 77 #region 接收数据 78 // 接收数据储存 79 List<byte> list = new List<byte>(); 80 // 数据区总长度 81 byte[] lenArr = new byte[4]; 82 networkStream.Read(lenArr, 0, lenArr.Length); 83 int dataLen = BitConverter.ToInt32(lenArr, 0); 84 // 读取数据区数据 85 int total = 0; 86 // 每次读取的数据大小 87 int arrLen = 1024; 88 while (true) 89 { 90 // 读取数据 91 byte[] arr = new byte[arrLen]; 92 int len = networkStream.Read(arr, 0, arr.Length); 93 for (int i = 0; i < len; i++) 94 { 95 list.Add(arr[i]); 96 } 97 // 判断数据的是否读取完成 98 total += len; 99 if (dataLen - total <= 0) 100 { 101 break; 102 } 103 if (dataLen - total < arrLen) 104 { 105 arrLen = dataLen - total; 106 } 107 Thread.Sleep(0); 108 } 109 // 根据功能或实际情况处理接收的数据 110 byte[] receiveData = list.ToArray(); 111 #endregion 112 #region 发送数据 113 // 取得数据 114 // 根据功能或实际情况做成需要发送的数据 115 byte[] dataArr = new byte[] { 0x01, 0x02, 0x03, 0x04 }; // 测试数据 116 if (dataArr != null) 117 { 118 // 数据长度 119 byte[] lenArr = BitConverter.GetBytes(dataArr.Length); 120 // 拼接数据头(lv结构) 121 byte[] sendArr = new byte[lenArr.Length + dataArr.Length]; 122 lenArr.CopyTo(sendArr, 0); 123 dataArr.CopyTo(sendArr, 4); 124 // 发送数据 125 try 126 { 127 lock (networkStream) 128 { 129 networkStream.Write(sendArr, 0, sendArr.Length); 130 } 131 } 132 catch { } 133 } 134 #endregion 135 } 136 } 137 catch (Exception ex) 138 { 139 // 错误日志 140 Log.WriteError(ex); 141 } 142 finally 143 { 144 // 判断TCP对象是否连接 145 if (tcpClient != null) 146 { 147 JudgeTcpConnection(tcpClient); 148 } 149 } 150 } 151 152 /// <summary> 153 /// 判断TCP对象是否连接 154 /// </summary> 155 private void JudgeTcpConnection(TcpClient tcpClient, int timeout = 3) 156 { 157 try 158 { 159 DateTime time = DateTime.Now; 160 while (true) 161 { 162 // 超时时间判断 163 if (time.AddSeconds(timeout) < DateTime.Now) 164 { 165 break; 166 } 167 // 连接状态判断 168 if (!tcpClient.Connected) 169 { 170 break; 171 } 172 else 173 { 174 bool flag = tcpClient.Client.Poll(1000, SelectMode.SelectRead); 175 if (flag) 176 { 177 break; 178 } 179 } 180 Thread.Sleep(0); 181 } 182 } 183 catch (Exception ex) 184 { 185 // 错误日志 186 Log.WriteError(ex); 187 } 188 finally 189 { 190 // 关闭连接 191 tcpClient.Close(); 192 } 193 } 194 } 195 }
搜索
复制
标签:tcpClient,C#,System,TCP,int,using,new,服务端 来源: https://www.cnblogs.com/smartnn/p/16635584.html