TCP心跳包文档
客户端:
C# code
//启动记时器
public void BeginTheTimer()
{
//th_UserLogin();
//这里只是要一个object类型数据,用它做为下面Timer的参数之一,没有其它意思
object myobject = (object)7;
//暂时设定为1秒钟启动一次!
System.Threading.Timer t = new System.Threading.Timer
(new System.Threading.TimerCallback(testTheNet), myobject, 1000, 1000);
}
//启动监视"已登录用户通信情况"的线程
public void testTheNet(object myobject)
{
//UserPassport up=new UserPassport();
Thread sendMyPulseThPro = new Thread(new ThreadStart(delegateSendMyPulse));
sendMyPulseThPro.Start();
}
/// <summary>
/// 每隔1秒就是要来做这些事情的
/// </summary>
public void delegateSendMyPulse()
{
loginServer lser = new loginServer();
Login l = new Login();
l.Id = lser.MyLogin.Id;
l.ClientTypeVersion = version;
l.RequestType = 3;
//3是确认联接正常的一个信号(让服务知道它与服务器的联接是正常的)
loginServer lserver = new loginServer();
//启动一个新线程去发送数据
Thread thSendDat2 = new Thread
(new ParameterizedThreadStart(lserver.delgSendDataMethod));
thSendDat2.Start(l);
thSendDat2.IsBackground = true;
//标记我已经发送出去一次数据了
longinserver.MyLostTime += 1;
//如果外发了3次请求暗号后仍不见服务器的回应,则认为客户端已经与服务器断开联系了
if(longinserver.MyLostTime>=3)
{
//停止Timer
//告诉用户:“你已经与服务器失去联系了…………”
longinserver.Controls["txtShowMsg"].Text = "You have lost the connect!";
}
}
服务端: | |
myRegistry[index].no_check_in_count += 1;
if (myRegistry[index].no_check_in_count >= 3)
{
//this.lblShowMsg.Text = "the student"
//this.lblShowMsg.Text += registry[index].studentID.ToString()
//this.lblShowMsg.Text += "is diaoxianle!";
this.txtLogin.Text += "88";
//标记该人已经与服务器失去连接了,因为他有连续3次的未到记录存在
registry[index].studentID = "";
registry[index].StudentName = "";
registry[index].StudentIP = "";
registry[index].status = 2; //掉线
}
}
}
}
} //定时检查在线人目前状态