【发布时间】:2013-07-16 20:02:42
【问题描述】:
我使用这个 sn-p 创建一个 Indy10 TCPServer 的新实例:
procedure TPortWindow.AddPort (Item : TListItem);
var
Socket : TIdTcpServer;
begin
Socket := TIdTcpServer.Create(nil);
try
Socket.DefaultPort := strtoint (item.Caption);
Socket.OnConnect := MainWindow.OnConnect;
Socket.OnDisconnect := MainWindow.OnDisconnect;
Socket.OnExecute := MainWindow.OnExecute;
Socket.Active := TRUE;
except
Socket.Free;
OutputError ('Error','Port is already in use or blocked by a firewall.' + #13#10 +
'Please use another port.');
Item.Data := Socket;
Item.Checked := FALSE;
end;
end;
我用它来删除实例:
procedure TPortWindow.RemovePort (Item : TListItem);
var
Socket : TIdTcpServer;
begin
if Item.Data = NIL then Exit;
Socket := TIdTcpServer(Item.Data);
try
Socket.Active := FALSE;
finally
Socket.Free;
end;
Item.Data := NIL;
end;
由于某种原因,实例不会停止侦听并且所有客户端都保持连接状态。当我尝试创建前一个端口的新实例(删除后)时,它说该端口已在使用中,这意味着它没有停止侦听。
如何正确关闭此实例(并断开所有连接的客户端)?
编辑:
procedure TMainWindow.OnConnect(AContext: TIdContext);
begin
ShowMessage ('connected');
end;
procedure TMainWindow.OnDisconnect(AContext: TIdContext);
begin
ShowMessage ('disconnected');
end;
procedure TMainWindow.OnExecute(AContext: TIdContext);
begin
// Not defined yet.
end;
【问题讨论】:
-
这些事件在工作线程中触发,但
ShowMessage()不是线程安全的。请改用Windows.MessageBox(),或将异步消息发布到主线程并让它显示消息框。
标签: delphi indy indy10 delphi-xe4