S
soulmate
Unregistered / Unconfirmed
GUEST, unregistred user!
我的客户端其实是一个GPRS无线模块,大家可以把它当做一个客户端,我也查了网上的很多资料,都没有给处在客户端异常断线的处理办法。
按道理说应该在DisConnect事件中例如在Memo控件中显示断线的客户端IP地址,但我发现只要在Disconnect事件中调用显示程序就抛出异常,另外也根本检测不到客户端的断线问题。
这个问题一直也没有解决,希望有作过的或给指点一下,我想Indy既然做出这个东西,总不至于连这些细节都不能解决吧,一定是我那里没有搞明白~
unit Main;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Buttons, IdBaseComponent, IdComponent, IdTCPServer,
IdTCPConnection, IdTCPClient, IdAntiFreezeBase, IdAntiFreeze;
type
TMainFrm = class(TForm)
EdtPort: TEdit;
Meo_Rec: TMemo;
BtnStart: TBitBtn;
BtnSend: TBitBtn;
BtnStop: TBitBtn;
IdTCPServer: TIdTCPServer;
EdtSend: TEdit;
ChkSndHex: TCheckBox;
BtnAbout: TBitBtn;
BtnClear: TBitBtn;
IdAntiFreeze: TIdAntiFreeze;
procedure BtnStartClick(Sender: TObject);
procedure BtnStopClick(Sender: TObject);
procedure IdTCPServerConnect(AThread: TIdPeerThread);
procedure IdTCPServerExecute(AThread: TIdPeerThread);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure BtnSendClick(Sender: TObject);
procedure BtnAboutClick(Sender: TObject);
procedure BtnClearClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
//function EncodeHEX(A : String) : String;
function EncodeHEX(AChar;len:Integer) : String;
end;
var
MainFrm: TMainFrm;
implementation
{$R *.dfm}
procedure TMainFrm.BtnStartClick(Sender: TObject);
begin
IdTCPServer.DefaultPort := StrToInt(EdtPort.Text);
IdTCPServer.Active := True;
BtnStart.Enabled := False;
BtnStop.Enabled := True;
Meo_Rec.Lines.Add('服务器已成功启动!');
end;
procedure TMainFrm.BtnStopClick(Sender: TObject);
begin
if (IdTCPServer.Active=true) then
begin
IdTCPServer.Active := False;
BtnStart.Enabled := True;
BtnStop.Enabled := False;
Meo_Rec.Lines.Add('服务器已成功停止!');
end;
end;
procedure TMainFrm.IdTCPServerConnect(AThread: TIdPeerThread);
begin
Meo_Rec.Lines.Add('来自主机 '
+ AThread.Connection.Socket.Binding.PeerIP
+ ' 的连接请求已被接纳!');
end;
procedure TMainFrm.IdTCPServerExecute(AThread: TIdPeerThread);
var
len:integer;
sCommand: array[0..4096] of char;
begin
if not AThread.Terminated and AThread.Connection.Connected then
begin
try
FillChar(sCommand,sizeof(sCommand),#0);
len:=AThread.Connection.ReadFromStack(false,-1,false);
if len>0 then
begin
AThread.Connection.ReadBuffer(sCommand,len);
Meo_Rec.Lines.Add('Asc显示为:'+sCommand);
Meo_Rec.Lines.Add('Hex显示为:'+EncodeHEX(sCommand,len))
end;
except
end;
end;
end;
procedure TMainFrm.FormClose(Sender: TObject; var Action: TCloseAction);
begin
IdTCPServer.Active := False;
end;
{function TMainFrm.EncodeHEX(A : String) : String;
var
N : Integer;
begin
N := Length(A);
SetLength(Result,N * 2 + 1);
BinToHex(PChar(A),PChar(Result),N);
end;}
function TMainFrm.EncodeHEX(AChar ;len:Integer) : String;
var
N : Integer;
begin
N := len;
SetLength(Result,N * 2 + 1);
BinToHex(PChar(A),PChar(Result),N);
end;
procedure TMainFrm.BtnSendClick(Sender: TObject);
var
tmp:array[0..1024] of char;
i,Count:integer;
str:string;
List:TList;
begin
if IdTCPServer.Active then
begin
str:= trim(EdtSend.Text);
if str='' then
begin
showmessage('不能为空!');
EdtSend.SetFocus;
Exit;
end;
try
if not ChkSndHex.Checked then
begin
strpcopy(tmp,str);
List := IdTCPServer.Threads.LockList; //取得所有客户列表
try
for Count := 0 to List.Count -1 do //遍历所有客户
try
TIdPeerThread(List.Items[Count]).Connection.WriteBuffer(tmp,length(str)); //向客户写字符串
except
TIdPeerThread(List.Items[Count]).Stop;
end;
finally
IdTCPServer.Threads.UnlockList;
end;
end
else
begin
if (length(str) mod 2)<>0 then
begin
showmessage('位数必须为偶数');
exit;
end
else
begin
FillChar(tmp,sizeof(tmp),#0);
for i:=0 to (length(str) div 2-1) do
begin
try
tmp:= char( strtoint('$'+ Copy(str,i*2+1,2)));
except
showmessage('格式不对!请查证!');
EdtSend.SetFocus;
Exit;
end;
end;
List := IdTCPServer.Threads.LockList; //取得所有客户列表
try
for Count := 0 to List.Count -1 do //遍历所有客户
try
TIdPeerThread(List.Items[Count]).Connection.WriteBuffer(tmp,length(str) div 2); //向客户写字符串
except
TIdPeerThread(List.Items[Count]).Stop;
end;
finally
IdTCPServer.Threads.UnlockList;
end;
end;
end;
except
end;
end
else
begin
showmessage('端口还没有打开');
end;
end;
procedure TMainFrm.BtnAboutClick(Sender: TObject);
begin
showmessage('端口监听工具'+#10#13+'Design by Gigi!'+#13#10+'EMail:soulmate_gigi@163.com!');
end;
procedure TMainFrm.BtnClearClick(Sender: TObject);
begin
Meo_Rec.Clear;
end;
procedure TMainFrm.FormCreate(Sender: TObject);
begin
BtnStop.Enabled :=false;
end;
end.
按道理说应该在DisConnect事件中例如在Memo控件中显示断线的客户端IP地址,但我发现只要在Disconnect事件中调用显示程序就抛出异常,另外也根本检测不到客户端的断线问题。
这个问题一直也没有解决,希望有作过的或给指点一下,我想Indy既然做出这个东西,总不至于连这些细节都不能解决吧,一定是我那里没有搞明白~
unit Main;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Buttons, IdBaseComponent, IdComponent, IdTCPServer,
IdTCPConnection, IdTCPClient, IdAntiFreezeBase, IdAntiFreeze;
type
TMainFrm = class(TForm)
EdtPort: TEdit;
Meo_Rec: TMemo;
BtnStart: TBitBtn;
BtnSend: TBitBtn;
BtnStop: TBitBtn;
IdTCPServer: TIdTCPServer;
EdtSend: TEdit;
ChkSndHex: TCheckBox;
BtnAbout: TBitBtn;
BtnClear: TBitBtn;
IdAntiFreeze: TIdAntiFreeze;
procedure BtnStartClick(Sender: TObject);
procedure BtnStopClick(Sender: TObject);
procedure IdTCPServerConnect(AThread: TIdPeerThread);
procedure IdTCPServerExecute(AThread: TIdPeerThread);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure BtnSendClick(Sender: TObject);
procedure BtnAboutClick(Sender: TObject);
procedure BtnClearClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
//function EncodeHEX(A : String) : String;
function EncodeHEX(AChar;len:Integer) : String;
end;
var
MainFrm: TMainFrm;
implementation
{$R *.dfm}
procedure TMainFrm.BtnStartClick(Sender: TObject);
begin
IdTCPServer.DefaultPort := StrToInt(EdtPort.Text);
IdTCPServer.Active := True;
BtnStart.Enabled := False;
BtnStop.Enabled := True;
Meo_Rec.Lines.Add('服务器已成功启动!');
end;
procedure TMainFrm.BtnStopClick(Sender: TObject);
begin
if (IdTCPServer.Active=true) then
begin
IdTCPServer.Active := False;
BtnStart.Enabled := True;
BtnStop.Enabled := False;
Meo_Rec.Lines.Add('服务器已成功停止!');
end;
end;
procedure TMainFrm.IdTCPServerConnect(AThread: TIdPeerThread);
begin
Meo_Rec.Lines.Add('来自主机 '
+ AThread.Connection.Socket.Binding.PeerIP
+ ' 的连接请求已被接纳!');
end;
procedure TMainFrm.IdTCPServerExecute(AThread: TIdPeerThread);
var
len:integer;
sCommand: array[0..4096] of char;
begin
if not AThread.Terminated and AThread.Connection.Connected then
begin
try
FillChar(sCommand,sizeof(sCommand),#0);
len:=AThread.Connection.ReadFromStack(false,-1,false);
if len>0 then
begin
AThread.Connection.ReadBuffer(sCommand,len);
Meo_Rec.Lines.Add('Asc显示为:'+sCommand);
Meo_Rec.Lines.Add('Hex显示为:'+EncodeHEX(sCommand,len))
end;
except
end;
end;
end;
procedure TMainFrm.FormClose(Sender: TObject; var Action: TCloseAction);
begin
IdTCPServer.Active := False;
end;
{function TMainFrm.EncodeHEX(A : String) : String;
var
N : Integer;
begin
N := Length(A);
SetLength(Result,N * 2 + 1);
BinToHex(PChar(A),PChar(Result),N);
end;}
function TMainFrm.EncodeHEX(AChar ;len:Integer) : String;
var
N : Integer;
begin
N := len;
SetLength(Result,N * 2 + 1);
BinToHex(PChar(A),PChar(Result),N);
end;
procedure TMainFrm.BtnSendClick(Sender: TObject);
var
tmp:array[0..1024] of char;
i,Count:integer;
str:string;
List:TList;
begin
if IdTCPServer.Active then
begin
str:= trim(EdtSend.Text);
if str='' then
begin
showmessage('不能为空!');
EdtSend.SetFocus;
Exit;
end;
try
if not ChkSndHex.Checked then
begin
strpcopy(tmp,str);
List := IdTCPServer.Threads.LockList; //取得所有客户列表
try
for Count := 0 to List.Count -1 do //遍历所有客户
try
TIdPeerThread(List.Items[Count]).Connection.WriteBuffer(tmp,length(str)); //向客户写字符串
except
TIdPeerThread(List.Items[Count]).Stop;
end;
finally
IdTCPServer.Threads.UnlockList;
end;
end
else
begin
if (length(str) mod 2)<>0 then
begin
showmessage('位数必须为偶数');
exit;
end
else
begin
FillChar(tmp,sizeof(tmp),#0);
for i:=0 to (length(str) div 2-1) do
begin
try
tmp:= char( strtoint('$'+ Copy(str,i*2+1,2)));
except
showmessage('格式不对!请查证!');
EdtSend.SetFocus;
Exit;
end;
end;
List := IdTCPServer.Threads.LockList; //取得所有客户列表
try
for Count := 0 to List.Count -1 do //遍历所有客户
try
TIdPeerThread(List.Items[Count]).Connection.WriteBuffer(tmp,length(str) div 2); //向客户写字符串
except
TIdPeerThread(List.Items[Count]).Stop;
end;
finally
IdTCPServer.Threads.UnlockList;
end;
end;
end;
except
end;
end
else
begin
showmessage('端口还没有打开');
end;
end;
procedure TMainFrm.BtnAboutClick(Sender: TObject);
begin
showmessage('端口监听工具'+#10#13+'Design by Gigi!'+#13#10+'EMail:soulmate_gigi@163.com!');
end;
procedure TMainFrm.BtnClearClick(Sender: TObject);
begin
Meo_Rec.Clear;
end;
procedure TMainFrm.FormCreate(Sender: TObject);
begin
BtnStop.Enabled :=false;
end;
end.