| | |
| | | } |
| | | finally |
| | | { |
| | | heartBeatThread = null; |
| | | if (heartBeatThread != null) |
| | | { |
| | | heartBeatThread = null; |
| | | } |
| | | HeartBeat(); |
| | | } |
| | | } |
| | |
| | | { |
| | | receiveThread = new Thread(() => |
| | | { |
| | | while (isConnected) |
| | | try |
| | | { |
| | | if (_tcpClient == null) |
| | | while (isConnected) |
| | | { |
| | | try |
| | | if (_tcpClient == null) |
| | | { |
| | | receiveThread?.Abort(); |
| | | } |
| | | catch { } |
| | | finally |
| | | { |
| | | receiveThread = null; |
| | | } |
| | | return; |
| | | } |
| | | if (!_tcpClient.Connected) |
| | | { |
| | | MainPage.Log("tcp客户端断开了连接..."); |
| | | isConnected = false; |
| | | return; |
| | | } |
| | | try |
| | | { |
| | | // 定义一个2M的缓存区; |
| | | byte[] arrMsgRec = new byte[1024 * 1024 * 2]; |
| | | try |
| | | { |
| | | int size = _tcpClient.GetStream().Read(arrMsgRec, 0, arrMsgRec.Length); |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | isConnected = false; |
| | | MainPage.Log($"局域网tcp数据接收异常:{ex.Message}"); |
| | | try |
| | | { |
| | | receiveThread?.Abort(); |
| | | } |
| | | catch { } |
| | | finally |
| | | { |
| | | receiveThread = null; |
| | | } |
| | | return; |
| | | } |
| | | var tcpDataString = System.Text.Encoding.UTF8.GetString(arrMsgRec, 0, arrMsgRec.Length); |
| | | |
| | | |
| | | if (!string.IsNullOrEmpty(tcpDataString)) |
| | | if (!_tcpClient.Connected) |
| | | { |
| | | MainPage.Log($"局域网tcp数据接收"); |
| | | Control.Ins.ConvertReceiveData(arrMsgRec, null); |
| | | |
| | | MainPage.Log("tcp客户端断开了连接..."); |
| | | isConnected = false; |
| | | return; |
| | | } |
| | | } |
| | | catch (Exception) { } |
| | | } |
| | | try |
| | | { |
| | | // 定义一个2M的缓存区; |
| | | byte[] arrMsgRec = new byte[1024 * 1024 * 2]; |
| | | try |
| | | { |
| | | int size = _tcpClient.GetStream().Read(arrMsgRec, 0, arrMsgRec.Length); |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | isConnected = false; |
| | | MainPage.Log($"局域网tcp数据接收异常:{ex.Message}"); |
| | | return; |
| | | } |
| | | var tcpDataString = System.Text.Encoding.UTF8.GetString(arrMsgRec, 0, arrMsgRec.Length); |
| | | |
| | | |
| | | if (!string.IsNullOrEmpty(tcpDataString)) |
| | | { |
| | | MainPage.Log($"局域网tcp数据接收"); |
| | | Control.Ins.ConvertReceiveData(arrMsgRec, null); |
| | | |
| | | } |
| | | } |
| | | catch (Exception) { } |
| | | } |
| | | }catch (Exception ex) |
| | | { |
| | | MainPage.Log($"tcp ReceiveMessage error :{ex.Message}"); |
| | | } |
| | | }); |
| | | //receiveThread.IsBackground = true; |
| | | receiveThread.Start(); |