主题:[原创]Delphi7中用TIdTCPServer和GPRS通信的问题,急!!!!!!!!
大家好。我这个最简单的程序为什么会在客户断开时不触发,并发现是在因为出现 Connection Closed Gracefully异常,所以不能触发OnDisconnect事件。
只有在OnExecution中加try...except...end,并在有问题时exit才会触发OnDisconnect。
我没有发现程序的错,因为太简单了。
请大家帮忙!!!!!
-------------------------------------------------------------
unit Unit2;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, IdAntiFreezeBase, IdAntiFreeze, IdThreadMgr, IdThreadMgrDefault,
IdBaseComponent, IdComponent, IdTCPServer, IdSocketHandle;
type
Tfrm_main = class(TForm)
tcp_server: TIdTCPServer;
IdThreadMgrDefault1: TIdThreadMgrDefault;
IdAntiFreeze1: TIdAntiFreeze;
procedure tcp_serverConnect(AThread: TIdPeerThread);
procedure tcp_serverException(AThread: TIdPeerThread;
AException: Exception);
procedure tcp_serverExecute(AThread: TIdPeerThread);
procedure FormShow(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure tcp_serverDisconnect(AThread: TIdPeerThread);
private
{ Private declarations }
procedure proc_set_server();
public
{ Public declarations }
end;
var
frm_main: Tfrm_main;
implementation
{$R *.dfm}
procedure Tfrm_main.tcp_serverConnect(AThread: TIdPeerThread);
begin
AThread.FreeOnTerminate:=true;
end;
procedure Tfrm_main.tcp_serverException(AThread: TIdPeerThread;
AException: Exception);
begin
AThread.Data:=nil;
if AThread.Connection.Connected then
AThread.Connection.DisconnectSocket;
try
AThread.Free;
except
end;
end;
procedure Tfrm_main.tcp_serverExecute(AThread: TIdPeerThread);
begin
with AThread.Connection do
begin
ReadfromStack();
end;
end;
procedure Tfrm_main.proc_set_server();
begin
if tcp_server.Active then tcp_server.Active := false;
tcp_server.DefaultPort := 10012;
tcp_server.Active := true;
end;
procedure Tfrm_main.FormShow(Sender: TObject);
begin
proc_set_server();
end;
procedure Tfrm_main.FormClose(Sender: TObject; var Action: TCloseAction);
begin
tcp_server.Active:=false;
end;
procedure Tfrm_main.tcp_serverDisconnect(AThread: TIdPeerThread);
begin
;
end;
end.
只有在OnExecution中加try...except...end,并在有问题时exit才会触发OnDisconnect。
我没有发现程序的错,因为太简单了。
请大家帮忙!!!!!
-------------------------------------------------------------
unit Unit2;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, IdAntiFreezeBase, IdAntiFreeze, IdThreadMgr, IdThreadMgrDefault,
IdBaseComponent, IdComponent, IdTCPServer, IdSocketHandle;
type
Tfrm_main = class(TForm)
tcp_server: TIdTCPServer;
IdThreadMgrDefault1: TIdThreadMgrDefault;
IdAntiFreeze1: TIdAntiFreeze;
procedure tcp_serverConnect(AThread: TIdPeerThread);
procedure tcp_serverException(AThread: TIdPeerThread;
AException: Exception);
procedure tcp_serverExecute(AThread: TIdPeerThread);
procedure FormShow(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure tcp_serverDisconnect(AThread: TIdPeerThread);
private
{ Private declarations }
procedure proc_set_server();
public
{ Public declarations }
end;
var
frm_main: Tfrm_main;
implementation
{$R *.dfm}
procedure Tfrm_main.tcp_serverConnect(AThread: TIdPeerThread);
begin
AThread.FreeOnTerminate:=true;
end;
procedure Tfrm_main.tcp_serverException(AThread: TIdPeerThread;
AException: Exception);
begin
AThread.Data:=nil;
if AThread.Connection.Connected then
AThread.Connection.DisconnectSocket;
try
AThread.Free;
except
end;
end;
procedure Tfrm_main.tcp_serverExecute(AThread: TIdPeerThread);
begin
with AThread.Connection do
begin
ReadfromStack();
end;
end;
procedure Tfrm_main.proc_set_server();
begin
if tcp_server.Active then tcp_server.Active := false;
tcp_server.DefaultPort := 10012;
tcp_server.Active := true;
end;
procedure Tfrm_main.FormShow(Sender: TObject);
begin
proc_set_server();
end;
procedure Tfrm_main.FormClose(Sender: TObject; var Action: TCloseAction);
begin
tcp_server.Active:=false;
end;
procedure Tfrm_main.tcp_serverDisconnect(AThread: TIdPeerThread);
begin
;
end;
end.