关于网友提出的“ TIdTCPServer 服务器端 如何断开 Client 问题”问题疑问,本网通过在网上对“ TIdTCPServer 服务器端 如何断开 Client 问题”有关的相关答案进行了整理,供用户进行参考,详细问题解答如下:
问题: TIdTCPServer 服务器端 如何断开 Client 问题
描述: 客户端 连接 server后,发送信息都是正常的!
当服务器端 强行关闭或者特殊原因关闭的时候,如何断开客户端;现在是如果客户端在线,那么关闭服务器端后,客户端Connected 属性依然 = True 很奇怪。
OnCLoseQuery 事件
Try
List := TIdTCPServer.Threads.LockList; //取得所有客户列表
if List.Count > 0 then
for Count := 0 to List.Count -1 do //遍历所有客户
try
if TIdPeerThread(List.Items[Count]).Connection.Connected then
TIdPeerThread(List.Items[Count]).Connection.Disconnect; //执行到这里后,出错,无法断开客户端的链接,不知道是什么原因
except
TIdPeerThread(List.Items[Count]).Stop;
end;
Except
TIdTCPServer.Active := False;
TIdTCPServer.Threads.UnlockList;
end;
解决方案1: 已经关闭了,再关闭一次肯定出错
解决方案2: 以下是我之前写的server端代码,直接Active置为False,没问题啊。
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, IdBaseComponent, IdComponent, IdTCPServer,StrUtils;
type
TForm1 = class(TForm)
Label1: TLabel;
Edit1: TEdit;
Label2: TLabel;
Edit2: TEdit;
Label3: TLabel;
ListBox1: TListBox;
Button1: TButton;
Button2: TButton;
Button3: TButton;
IdTCPServer1: TIdTCPServer;
procedure Button3Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure IdTCPServer1Execute(AThread: TIdPeerThread);
procedure IdTCPServer1Connect(AThread: TIdPeerThread);
private
{ Private declarations }
public
{ Public declarations }
procedure DisplayData();
procedure AddLogEntry();
end;
var
Form1: TForm1;
FLogEntry:string;
FReceived:string;
implementation
{$R *.dfm}
procedure TForm1.DisplayData();
begin
edit2.Text := FReceived;
end;
procedure TForm1.AddLogEntry();
begin
self.ListBox1.Items.Add(FLogEntry);
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
close;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
IdTCPServer1.DefaultPort := StrToInt(edit1.Text);
IdTCPServer1.Active := True;
self.Button1.Enabled := False;
self.Button2.Enabled := True;
self.ListBox1.Items.Add('服务器已成功启动!');
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
IdTCPServer1.Active := False;
self.Button1.Enabled := True;
self.Button2.Enabled := False;
self.ListBox1.Items.Add('服务器已成功停止!');
end;
procedure TForm1.IdTCPServer1Execute(AThread: TIdPeerThread);
var
sCommand: string;
begin
with AThread.Connection do
begin
sCommand := ReadLn();
FLogEntry := sCommand + ' 来自于主机 '+ AThread.Connection.Socket.Binding.PeerIP;
AThread.Synchronize(AddLogEntry);
if AnsiStartsText('DATA ', sCommand) then
begin
FReceived := RightStr(sCommand, Length(sCommand)-5);
WriteLn('200: 数据接收成功!');
AThread.Synchronize(DisplayData);
end
else if SameText(sCommand, 'QUIT') then
begin
FLogEntry := '断开同主机 '+ AThread.Connection.Socket.Binding.PeerIP + ' 的连接!';
AThread.Synchronize(AddLogEntry);
Disconnect;
end
else
begin
WriteLn('500: 无法识别的命令!');
FLogEntry := '无法识别命令:' + sCommand;
AThread.Synchronize(AddLogEntry);
end;//endif
end;
end;
procedure TForm1.IdTCPServer1Connect(AThread: TIdPeerThread);
begin
self.ListBox1.Items.Add('来自主机 '
+ AThread.Connection.Socket.Binding.PeerIP
+ ' 的连接请求已被接纳!');
AThread.Connection.WriteLn('100: 欢迎连接到简单TCP服务器!');
end;
end.
解决方案3: 啥错误,捕获一下
解决方案4: IdTCPServer1.Active := False; 不就直接断开了吗
以上介绍了“ TIdTCPServer 服务器端 如何断开 Client 问题”的问题解答,希望对有需要的网友有所帮助。
本文网址链接:http://www.codes51.com/itwd/3167105.html