如何在客户端连接的 ListView 中存储标识符 - Indy 10

发布于 2024-12-19 12:03:18 字数 348 浏览 5 评论 0原文

我正在创建一个 TCP 服务器,它接收多个客户端并且必须能够向每个客户端发送消息。

如何获取客户端连接的句柄然后发送任意数据?

谢谢:D

代码:

procedure TFRM_Main.ServerConnect(AContext: TIdContext);
var lAdd: TListItem;
var Index: integer;
begin
  lAdd := ListView.Items.Add;
  //AContext connection ID, what to do here?
  lAdd.Caption := IntToStr(Index);
end;

I am creating a TCP server which receives multiple clients and must be able to send messages to each.

How do I get a handle to the client connection and then send arbitrary data?

Thanks: D

Code:

procedure TFRM_Main.ServerConnect(AContext: TIdContext);
var lAdd: TListItem;
var Index: integer;
begin
  lAdd := ListView.Items.Add;
  //AContext connection ID, what to do here?
  lAdd.Caption := IntToStr(Index);
end;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

疏忽 2024-12-26 12:03:18

TIdTCPServer 是一个多线程组件。直接从其 OnConnect 事件(或 OnDisconnectOnExecuteOnException)内访问 UI 不是线程。安全的!您需要使用 Indy 的 TIdSyncTIdNotify 类来安全地访问 UI。

要回答最初的问题,最简单但不一定是最安全的方法是将 TIdContext.Connection 对象指针存储在 TListItem.Data 属性中。主线程代码将在需要时直接访问连接。

但我不建议这样做。更安全的解决方案是自己唯一地标识每个客户端,例如使用客户端发送的用户名,并将该 ID 存储在 TIdContext.DataTListItem.Data 属性中。然后,当您的主线程代码想要向客户端发送消息时,它可以循环遍历 TIdTCPServer.Contexts 列表来查找所需的 ID,如果找到,则它将有权访问相应的 <代码>TIdContext.Connection对象。

TIdTCPServer is a multithreaded component. Accessing the UI directly from within its OnConnect event (or OnDisconnect, OnExecute, or OnException) is not thread-safe! You need to use Indy's TIdSync or TIdNotify class to access the UI safely.

To answer the original question, the simpliest, but not necessarily the safest, way is to store the TIdContext.Connection object pointer in the TListItem.Data property. The main thread code will then have direct access to the connection when it needs it.

I do not advise that, though. A safer solution is to uniquely identify each client yourself, such as with a username that the client sends, and store that ID in the TIdContext.Data and TListItem.Data properties. Then, when your main thread code wants to send a message to a client, it can loop through the TIdTCPServer.Contexts list looking for the desired ID, and if found then it will have access to the corresponding TIdContext.Connection object.

从来不烧饼 2024-12-26 12:03:18

使用:

AContext.Connection.IOHandler.Write( (* bytes *) );
AContext.Connection.IOHandler.WriteFile( (* send a file to the client *) );

要获得更多选项,只需在 IOHandler 之后调用代码完成(CTRL+SPACE)并查看可用选项,前段时间,我编写了一个简单的客户端/服务器测试应用程序,单击 此处 查看和/或下载源代码。

use:

AContext.Connection.IOHandler.Write( (* bytes *) );
AContext.Connection.IOHandler.WriteFile( (* send a file to the client *) );

For more options, just invoke the code completion(CTRL+SPACE) after IOHandler and see available options, some time ago, I've wrote a simple client/server test app, click here to see and/or download source.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文