惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园 - 三生石上(FineUI控件)
SecWiki News
SecWiki News
P
Proofpoint News Feed
P
Palo Alto Networks Blog
L
LINUX DO - 热门话题
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Spread Privacy
Spread Privacy
A
Arctic Wolf
Recorded Future
Recorded Future
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
博客园_首页
J
Java Code Geeks
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
D
DataBreaches.Net
量子位
K
Kaspersky official blog
I
InfoQ
W
WeLiveSecurity
Engineering at Meta
Engineering at Meta
Scott Helme
Scott Helme
U
Unit 42
Cyberwarzone
Cyberwarzone
L
Lohrmann on Cybersecurity
Cisco Talos Blog
Cisco Talos Blog
T
Troy Hunt's Blog
Stack Overflow Blog
Stack Overflow Blog
WordPress大学
WordPress大学
Jina AI
Jina AI
Recent Announcements
Recent Announcements
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
C
Cybersecurity and Infrastructure Security Agency CISA
H
Heimdal Security Blog
aimingoo的专栏
aimingoo的专栏
H
Help Net Security
Security Latest
Security Latest
B
Blog RSS Feed
Project Zero
Project Zero
C
Cisco Blogs
S
Securelist
V
Visual Studio Blog
Hugging Face - Blog
Hugging Face - Blog
G
GRAHAM CLULEY
N
Netflix TechBlog - Medium
T
Tor Project blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
GbyAI
GbyAI
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
F
Fortinet All Blogs

博客园 - 张皓

Stereoscopic Player 1.7.4 (SSP) 加载字幕 layout_weight体验(实现按比例显示) Linux5配置jboss环境 pager-taglib 分页扩展实例 DisplayTag1.2 扩展(自定义分页、排序、导出、页面导航) EJB3在JBoss5内集群探究 maven2 Jetty运行多模块的web application JBoss5开发web service常见问题 jboss启动常见的错误 ADO连接池 IDUdpServer研究心得 DLL内线程同步主线程研究(子线程代码放到主线程执行) DBGrid内使用CheckBox功能 子绑定控件获取父绑定项的值 - 张皓 - 博客园 . Net环境下消息队列(MSMQ)对象的应用[转] 消息队列(Message Queue)简介及其使用[转] 分布式事务TransactionScope小结[转] 分布式事务的点滴 调试SQL Server存储过程方法
IdTcpServer 用户掉线检测方法
张皓 · 2009-11-13 · via 博客园 - 张皓

正常情况下,当登陆用户异常掉线并不会通知服务器,这时服务器一直以为用户在线,解决这种问题有以下两种方法:

一、轮训检测连接情况,需要Timer轮训检测,如下代码:
procedure TMainForm.Timer1Timer(Sender: TObject);
begin
  CheckForDisconnect();
end;

procedure TMainForm.CheckForDisconnect();
var
  lst:TList;
  I:Integer;
  User:TUser;
begin
  lst:=FUserList.LockList;
  try
    for I:=0 to lst.Count-1 do
      // 检查连接,如失去连接则会在本线程的OnExecute事件内出现读取异常,然后出发OnDsiconnet事件
      User.Context.Connection.CheckForGracefulDisconnect;
  finally
    FUserList.UnlockList;
  end;
end;

其中FUserList是登陆用户实体列表,用户实体内含有IdTcpServer用户线程信息。

二、连接时声明检测连接,需要用WinSock2(可网上下载),代码如下:

uses WinSock2;

procedure TForm1.IdTCPServer1Connect(AThread: TIdPeerThread);
type
  TCP_KeepAlive = record
    OnOff: Cardinal;
    KeepAliveTime: Cardinal;
    KeepAliveInterval: Cardinal
  end;
var
  Val: TCP_KeepAlive;
  Ret: DWord;
begin
  Val.OnOff:=1;
  Val.KeepAliveTime:=xxx;
  Val.KeepAliveInterval:=xxx;
  WSAIoctl(AThread.Connection.Socket.Binding.Handle, IOC_IN or IOC_VENDOR or 4,
    @Val, SizeOf(Val), nil, 0, @Ret, nil, nil)
end; 

同样,当检测到用户连接失败时会在OnExecute事件内抛出读异常,然后激发OnDisconnect事件。