关于Socket通信服务的心跳包(转)
生活随笔
收集整理的這篇文章主要介紹了
关于Socket通信服务的心跳包(转)
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
代碼 在一些系統(tǒng)中,經(jīng)常用到客戶(hù)端和服務(wù)器之間的通信,服務(wù)器要時(shí)刻知道客戶(hù)端的網(wǎng)絡(luò)連接狀態(tài),這大概就是所謂的“心跳包”。
下面是客戶(hù)端心跳包核心代碼:
# region ++++++++++++++++++++ 客戶(hù)端的感覺(jué)系統(tǒng)
//啟動(dòng)記時(shí)器
public void BeginTheTimer()
{
//th_UserLogin();
//這里只是要一個(gè)object類(lèi)型數(shù)據(jù),用它做為下面Timer的參數(shù)之一,沒(méi)有其它意思
object myobject = (object)7;
//暫時(shí)設(shè)定為1秒鐘啟動(dòng)一次!
System.Threading.Timer t = new System.Threading.Timer
(new System.Threading.TimerCallback(testTheNet), myobject, 1000, 1000);
}
//啟動(dòng)監(jiān)視"已登錄用戶(hù)通信情況"的線程
public void testTheNet(object myobject)
{
//UserPassport up=new UserPassport();
Thread sendMyPulseThPro = new Thread(new ThreadStart(delegateSendMyPulse));
sendMyPulseThPro.Start();
}
/// <summary>
/// 每隔1秒就是要來(lái)做這些事情的
/// </summary>
public void delegateSendMyPulse()
{
loginServer lser = new loginServer();
Login l = new Login();
l.Id = lser.MyLogin.Id;
l.ClientTypeVersion = version;
l.RequestType = 3;
//3是確認(rèn)聯(lián)接正常的一個(gè)信號(hào)(讓服務(wù)知道它與服務(wù)器的聯(lián)接是正常的)
loginServer lserver = new loginServer();
//啟動(dòng)一個(gè)新線程去發(fā)送數(shù)據(jù)
Thread thSendDat2 = new Thread
(new ParameterizedThreadStart(lserver.delgSendDataMethod));
thSendDat2.Start(l);
thSendDat2.IsBackground = true;
//標(biāo)記我已經(jīng)發(fā)送出去一次數(shù)據(jù)了
longinserver.MyLostTime += 1;
//如果外發(fā)了3次請(qǐng)求暗號(hào)后仍不見(jiàn)服務(wù)器的回應(yīng),則認(rèn)為客戶(hù)端已經(jīng)與服務(wù)器斷開(kāi)聯(lián)系了
if(longinserver.MyLostTime>=3)
{
//停止Timer
//告訴用戶(hù):“你已經(jīng)與服務(wù)器失去聯(lián)系了…………”
longinserver.Controls["txtShowMsg"].Text = "You have lost the connect!";
}
}
# endregion +++++++++++++++++++++ 客戶(hù)端的感覺(jué)系統(tǒng)
下面是服務(wù)器端核心代碼如下:
# region +++++++++++++++++++++++ 服務(wù)器的感覺(jué)系統(tǒng)
//啟動(dòng)記時(shí)器
public void LoadTheTimer()
{
object o=(object)loginedCount++;
UserPassport up = new UserPassport();
//暫時(shí)設(shè)定為1秒鐘啟動(dòng)一次!
System.Threading.Timer t = new System.Threading.Timer
(new System.Threading.TimerCallback(watchTheLoginUser), o, 1000, 1000);
}
//啟動(dòng)監(jiān)視"已登錄用戶(hù)通信情況"的線程
public void watchTheLoginUser(object o)
{
//UserPassport up=new UserPassport();
Thread checktheloginuser = new Thread(new ThreadStart(iAmAWatcher));
checktheloginuser.Start();
}
//真正做事的工人:這個(gè)工人的使命是每隔1秒鐘后就查看一下登記薄
//registry里面有誰(shuí)沒(méi)有定時(shí)來(lái)向服務(wù)器報(bào)到了,如果出現(xiàn)誰(shuí)三次檢查都沒(méi)有簽到則除之名
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";
//標(biāo)記該人已經(jīng)與服務(wù)器失去連接了,因?yàn)樗羞B續(xù)3次的未到記錄存在
registry[index].studentID = "";
registry[index].StudentName = "";
registry[index].StudentIP = "";
registry[index].status = 2; //掉線
}
}
}
}
} //定時(shí)檢查在線人目前狀態(tài)
# endregion +++++++++++++++++++ 服務(wù)器的感覺(jué)系統(tǒng)
下面是客戶(hù)端心跳包核心代碼:
# region ++++++++++++++++++++ 客戶(hù)端的感覺(jué)系統(tǒng)
//啟動(dòng)記時(shí)器
public void BeginTheTimer()
{
//th_UserLogin();
//這里只是要一個(gè)object類(lèi)型數(shù)據(jù),用它做為下面Timer的參數(shù)之一,沒(méi)有其它意思
object myobject = (object)7;
//暫時(shí)設(shè)定為1秒鐘啟動(dòng)一次!
System.Threading.Timer t = new System.Threading.Timer
(new System.Threading.TimerCallback(testTheNet), myobject, 1000, 1000);
}
//啟動(dòng)監(jiān)視"已登錄用戶(hù)通信情況"的線程
public void testTheNet(object myobject)
{
//UserPassport up=new UserPassport();
Thread sendMyPulseThPro = new Thread(new ThreadStart(delegateSendMyPulse));
sendMyPulseThPro.Start();
}
/// <summary>
/// 每隔1秒就是要來(lái)做這些事情的
/// </summary>
public void delegateSendMyPulse()
{
loginServer lser = new loginServer();
Login l = new Login();
l.Id = lser.MyLogin.Id;
l.ClientTypeVersion = version;
l.RequestType = 3;
//3是確認(rèn)聯(lián)接正常的一個(gè)信號(hào)(讓服務(wù)知道它與服務(wù)器的聯(lián)接是正常的)
loginServer lserver = new loginServer();
//啟動(dòng)一個(gè)新線程去發(fā)送數(shù)據(jù)
Thread thSendDat2 = new Thread
(new ParameterizedThreadStart(lserver.delgSendDataMethod));
thSendDat2.Start(l);
thSendDat2.IsBackground = true;
//標(biāo)記我已經(jīng)發(fā)送出去一次數(shù)據(jù)了
longinserver.MyLostTime += 1;
//如果外發(fā)了3次請(qǐng)求暗號(hào)后仍不見(jiàn)服務(wù)器的回應(yīng),則認(rèn)為客戶(hù)端已經(jīng)與服務(wù)器斷開(kāi)聯(lián)系了
if(longinserver.MyLostTime>=3)
{
//停止Timer
//告訴用戶(hù):“你已經(jīng)與服務(wù)器失去聯(lián)系了…………”
longinserver.Controls["txtShowMsg"].Text = "You have lost the connect!";
}
}
# endregion +++++++++++++++++++++ 客戶(hù)端的感覺(jué)系統(tǒng)
下面是服務(wù)器端核心代碼如下:
# region +++++++++++++++++++++++ 服務(wù)器的感覺(jué)系統(tǒng)
//啟動(dòng)記時(shí)器
public void LoadTheTimer()
{
object o=(object)loginedCount++;
UserPassport up = new UserPassport();
//暫時(shí)設(shè)定為1秒鐘啟動(dòng)一次!
System.Threading.Timer t = new System.Threading.Timer
(new System.Threading.TimerCallback(watchTheLoginUser), o, 1000, 1000);
}
//啟動(dòng)監(jiān)視"已登錄用戶(hù)通信情況"的線程
public void watchTheLoginUser(object o)
{
//UserPassport up=new UserPassport();
Thread checktheloginuser = new Thread(new ThreadStart(iAmAWatcher));
checktheloginuser.Start();
}
//真正做事的工人:這個(gè)工人的使命是每隔1秒鐘后就查看一下登記薄
//registry里面有誰(shuí)沒(méi)有定時(shí)來(lái)向服務(wù)器報(bào)到了,如果出現(xiàn)誰(shuí)三次檢查都沒(méi)有簽到則除之名
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";
//標(biāo)記該人已經(jīng)與服務(wù)器失去連接了,因?yàn)樗羞B續(xù)3次的未到記錄存在
registry[index].studentID = "";
registry[index].StudentName = "";
registry[index].StudentIP = "";
registry[index].status = 2; //掉線
}
}
}
}
} //定時(shí)檢查在線人目前狀態(tài)
# endregion +++++++++++++++++++ 服務(wù)器的感覺(jué)系統(tǒng)
?
轉(zhuǎn)載于:https://www.cnblogs.com/cyrix/articles/1706894.html
總結(jié)
以上是生活随笔為你收集整理的关于Socket通信服务的心跳包(转)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: WCF服务端基于配置的实现——拦截
- 下一篇: sybase函数学习(八)