L
liboy.com
Unregistered / Unconfirmed
GUEST, unregistred user!
首先定义了如下一个记录和指针
type
TSimpleClient = Record
id: longint; //系统编号
utype: string; //gprs, emp, unknow
Name: string; //手机号,登录操作员名称
IP: string; //IP
Port: integer; //端口
Status: string; //NULL 登录中 操作中
LastTime: integer; //登录时间
UpdateTime: Integer; //更新时间
HardWare: String; //硬件类型
DataBackTime: Integer; //监控时间, 超时则断开
end;
PClient = ^TSimpleClient;
//客户新建链接时记录客户端信息到记录中
procedure TfrmNet.TCPServerConnect(AThread: TIdPeerThread);
var Client: PClient;
begin
Client := new( PClient );
Client.id := GetTickCount + Random(1000);
Client.uType := 'GUEST';
Client.IP := AThread.Connection.Socket.Binding.PeerIP;
Client.Port := AThread.Connection.Socket.Binding.PeerPort;
Client.LastTime := GetTickCount;
Client.UpdateTime := Client.LastTime;
Client.Status := '登录中';
Client.Name := 'GUEST';
Client.HardWare := GPS_NAME_UNKNOW;
Client.DataBackTime := 3600; //监控周期
AThread.Data := Pointer( client ); <<指到 athread的指针中
end;
//客户端断开事件中释放
procedure TfrmNet.TCPServerDisconnect(AThread: TIdPeerThread);
var Client: PClient;
begin
Client := Pointer(AThread.Data);
AThread.Data := nil;
end;
//与客户通讯的处理过程
procedure TfrmNet.TCPServerExecute(AThread: TIdPeerThread);
var c: PClient;
begin
if (AThread.Connection.Connected) and (not Athread.Terminated) then
begin
sStr := AThread.Connection.CurrentReadBuffer;
end;
//其他不会造成任何死循环或者异常的处理代码
end;
//关掉当前用户以前打开的tcp/ip链接
//当用户突然断线时,所打开的tcp/ip链接有可能继续保持不断线
procedure TfrmNet.Clients_CloseGprsBeforeConnect( curId: Integer; sName: String );
var i: integer;
list: TList;
Client: PClient;
begin
List := tcpServer.Threads.LockList;
try
for i := 0 to List.Count -1 do begin
try
Client := Pointer( TIdPeerThread(List.Items).Data );
if Client = nil then continue;
if (Client.Name <> sName) or (Client.id = curId ) or (Client.utype <> 'GPRS') then Continue;
if TIdPeerThread(List.Items).Connection.Connected then
TIdPeerThread(List.Items).Connection.Disconnect;
except
TIdPeerThread(List.Items).Stop;
end;
end;
finally
tcpServer.Threads.UnlockList;
end;
end;
问题是:
大量的终端设备通过TCP/IP连接到服务器,由于设备硬件以及使用的是GPRS网络的原因.
设备经常会像断电那样,重新连接服务器,然而之前的连接没有断开(不知道是不是GPRS的原因)
虽然程序已经做了判断,断开这种异常的连接,
但是服务器程序还是经常会死掉,原因不明.请大家帮助分析分析.
type
TSimpleClient = Record
id: longint; //系统编号
utype: string; //gprs, emp, unknow
Name: string; //手机号,登录操作员名称
IP: string; //IP
Port: integer; //端口
Status: string; //NULL 登录中 操作中
LastTime: integer; //登录时间
UpdateTime: Integer; //更新时间
HardWare: String; //硬件类型
DataBackTime: Integer; //监控时间, 超时则断开
end;
PClient = ^TSimpleClient;
//客户新建链接时记录客户端信息到记录中
procedure TfrmNet.TCPServerConnect(AThread: TIdPeerThread);
var Client: PClient;
begin
Client := new( PClient );
Client.id := GetTickCount + Random(1000);
Client.uType := 'GUEST';
Client.IP := AThread.Connection.Socket.Binding.PeerIP;
Client.Port := AThread.Connection.Socket.Binding.PeerPort;
Client.LastTime := GetTickCount;
Client.UpdateTime := Client.LastTime;
Client.Status := '登录中';
Client.Name := 'GUEST';
Client.HardWare := GPS_NAME_UNKNOW;
Client.DataBackTime := 3600; //监控周期
AThread.Data := Pointer( client ); <<指到 athread的指针中
end;
//客户端断开事件中释放
procedure TfrmNet.TCPServerDisconnect(AThread: TIdPeerThread);
var Client: PClient;
begin
Client := Pointer(AThread.Data);
AThread.Data := nil;
end;
//与客户通讯的处理过程
procedure TfrmNet.TCPServerExecute(AThread: TIdPeerThread);
var c: PClient;
begin
if (AThread.Connection.Connected) and (not Athread.Terminated) then
begin
sStr := AThread.Connection.CurrentReadBuffer;
end;
//其他不会造成任何死循环或者异常的处理代码
end;
//关掉当前用户以前打开的tcp/ip链接
//当用户突然断线时,所打开的tcp/ip链接有可能继续保持不断线
procedure TfrmNet.Clients_CloseGprsBeforeConnect( curId: Integer; sName: String );
var i: integer;
list: TList;
Client: PClient;
begin
List := tcpServer.Threads.LockList;
try
for i := 0 to List.Count -1 do begin
try
Client := Pointer( TIdPeerThread(List.Items).Data );
if Client = nil then continue;
if (Client.Name <> sName) or (Client.id = curId ) or (Client.utype <> 'GPRS') then Continue;
if TIdPeerThread(List.Items).Connection.Connected then
TIdPeerThread(List.Items).Connection.Disconnect;
except
TIdPeerThread(List.Items).Stop;
end;
end;
finally
tcpServer.Threads.UnlockList;
end;
end;
问题是:
大量的终端设备通过TCP/IP连接到服务器,由于设备硬件以及使用的是GPRS网络的原因.
设备经常会像断电那样,重新连接服务器,然而之前的连接没有断开(不知道是不是GPRS的原因)
虽然程序已经做了判断,断开这种异常的连接,
但是服务器程序还是经常会死掉,原因不明.请大家帮助分析分析.