关于Socket通信服务的心跳包(转)
在一些系统中,经常用到客户端和服务器之间的通信,服务器要时刻知道客户端的网络连接状态,这大概就是所谓的“心跳包”。
下面是客户端心跳包核心代码:
# region ++++++++++++++++++++ 客户端的感觉系统
//启动记时器
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!";
}
}
# endregion +++++++++++++++++++++ 客户端的感觉系统
//启动记时器
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!";
}
}
# endregion +++++++++++++++++++++ 客户端的感觉系统
下面是服务器端核心代码如下:
# region +++++++++++++++++++++++ 服务器的感觉系统
//启动记时器
public void LoadTheTimer()
{
object o=(object)loginedCount++;
UserPassport up = new UserPassport();
//暂时设定为1秒钟启动一次!
System.Threading.Timer t = new System.Threading.Timer
(new System.Threading.TimerCallback(watchTheLoginUser), o, 1000, 1000);
}
//启动监视"已登录用户通信情况"的线程
public void watchTheLoginUser(object o)
{
//UserPassport up=new UserPassport();
Thread checktheloginuser = new Thread(new ThreadStart(iAmAWatcher));
checktheloginuser.Start();
}
//真正做事的工人:这个工人的使命是每隔1秒钟后就查看一下登记薄
//registry里面有谁没有定时来向服务器报到了,如果出现谁三次检查都没有签到则除之名
public void iAmAWatcher()
{
this.txtLogin.Text += "@+";
int index = 0;
for (index = 0; index < loginedCount; index++)
{
if (myRegistry[index].alive==false&®istry[index].studentID!="")
{
lock(this)
{
//坏(未到)记录增加一次
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; //掉线
}
}
}
}
} //定时检查在线人目前状态
# endregion +++++++++++++++++++ 服务器的感觉系统
//启动记时器
public void LoadTheTimer()
{
object o=(object)loginedCount++;
UserPassport up = new UserPassport();
//暂时设定为1秒钟启动一次!
System.Threading.Timer t = new System.Threading.Timer
(new System.Threading.TimerCallback(watchTheLoginUser), o, 1000, 1000);
}
//启动监视"已登录用户通信情况"的线程
public void watchTheLoginUser(object o)
{
//UserPassport up=new UserPassport();
Thread checktheloginuser = new Thread(new ThreadStart(iAmAWatcher));
checktheloginuser.Start();
}
//真正做事的工人:这个工人的使命是每隔1秒钟后就查看一下登记薄
//registry里面有谁没有定时来向服务器报到了,如果出现谁三次检查都没有签到则除之名
public void iAmAWatcher()
{
this.txtLogin.Text += "@+";
int index = 0;
for (index = 0; index < loginedCount; index++)
{
if (myRegistry[index].alive==false&®istry[index].studentID!="")
{
lock(this)
{
//坏(未到)记录增加一次
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; //掉线
}
}
}
}
} //定时检查在线人目前状态
# endregion +++++++++++++++++++ 服务器的感觉系统
来源:http://www.cnblogs.com/hexiaosheng/archive/2008/04/14/xintiaobao.html
作者: XuGang 网名:钢钢 |
出处: http://xugang.cnblogs.com |
声明: 本文版权归作者和博客园共有。转载时必须保留此段声明,且在文章页面明显位置给出原文连接地址! |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架