[Qt-interest] Can two thread share a QAbstractSocket?
Gabriele Kahlout
gabriele at mysimpatico.com
Wed May 12 20:00:16 CEST 2010
> A socket descriptor is not a QAbstractSocket. It's like comparing a byte to
> a
> QString.
>
Okay. Qt forbids:
*Note:* It is not possible to initialize two abstract sockets with the same
native socket descriptor.
This is a very subtle note/warning of what I requested to be in the
documentation.
>
> 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?
>
Transfer images. So a client may tell the server, send this image/screenshot
(QByteArray?) to X. Who should receive and serve this call? In the
arcitecture I'm familiar to (the java code) a separate thread handles all of
that.
1. Single thread serving all requests: slow, while a client requested only
to 'login' or a small image, it must wait before a long one of another is
sent. Also, confusing.
2. Spawning a thread with QtConcurrent.run to serve requests, is the same
as I'm trying to do, only that the thread lives only once.
>
> > 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.
>
True this code is TCP. However, I'd like to use UDP now, hoping to exploit
that "QAbstractSocket's API unifies most of the differences between the two
protocols".
>
> void SomeObject::slotNewConnection()
> {
> QTcpSocket *clientSocket = listener->nextPendingConnection();
> ClientHandler *handler = new ClientHandler;
>
clientSocket->moveToThread(handler);
handler->start(); // in my code i put this at the end of the constructor.
Not recommended?
> }
>
> > -----
> > ClientHandler
> >
> > public void run() {
> > while (true) {
> >
> > String msg = receive();
> > }
>
> void ClientHandler::run()
> {
> SomeOtherObject obj;
>
//Why is this needed? Wouldn't " this-> " do too?
> obj.connect(clientSocket, SIGNAL(readyRead()),
> SLOT(slotReadyRead()));
>
//or waitForReadyRead().
> exec();
> }
>
> void SomeOtherObject::slotReadyRead()
> {
> QByteArray msg = clientSocket->readAll();
> }
>
> Note that the threading bits in my code are completely unnecessary.
>
// u mean the SomeOtherObject stuff?
>
> PS: please exercise quoting in your replies to emails.
>
What do you mean? I know quoting to mean using quotes "", and to cite
authors of works I refer to. What's the issue here?
> --
> 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
>
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at trolltech.com
> http://lists.trolltech.com/mailman/listinfo/qt-interest
>
>
--
Regards,
K. Gabriele
--- unchanged since 25/1/10 ---
P.S. Unless a notification (LON), please reply either with an answer OR with
" ACK" appended to this subject within 48 hours. Otherwise, I might resend.
In(LON, this) ∨ In(48h, TimeNow) ∨ ∃x. In(x, MyInbox) ∧ IsAnswerTo(x, this)
∨ (In(subject(this), subject(x)) ∧ In(ACK, subject(x)) ∧
¬IsAnswerTo(x,this)) ⇒ ¬IResend(this).
Also note that correspondence may be received only from specified a priori
senders, or if the subject of this email ends with a code, eg. -LICHT01X,
then also from senders whose reply contains it.
∀x. In(x, MyInbox) ⇒ In(senderAddress(x), MySafeSenderList) ∨ (∃y. In(y,
subject(this) ) ∧ In(y,x) ∧ isCodeLike(y, -LICHT01X) ).
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20100512/dbb1df8f/attachment.html
More information about the Qt-interest-old
mailing list