[Qt-interest] Can two thread share a QAbstractSocket?
Thiago Macieira
thiago at kde.org
Wed May 12 19:18:59 CEST 2010
Em Quarta-feira 12. Maio 2010, às 18.45.41, Gabriele Kahlout escreveu:
> And I piggybag two question:
>
> 1. Not even a socket descriptor? I'm currently able to pass it through
> threads. This is related to the why now question.
A socket descriptor is not a QAbstractSocket. It's like comparing a byte to a
QString.
> 2.How can I achieve the wanted behavior:
>
> A server (preferably udp) receives incoming client connections, and then
> spawns a thread that deals (receive all requests from that client and
> serves them in turn).
First, I have to question why you need a thread at all. What kind of
operations do you plan on doing that require a thread?
> The java is:
>
> public void run() {
> while (true) {
> Socket clientSocket = listener.accept();
> new ClientHandler(clientSocket);
> }
UDP has no "accept". So I will assume you meant TCP servers.
void SomeObject::slotNewConnection()
{
QTcpSocket *clientSocket = listener->nextPendingConnection();
ClientHandler *handler = new ClientHandler;
clientSocket->moveToThread(handler);
handler->start();
}
> -----
> ClientHandler
>
> public void run() {
> while (true) {
>
> String msg = receive();
> }
void ClientHandler::run()
{
SomeOtherObject obj;
obj.connect(clientSocket, SIGNAL(readyRead()), SLOT(slotReadyRead()));
exec();
}
void SomeOtherObject::slotReadyRead()
{
QByteArray msg = clientSocket->readAll();
}
Note that the threading bits in my code are completely unnecessary.
PS: please exercise quoting in your replies to emails.
--
Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
Senior Product Manager - Nokia, Qt Development Frameworks
PGP/GPG: 0x6EF45358; fingerprint:
E067 918B B660 DBD1 105C 966C 33F5 F005 6EF4 5358
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 190 bytes
Desc: This is a digitally signed message part.
Url : http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20100512/6efcb18e/attachment.bin
More information about the Qt-interest-old
mailing list