构建多线程TCP / IP服务器 [英] Building Multi threaded TCP/IP Server

查看:83
本文介绍了构建多线程TCP / IP服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想构建一个TCP / IP服务器,最多可有100个并发客户端使用它,但仍然不确定如何入门。

I wanna build a TCP/IP server that will be used by up to 100 concurrent clients, but still not sure how to get started.

至少我需要服务器:


  1. 监听客户端,并将它们全部存储在数组或列表中。

  2. 对于每个客户端,它需要根据其客户端状态来接收和发送数据。

  3. 服务器应在有人连接或断开连接时更新客户端列表。

  4. 更喜欢使用GUI作为服务来对其进行管理。

  1. Listening to client, and store all them on array or list.
  2. for each client, it need to receive and send data based on it's client status.
  3. The server should update the clients list when someone connect or disconnect.
  4. Prefer to work as service with GUI to manage it.

有人可以帮助您开始使用它吗? indy示例,但它们并没有帮助,也查找了大多数组件,但仍在搜索。

Could anyone help how to get start with that,I looked at indy sample but they didn't help, also looked for most components but still searching.

推荐答案

您需要使用 TidTCPServer 是多线程内部的。无需您管理线程。一切都是透明的,因此(为几乎一个用户编写应用程序的方式)与为一个客户端编写应用程序的方式相同。请参阅 OnConnect 事件。有一个 TidContext 参数,其中包含一个TThreadList。您可以使用该事件将客户注册 /添加到自定义阵列/列表,并使用 OnDisconnect 删除客户端。

You need to use the TidTCPServer which is multithreaded inside. No need for you to manage the threads. Everything is transparent, so in the way in which you write the application for one client, in (almost) the same way you will write it for many. See the OnConnect event. There is a TidContext parameter which has a TThreadList inside. You can use that event to 'register'/add your clients to your custom array/list and OnDisconnect to remove the clients.

服务器接收到消息时,会触发 OnExecute 事件。使用其参数读取发送的消息。

The OnExecute event is triggered when the server receives a message. Use its parameters to read the message which is sent.

此外,您还需要另一个应用程序,它将是您的客户端,并使用 TidTCPClient 。在此应用程序中,您将设置服务器的地址(请参阅主机属性)以及应与服务器的端口匹配的端口。您应该调用 Connect (在服务器运行时),并使用 SendCmd 方法发送字符串。 (如果需要的话,也请参见 IOHandler.WriteLn

Also, you need another application which will be your client, using TidTCPClient. In this application you will set the address of your server (see the Host property) as well as the Port which should match with the server's one. You should call Connect (when the server is running) and to send strings you have SendCmd method. (Also see IOHandler.WriteLn if you want)

还有其他内容,但我认为这足以获取你开始了。您也可以在.Delphi.Winsock论坛中的Inmbarcadero论坛中发布Indy团队成员。或者,您可以直接在.Delphi.Non-Technical中询问,那里的人将为您提供指导。

There are also other things but I think that's enough to get you started. Also you can post in Embarcadero's forums in the .Delphi.Winsock forum where the Indy team members are floating over. Or perhaps you can ask directly in .Delphi.Non-Technical and the guys there will guide you.

另一种方法是 DataSnap ,这是一种Indy上的更多面向对象层(不要与DBX混淆)为您提供JSON,REST和其他优点。在这里

Another approach is DataSnap which is a more object-oriented layer over Indy (not to be confused with DBX) which gives your JSON, REST and other goodies. See for a small review here.

这篇关于构建多线程TCP / IP服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆