在我们的考试系统中用到客户端和服务器之间的通信,服务器要时刻知道客户端的网络连接状态,当时我遇到这个问题的时候就没有在网上找到传说中的“心跳包”。只是凭自己心思随想写出了一个解决时下问题的算法。下面是客户端心跳包核心代码:
#region ++++++++++++++++++++客户端的感觉系统
//启动记时器
public void BeginTheTimer()
{
//th_UserLogin();
object myobject = (object)7; //这里只是要一个object类型数据,用它做为下面Timer的参数之一,其它没有什么意思
//暂时设定为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; //标记我已经发送出去一次数据了
if(longinserver.MyLostTime>=3) //如果外发了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" + registry[index].studentID.ToString() + "is diaoxianle!";
this.txtLogin.Text += "88";
//标记该人已经与服务器失去连接了,因为他有连续3次的未到记录存在
registry[index].studentID = "";
registry[index].StudentName = "";
registry[index].StudentIP = "";
registry[index].status = 2; //掉线
}
}
}
}
}//定时检查在线人目前状态
#endregion +++++++++++++++++++服务器的感觉系统
分享到:
相关推荐
势创-中为UDP通信协议
中为串口服务器测试软件
autobahn-python, 在 python 中为 Twisted 和 asyncio,web socket和, Autobahn|Pythonweb socket & WAMP用于 Twisted 和asyncio上的python 。 快速链接: 源代码 - 文档- web socket示例- web WebSocke
代码是用来在web项目中,为某个java类生成对应的二维码图片,移动端扫描后能够预览该类的相关信息.
附件中为SOCKET通讯的简单案例,通俗易懂,把电脑的IP设置为192.168.1.239 ,可以在单机上进行测试。
VB环境下的UDP方式socket通讯,注意调试代码前需要先添加winsocket控件,代码中为一个为发送端,一个为客户端
网络游戏-在光通信网络中为给定连接确定频谱路由的方法.zip
电信设备-数字通信系统中为透明数据业务选择链路协议的方法.zip
这个工具原本是项目中为测试TOE引擎的效率而设计的, 可以作为socket编程的一个例子来学习。 [2. 文件介绍] 程序中使用socket实现了文件的传输。 在VC6.0中编译通过,目录中olfile.cpp/olfile.h是原文件,可以...
详细地VS2008中为MFC项目插入右键菜单,是个很不错的例子,供参考。
电信设备-无线通信中为二元判定提供偏置准则以加强防护的用户设备.zip
中为现场服务_2023.3.7.pdf
网络游戏-在无线通信网络中为时隙消息提供时隙预留的方法和设备.zip
在WindowsServer2003中为SNMP服务配置网络安全性.pdf
“已知“端口0~1023由IANA控制,是在UNIX中为固定服务保留的。 已注册的端口1024~49151是IANA列出来的,供普通用户的普通用户进程或程序使用。 49152~65535是动态和(或)私用端口。 普通用户应用应该选择...
嗨,我正在Windows XP上使用VC ++和Visual Studio 2010开发ActiveX控件。我想为我的项目提供多种发布类型。 版本类型在功能上会有所不同。 版本A可能仅具有功能A1 ...而版本B可能仅具有功能B1 .... Bn。 还有...
NULL 博文链接:https://liuzidong.iteye.com/blog/898673
在OCaml中为MirageOS实现矩阵服务器_OCaml_下载.zip
网络游戏-WiMAX网络中为定位业务提供服务质量保证的方法.zip
网络游戏-在峰窝网络中为广播多播服务协作地中继数据的方法.zip