关于Socket通信服务的心跳包(转)
生活随笔
收集整理的這篇文章主要介紹了
关于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 +++++++++++++++++++++ 客戶端的感覺系統
下面是服務器端核心代碼如下:
# 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 +++++++++++++++++++ 服務器的感覺系統
下面是客戶端心跳包核心代碼:
# 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 +++++++++++++++++++++ 客戶端的感覺系統
下面是服務器端核心代碼如下:
# 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 +++++++++++++++++++ 服務器的感覺系統
?
轉載于:https://www.cnblogs.com/cyrix/articles/1706894.html
總結
以上是生活随笔為你收集整理的关于Socket通信服务的心跳包(转)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: WCF服务端基于配置的实现——拦截
- 下一篇: sybase函数学习(八)