日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

关于Socket通信服务的心跳包(转)

發布時間:2025/3/15 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 关于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 +++++++++++++++++++ 服務器的感覺系統

?

轉載于:https://www.cnblogs.com/cyrix/articles/1706894.html

總結

以上是生活随笔為你收集整理的关于Socket通信服务的心跳包(转)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。