TCP/IP class with timer, timer tick not activating...

Movian

Member
Joined
Jul 29, 2013
Messages
8
Location
VA, USA
Programming Experience
3-5
Hey,
I am learning tcp/ip & Sockets and have written a class to handle the TCP/IP interactions for my program. I have setup a timer that should send a message to the server and accept a response so that the connection is kept alive and also to check that the connection has not dropped.

However after some tweaking with threads to ensure smooth operation of the main system the timer tick event does not appear to be firing... When in debug mode i have an additional "Console" window (just a form i setup with a rich textbox that i send debugging info to). the "ping" information doesn't go there and when insert a code break the code itself never fires..

Was hoping someone could point me in the right direction. I already tried switching this to a background worker with the same result, i want to keep the connection code on a separate thread so that the GUI doesn't lock up in the event of for example an attempt to connect that times out etc.

Thanks in advance!

Here is the code i use to instantiate the serverconnection object.
C#:
        #if DEBUG            
         public RealTimeLog Log = new RealTimeLog();
        #endif


        Private void Form1_Load(object sender, EventArgs e)
        {
            #if DEBUG           
               Log.Visible = true;
            //Log.showOnMonitor(2);
            #endif
            Options.LoadValues();
            Thread ServerThread = new Thread(ServerConnect);
            ServerThread.Start();
        }


        private void ServerConnect()
        {
            Server = new ServerConnection(Options.ServerIp, Convert.ToInt16(Options.ServerPort), this);
            Server.OpenConnection();
        }


Here is the Code for the ServerConnection class.
C#:
using System;using System.IO;
using System.Threading;
using System.Net;
using System.Net.Sockets;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.Calendar;


namespace SonoSoft_Schedule
{
    public class ServerConnection
    {
        private string _ServerIP = "";
        private int _ServerPort = 8000;
        private Form1 form1;
        private System.Windows.Forms.Timer keepAliveTimer = new System.Windows.Forms.Timer();
        private TcpClient tcpclnt = new TcpClient();


        public string ServerIP
        {
            get { return _ServerIP; }
            set { _ServerIP = value; }
        }


        public int ServerPort
        {
            get { return _ServerPort; }
            set { _ServerPort = value; }
        }




        public ServerConnection(string ServIP, int servPort, Form1 form1)
        {
            this.form1 = form1;
            _ServerIP = ServIP;
            _ServerPort = servPort;
            keepAliveTimer.Enabled = true;
            keepAliveTimer.Interval = 5000;
            keepAliveTimer.Tick += new EventHandler(SendKeepAlive);
        }


        private void SendKeepAlive(object sender, EventArgs e)
        {
            form1.Log.WriteToLog("Ping...");


            //Sending Ping
            Stream stm = tcpclnt.GetStream();
            ASCIIEncoding asen = new ASCIIEncoding();
            byte[] ba = asen.GetBytes("P");


            try
            {
                stm.Write(ba, 0, ba.Length);
            }
            catch (Exception ex)
            {
                form1.Log.WriteToLog(ex.Message);
            }


            byte[] bb = new byte[100];
            try
            {
                int k = stm.Read(bb, 0, 100);
            }
            catch (Exception ex)
            {
                form1.Log.WriteToLog(ex.Message);
            }
            string buffer = Encoding.ASCII.GetString(bb, 0, 100);


            form1.Log.WriteToLog(buffer);
        }


        public bool OpenConnection()
        {
            try
            {
                tcpclnt.Connect(Options.ServerIp, Convert.ToInt16(Options.ServerPort));
                form1.Log.WriteToLog("Attempting to connect to server...");
                form1.Log.WriteToLog("Status: " + tcpclnt.Connected.ToString());


                //Start Keep Alive Timer 
                keepAliveTimer.Start();


                return tcpclnt.Connected;
            }
            catch (Exception ex)
            {
                form1.Log.WriteToLog(ex.Message);
                return false;
            }
        }


        public bool CloseConnection()
        {
            try
            {
                form1.Log.WriteToLog("Closing Connection");
                Stream stm = tcpclnt.GetStream();


                ASCIIEncoding asen = new ASCIIEncoding();
                byte[] ba = asen.GetBytes("C");


                stm.Write(ba, 0, ba.Length);


                byte[] bb = new byte[100];
                int k = stm.Read(bb, 0, 100);
                tcpclnt.Close();
                form1.Log.WriteToLog("Attempting to close connection to server:");
                form1.Log.WriteToLog("Status: " + tcpclnt.Connected.ToString());
                keepAliveTimer.Stop();
                return !tcpclnt.Connected;
            }
            catch (Exception ex)
            {
                form1.Log.WriteToLog(ex.Message);
                return false;
            }
        }


        public bool SendMessage(string message)
        {
            try
            {
                //document process
                form1.Log.WriteToLog("Starting TCP Send");
                form1.Log.WriteToLog("Server IP:" + Options.ServerIp);
                form1.Log.WriteToLog("Server Port:" + Options.ServerPort);


                // Use the ipaddress as in the server program
                form1.Log.WriteToLog(message);
                Stream stm = tcpclnt.GetStream();


                ASCIIEncoding asen = new ASCIIEncoding();
                byte[] ba = asen.GetBytes(message);


                stm.Write(ba, 0, ba.Length);


                byte[] bb = new byte[100];
                int k = stm.Read(bb, 0, 100);


                string buffer = Encoding.ASCII.GetString(bb, 0, 100);


                form1.Log.WriteToLog(buffer);


                //Message sent so reset keep alive timer.
                keepAliveTimer.Stop();
                keepAliveTimer.Start();
            }
            catch (Exception ex)
            {
                form1.Log.WriteToLog(ex.Message);
                MessageBox.Show("Ops! Error! " + ex.StackTrace);
            }
            return true;
        }


        public bool SendCalItem(CalendarItem SendItem)
        {
            //will imnplement serialization to send CalendarItem over TCP Connection to server.
            return true;
        }


    }
}
 
Thanks, i had already made some changes for that but I guess I am still missing some sections making those calls... I will review and come back if i am still having issues.

Thanks for the input
 

Latest posts

Back
Top Bottom