I have a TcpClient code that works fine. It connects to the perl server on a Linux system and receives everything that the server passes to it. It works beautifully.
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)); } }
However, under certain conditions, I have some problems:
- My working connection often disconnects the connection after some downtime, so I implemented it on the server, so when it receives PING, it answers PONG. I can send PING from UDP to the server and it will reply PONG to tcp, but I would prefer the built-in method to the tcp client so that it sends PING every 60 seconds or so. Even if the UDP solution is acceptable, I donβt have a timeout on the
string varReadData = Encoding.ASCII.GetString(data, 0, bytes).Trim();
, therefore, when PONG does not come, my client does not even notice it. He just keeps waiting ... that brings me to ... - My other problem is that at some point
string varReadData = Encoding.ASCII.GetString(data, 0, bytes).Trim();
it is waiting for data all the time. When the server crashes or shuts down my client, I donβt even notice it. I would like the server to have some kind of timeout
or check if the connection is enabled. If it is inactive, it should try to reconnect.
What would be the easiest way to fix this TcpClient? How to implement two-way communication, making sure that if the server refuses my connections or my network disconnects, the client will notice this and reconnect?
source share