| | |
| | | /// </summary> |
| | | public void Connect() |
| | | { |
| | | return; |
| | | lock (lockObj) |
| | | { |
| | | if (isConnected) |
| | |
| | | /// <param name="bytes">需要发送的字节</param> |
| | | public void SendMessage(byte[] bytes) |
| | | { |
| | | return; |
| | | #if __IOS__ |
| | | |
| | | #endif |
| | | if (heartBeatLogIdList.Count > 3) |
| | | { |
| | | try |
| | |
| | | } |
| | | try |
| | | { |
| | | if (_tcpClient == null) |
| | | { |
| | | return; |
| | | } |
| | | if (!_tcpClient.Connected) |
| | | { |
| | | return; |
| | | } |
| | | if (_tcpClient.GetStream().CanWrite&& isConnected) |
| | | { |
| | | _tcpClient.GetStream().Write(bytes, 0, bytes.Length); |
| | |
| | | private DateTime heartBeatTime; |
| | | public void HeartBeat() |
| | | { |
| | | return; |
| | | lock (lockObj) |
| | | { |
| | | if (heartBeatThread == null) |
| | |
| | | { |
| | | 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(); |