IdTcpServer 用户掉线检测方法
2009-11-13 13:01
190 查看
正常情况下,当登陆用户异常掉线并不会通知服务器,这时服务器一直以为用户在线,解决这种问题有以下两种方法:
一、轮训检测连接情况,需要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事件。
一、轮训检测连接情况,需要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事件。
相关文章推荐
- IdTcpServer 部分总结 (用户掉线的检测方法,控制用户量等)
- IdTcpServer 部分总结 (用户掉线的检测方法,控制用户量等)
- Delphi中IdTcpServer 用户掉线检测方法
- IdTcpServer 部分总结 (用户掉线的检测方法,控制用户量等)
- 当恢复sqlserver bak文件时,原始的用户无法删除的解决方法
- Microsoft Dynamics CRM 2013 安装 报表服务出现“ SQL Server Reporting Services 帐户是本地用户且不受支持 ”错误的解决方法
- ubuntu_ftp_server配置方法【附root用户无法登陆ftp解决方案】
- sql server 2012错误:"用户 sa 登录失败 microsoft sql server,错误:18456"解决方法
- Python编写检测数据库SA用户的方法
- Linux用户进程内存泄露一种检测方法
- ubuntu-server 16.04 启用root用户的方法
- Lync Server 2010调整用户设置时,报“访问特权不够”错误解决方法
- javascript浏览器用户代理检测脚本实现方法
- 用户异常掉线检测
- 使用trim方法检测用户输入
- 可使用两种方法之一生成窗体身份验证 Cookie,并将用户重定向到 cmdLogin_ServerClick 事件中的相应页。
- SIO_KEEPALIVE_VALS 用户异常掉线检测
- SIO_KEEPALIVE_VALS 用户异常掉线检测
- socket用户异常掉线检测
- HOSt ip is not allowed to connect to this MySql server, MYSQL添加远程用户或允许远程访问三种方法