From fcb189a4cdcb16101e3dfd9f93224a290a287459 Mon Sep 17 00:00:00 2001 From: szporowolik Date: Fri, 13 Sep 2019 15:56:56 +0200 Subject: [PATCH] Reconnecting issue solved --- .../Perun_v1/01_Classes/TCPController.cs | 19 ++++++++++++++++--- 02_Windows_App/Perun_v1/02_Forms/form_Main.cs | 18 ++++++++++++++++++ 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/02_Windows_App/Perun_v1/01_Classes/TCPController.cs b/02_Windows_App/Perun_v1/01_Classes/TCPController.cs index 6ca157b..96695c4 100644 --- a/02_Windows_App/Perun_v1/01_Classes/TCPController.cs +++ b/02_Windows_App/Perun_v1/01_Classes/TCPController.cs @@ -41,6 +41,9 @@ public class TCPController public void StartListen() { + NetworkStream ns=null; + TcpClient tcpClient=null; + while (!bDone) { Console.WriteLine("TCP Listen start"); @@ -59,8 +62,8 @@ public class TCPController // Start listening Console.WriteLine("TCP: Waiting for packet"); - TcpClient tcpClient = tcpServer.AcceptTcpClient(); //if a connection exists, the server will accept it - NetworkStream ns = tcpClient.GetStream(); //networkstream is used to send/receive messages + tcpClient = tcpServer.AcceptTcpClient(); //if a connection exists, the server will accept it + ns = tcpClient.GetStream(); //networkstream is used to send/receive messages while (tcpClient.Connected && !bDone) //while the client is connected, we look for incoming messages { @@ -78,7 +81,7 @@ public class TCPController numberOfBytesRead = ns.Read(ReadBuffer, 0, ReadBuffer.Length); CompleteMessage.AppendFormat("{0}", Encoding.ASCII.GetString(ReadBuffer, 0, numberOfBytesRead)); } - while (ns.DataAvailable); + while (ns.DataAvailable && !bDone); } strReceivedData = CompleteMessage.ToString(); Console.WriteLine("Sender: {0} Payload: {1}", null, strReceivedData); @@ -121,6 +124,16 @@ public class TCPController } } + + if (!(ns is null)) + { + ns.Flush(); + ns.Close(); + } + if (!(tcpClient is null)) + { + tcpClient.Close(); + } if (!(tcpServer is null)) { tcpServer.Stop(); diff --git a/02_Windows_App/Perun_v1/02_Forms/form_Main.cs b/02_Windows_App/Perun_v1/02_Forms/form_Main.cs index a9a3360..070af1d 100644 --- a/02_Windows_App/Perun_v1/02_Forms/form_Main.cs +++ b/02_Windows_App/Perun_v1/02_Forms/form_Main.cs @@ -112,6 +112,7 @@ namespace Perun_v1 private void con_Button_Listen_ON_Click(object sender, EventArgs e) { // Start listening + PerunHelper.LogHistoryAdd(ref Globals.arrLogHistory, "Opening connections"); tcpcServer.Create(Int32.Parse(con_txt_dcs_server_port.Text), ref Globals.arrLogHistory, ref arrSendBuffer); tcpcServer.thrTCPListener = new Thread(tcpcServer.StartListen); tcpcServer.thrTCPListener.Start(); @@ -135,6 +136,15 @@ namespace Perun_v1 private void con_Button_Listen_OFF_Click(object sender, EventArgs e) { // Stop listening + + // Display information + PerunHelper.LogHistoryAdd(ref Globals.arrLogHistory, "Closing connections"); + con_Button_Listen_OFF.Enabled = false; + timer1_Tick(null, null); + this.Refresh(); + Application.DoEvents(); + + try { tcpcServer.StopListen(); @@ -144,6 +154,10 @@ namespace Perun_v1 Console.WriteLine(ex.ToString()); } + while (tcpcServer.thrTCPListener.IsAlive) + { + Thread.Sleep(100); //ms + } form_Main_EnableControls(); // Enable controls con_img_db.Image = null; @@ -155,6 +169,10 @@ namespace Perun_v1 tim_1000ms.Enabled = false; tim_10000ms.Enabled = false; tim_200ms.Enabled = false; + + // Display information about closed connections + PerunHelper.LogHistoryAdd(ref Globals.arrLogHistory, "Connections closed"); + timer1_Tick(null, null); } private void con_lab_github_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)