Socket心跳包 谁知道心跳包怎么现实啊?最好有详细代码急!!! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 晕,这个就是普通的SOCKET发送数据的方式,唯一不同的是这个包是用来检测连接状态的,比如每隔半小时发送一个特殊指令"xxxx",没有特定的心跳包格式 比如A端上的socket,隔半分钟send("xxxx");如果发送成功,则A端上的连接可默认为正常B端SOCKET上收到"xxxx"后,B端上的连接也为正常 客户端://启动记时器 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!"; } }服务端://启动记时器 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; //掉线 } } } } } //定时检查在线人目前状态 loginServer lser = new loginServer(); Login l = new Login(); l.Id = lser.MyLogin.Id; l.ClientTypeVersion = version; l.RequestType = 3; 编译不了,这几行什么意思啊?注释一下。。 就是一个socket连接然后一个定时器定时发送和处理接收到的心跳包就可以了你其他东西怎么发送的,心跳包也怎么发只不过改改内容而已 http://www.codeproject.com/KB/dotnet/heartbeat.aspx 楼主你这就不是来问原理的,根本就是来依赖别人给你写代码的,连最基本的SOCKET通讯都不理解,还有什么用,别想一步登天,去学基础去!!! 心跳包其实就是客户,服务端,各有一个线程工作,客户端周期性的发送一个自定义的数据包给服务端,服务端收到了以后,就认为客户端还跟服务端保持着连接看看Socket.SendTimeout和Socket.ReceiveTime c#调用带存储过程(带有in(@para)) 如何使用时间控件(Calendar1) 怎么样使Winfom上的控件,随窗体的大小来按比例改变大小? 怎么把当前操作信息记录在一个文档内?急待~~~ 询问网络高手,抓下来的网页HTML代码同IE打开所获得的HTML代码不一样 请问怎么用c#代码进行ie的安全设置?谢谢 求助!!! 水晶报表的问题!求前辈们帮助!!!!! 急~~~简单送分题,关于验证控件失效的问题 怎样将一个combobox或textbox里的输入文本,从string型转为int型 各位帮忙,谢谢了 按钮问题 求解
B端SOCKET上收到"xxxx"后,B端上的连接也为正常
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!";
}
}服务端:
//启动记时器
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; //掉线
}
}
}
}
} //定时检查在线人目前状态
Login l = new Login();
l.Id = lser.MyLogin.Id;
l.ClientTypeVersion = version;
l.RequestType = 3;
编译不了,这几行什么意思啊?注释一下。。
然后一个定时器
定时发送和处理接收到的心跳包就可以了
你其他东西怎么发送的,心跳包也怎么发
只不过改改内容而已
楼主你这就不是来问原理的,根本就是来依赖别人给你写代码的,连最基本的SOCKET通讯都不理解,还有什么用,别想一步登天,去学基础去!!!
看看Socket.SendTimeout和Socket.ReceiveTime