带有计时器的TCP / IP类,计时器刻度未激活...

莫维安

成员
已加入
2013年7月29日
留言内容
8
地点
美国弗吉尼亚州
编程经验
3-5
嘿,
我正在学习tcp / ip&套接字并编写了一个类来处理我程序的TCP / IP交互。我已经设置了一个计时器,该计时器应该向服务器发送消息并接受响应,以使连接保持活动状态,并检查连接是否断开。

但是,在对线程进行了一些调整以确保主系统的平稳运行之后,计时器滴答事件似乎没有触发...在调试模式下,我还有一个额外的选择。"Console"窗口(仅是我使用丰富的文本框设置的表单,可以将调试信息发送到该文本框)。这"ping"信息不存在,插入代码时中断代码本身就不会触发。

希望有人能指出我正确的方向。我已经尝试将其切换到具有相同结果的后台工作人员,我想将连接代码保留在单独的线程上,以便在例如尝试连接超时等事件时,GUI不会锁定。

提前致谢!

这是我用来实例化serverconnection对象的代码。
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();
        }


这是ServerConnection类的代码。
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;
        }


    }
}
 
最佳 底部