我有這個TcpClient的代碼工作正常。它連接到Linux系統上的perl服務器並接收任何服務器向其發送的消息。很好地工作。TcpClient與服務器通信保持在C#中的連接活動?
- 我的工作有關的常滴一些空閒時間,所以我一直在服務器實現的,所以當它接收PING它響應後連接:
在我有與它的一些問題,一定條件下public static void Main() { foreach (ProtocolConnection tcpConnection in TcpConnectionsList) { ProtocolConnection connection = tcpConnection; ThreadPool.QueueUserWorkItem(_ => { ThreadTcpClient(connection); ManualResetEventTcp.Set(); }); } ... Some code... } public static void TcpConnect(ProtocolConnection varConnection) { int retryCountSeconds = varConnection.RetryEverySeconds*Program.MilisecondsMultiplier; int count = 0; while (true) { try { using (var client = new TcpClient(varConnection.IpAddress.ToString(), varConnection.Port) { NoDelay = true }) using (var stream = client.GetStream()) { var data = new Byte[256]; while (!Program.PrepareExit) { Int32 bytes = stream.Read(data, 0, data.Length); string varReadData = Encoding.ASCII.GetString(data, 0, bytes).Trim(); if (varReadData != "" && varReadData != "PONG") { VerificationQueue.EnqueueData(varReadData); Logging.AddToLog("[TCP][" + varConnection.Name + "][DATA ARRIVED]" + varReadData); } else { Logging.AddToLog("[TCP]" + varReadData); } } } } catch (Exception e) { if (e.ToString().Contains("No connection could be made because the target machine actively refused it")) { Logging.AddToLog("[TCP][ERROR] Can't connect to server (" + varConnection.Name + ") " + varConnection.IpAddress + ":" + varConnection.Port); } else { Logging.AddToLog(e.ToString()); } } DateTime startTimeFunction = DateTime.Now; do { Thread.Sleep(1000); } while (((DateTime.Now - startTimeFunction).TotalSeconds < retryCountSeconds)); } }
然而與PONG。我可以使用UDP發送PING到服務器,並且它會在tcp上響應PONG,但我更喜歡內置到tcp客戶端的方式,因此它每隔60秒左右發送一次PING。即使UDP解決方案可以接受,我也沒有在
string varReadData = Encoding.ASCII.GetString(data, 0, bytes).Trim();
上超時,所以當PONG沒有到達時,我的客戶無論如何都沒有注意到它。它只是一直在等待...這使我想.. - 我的其他問題是,在某個點
string varReadData = Encoding.ASCII.GetString(data, 0, bytes).Trim();
這是一直在等待數據。當服務器崩潰或斷開我的客戶,我甚至沒有注意到。我想要服務器有某種timeout
或檢查連接是否處於活動狀態。如果它不活動,它應該嘗試重新連接。
什麼是最簡單的方法來解決這個TcpClient?我如何實現雙向通信,確保服務器斷開我的連接或我的網絡斷開連接的客戶端會注意到它並重新建立連接?
那麼這就是我想要的......有我自己的心跳在TCP上,只是我不知道如何做到這一點。將測試ReceiveTimeout如何工作,看看它是否工作,因爲我認爲它的確如此。如果它流了。讀取()放手,當沒有數據收到後,讓我們說50秒,然後我可以添加「發送消息PING」在代碼中,因爲服務器已經在等待。如果它收到PING,它會發送PONG,所以我會知道它是否工作。 – MadBoy 2010-06-26 17:18:29
'ReceiveTimeout'唯一的問題是它會拋出異常並丟棄連接,所以我將不得不使新的能夠發送心跳。 – MadBoy 2010-06-26 21:37:44