如何使用QThreads在多个QTcpSockets上编写? [英] How to write on multiple QTcpSockets using QThreads?

查看:129
本文介绍了如何使用QThreads在多个QTcpSockets上编写?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在QTcpSockets和QThreads上苦苦挣扎了几天.

I've been struggling for a couple of days now with this problem on QTcpSockets and QThreads.

我有一个QTcpServer,它侦听端口并使用nextPendingConnection()创建一个新的客户端.所以现在客户端有了一个qtcpsocket,我可以用它来读写.

I have a QTcpServer that listens on a port and creates a new Client using the nextPendingConnection(). So now the client has a qtcpsocket which I can use for reading and writing.

假设我有100个客户端连接到我的服务器.当其中一个想要向所有人广播消息时,我的主线程(使用nextPendingConnection()创建客户端的地方)将必须迭代100多个客户端,并在其套接字上调用write方法.例如,如果有10个用户同时广播,那么我将不得不进行1000次迭代,因此我认为客户端(当用户收到消息时)会有一些延迟.

Let's suppose I have 100 clients connected to my server. When one of them wants to broadcast a message to everybody, my main thread (where I create the clients with nextPendingConnection() ) will have to iterate over 100 clients and call the write method on their sockets. If for example 10 users broadcast at the same time, I will have to make 1000 iterations, so in my opinion there will be some latency on the client side(when the user receive the messages).

我想在Separe线程中完成所有套接字写操作,以便我可以并行发送数据.我知道我无法从创建它的线程中调用另一个线程的套接字,并且我不想为每个客户端使用一个线程,因为我不知道拥有很多线程是否是一个好主意(此外,除非客户端断开连接,否则这些线程应始终运行).

I would like to do all the socket writing in separe threads, so that I can send data in paralel. I know that I can't call a socket from another thread from the one that it was created in and I don't want to use one thread per client because I don't know if it's a good think to have a lot of threads(besides, these threads should be always running unless a client disconnects).

我已经阅读了线程财富服务器示例,但是它没有解决我的问题,因为它们在发送数据后破坏了套接字;我不想破坏我的套接字,因为我必须在套接字上侦听传入的消息(这样我才能广播它们).

I've read the thread fortune server example, but it doesn't answer my problem because they destroy the socket after sending data; I don't want to destroy my socket because I have to listen on it for the incoming messages(so I can broadcast them).

我考虑过的一件事是为每个客户端(在服务器和客户端)有2个套接字;这样我就可以保留一个套接字进行读取,而另一个线程可以像在线程财富服务器示例中那样使用;在这种情况下,我还将使用线程池来限制任务数量(否则将与每个客户端使用一个线程相同).

One thing I thought about is to have 2 sockets for each client(on the server and on the client side); so that I keep one socket for reading, and the other one i can use just like in the thread fortune server example; in this case, i would also use a threadpool to limit the number of tasks (otherwise it would be the same thing as using one thread per client).

能否请您指出正确的方向或给我一些提示,以了解如何在没有每个客户端实际拥有一个线程的情况下实现并行套接字编写??

Could you please point me in the right direction or give me some hints about how implementing paralel socket writing without actually having one thread per client...?

最好的问候,

塞巴斯蒂安

推荐答案

具有一个"WriteToSocket"函数,该函数将数据写入特定的套接字.另外,还有一个队列,该队列保存受套接字约束的数据,并受互斥锁保护.在函数中,将要发送的数据添加到套接字的队列中.检查套接字是否已分配给线程(为此保留标记).如果是这样,那么您就完成了.如果没有,则将作业计划到线程池以将数据发送到套接字.

Have a 'WriteToSocket' function that writes data to a particular socket. Also, have a queue that holds data bound for a socket, protected by a mutex. In the function, add the data you want to send to the queue for the socket. Check if the socket is already assigned to a thread (keep a flag for this purpose). If so, you're done. If not, schedule a job to a thread pool to send the data out the socket.

根据许多细节,您可能更喜欢其他实现.在此实现中,每个套接字的发送逻辑具有两种模式:已排队和未排队.套接字开始不排队.当您要发送到套接字时,请获取保护它的锁.如果它处于排队模式,则将数据添加到队列中就可以了.

Depending on a lot of details, you may prefer an alternative implementation. In this implementation, the send logic for each socket has two modes, queued and unqueued. Sockets start unqueued. When you want to send to a socket, acquire the lock that protects it. If it's in queued mode, add the data to the queue and you're done.

如果您处于非排队模式,请尝试使用非阻塞发送来发送数据.如果将所有内容都发送出去,那就完成了.否则,将未发送的数据保存到队列中,然后将套接字置于队列模式.

If you're in unqueued mode, try to send the data with a non-blocking send. If you send all of it, you're done. Otherwise, save the unsent data to the queue and put the socket in queued mode.

您将需要安排某种机制来定期"尝试以排队模式在套接字上发送数据.通常,您可以使用选择"或投票"之类的方法来执行此操作.使用QTcpSockets,您可以信号和插槽.

You will need to arrange some mechanism to 'periodically' attempt to send data on the sockets in queued mode. Normally, you would do that with something like 'select' or 'poll'. With QTcpSockets, you have signals and slots.

这篇关于如何使用QThreads在多个QTcpSockets上编写?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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