将相同的信息发送到多个线程/套接字?

发布于 2024-12-24 02:05:45 字数 231 浏览 1 评论 0原文

我正在编写一个服务器应用程序,它只需将本地串行端口连接到多个网络连接的客户端。我使用linux和C语言作为服务器应用程序,因为该程序的设备是内存有限的路由器。

我已经为多个客户端进行了所有设置,以连接每个连接并使用 fork() 进程将数据发送到串行端口。

我的问题在于将串行端口上传入的数据发送到多个(数量不同)客户端连接。我的问题在于为每个活动套接字设计一种方法来获取所有传入数据,并且只获取一次。有什么帮助吗?

I am writing a server application that simply connects a local serial port to multiple network connected clients. I am using linux and C for the server application because the equipment for the program is a router with limited memory.

I have everything setup for multiple clients to connect and send data to the serial port using a fork() process for each connection.

My problem lies in getting data incoming on the serial port out to the multiple (varing number) client connections. my problem lies in designing a way for each active socket to get all of the incoming data, and to only get it once. Any help?

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

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

发布评论

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

评论(3

尬尬 2024-12-31 02:05:45

听起来您需要为每个连接的客户端一个数据队列(缓冲区)。每次数据进入端口时,您都将其发布到每个客户端队列的后面。然后,客户端从各自队列的前端读取数据。由于所有客户端可能会以不同的速率/时间读取,这将确保所有客户端仅获得一次数据副本,并且当更多数据进入时,您不会因等待任何一个客户端而挂断。当然,您需要为每个连接的客户端队列分配一定量的内存(我不确定您期望有多少客户端,并且您确实说过可用内存有限),并且您需要考虑如果出现以下情况该怎么办在客户端读取全部队列之前,队列已满。

Sounds like you need a data queue (buffer) for each connected client. Each time data comes in on the port, you post it to the back of each client's queue. The clients then read the data from the front of their respective queues. Since all the clients will probably read at different rates/times, this will ensure all of them get a copy of the data only once, and you won't get hung up waiting for any one client while more data comes in. Of course, you'll need to allocate a certain amount of memory for each connected client's queue (I'm not sure how many clients you're expecting, and you did say your available memory is limited), and you need to consider what to do if a queue gets full before the client reads all of it.

琴流音 2024-12-31 02:05:45

假设您保留了连接客户端的列表或其他一些引用,为什么不循环遍历每一位信息并将其发送给所有客户端呢?

Presumably you keep a list or some other reference of/to connected clients, why not just loop over that for each bit of information and send it to all of them?

睫毛溺水了 2024-12-31 02:05:45

每套接字线程设计可能不是解决此问题的最佳方法。事件驱动的异步方法应该更适合。但是,如果必须使用线程来完成此操作,并且考虑到串行端口无论如何都很慢,那么在侦听串行端口的线程和与网络客户端通信的所有线程之间构建一个管道是最实用的。您可以使用 rwlocks 做一些奇特的事情来移动数据,但是您仍然需要一种方法让网络线程同时等待套接字和来自串行端口的数据,因此您需要对两者使用文件描述符,例如轮询。

但说真的,如果没有线程,这可能会更容易并且性能会更好。将其视为一个主循环,等待轮询,轮询正在监视网络和串行端口,确定发生了哪个事件,并相应地分发数据。一旦你有了想法,一切都应该变得更容易。

A thread per socket design might not be the best way to solve this. An event driven asynchronous approach should be a much better fit. However, if you must do it with threads, and given that serial ports are slow anyway, building a pipe between the thread listening to the serial port and all the threads talking to the network clients is the most practical. You could do fancy things with rwlocks to move the data, but you'll still need a way for the network threads to wait on both the socket and the data from the serial port, so you need to use file descriptors for both and something like poll.

But seriously, this would likely be much easier and would perform better without the threads. Think of it as a main loop which waits on poll which is watching the network and the serial port, determines which event occurred, and distributes data accordingly. It should be easier all around once you get the idea.

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