PDA

View Full Version : Using QTcpSockt to check an IMAP server



guilugi
11th March 2006, 15:37
Hello there,

I have some troubles using QTcpSocket (Qt4)

My code does those simple steps :
- Connect to an IMAP server
- Login (with username / password pair)
- Look for new messages (if any)
- Logout

I checked the FortuneThreaded in the Qt4 doc, and tried to do something similar...
but it doesn't work that much : it seems I can only connect to server...
And the program seems to close unexpectedly, even if I put some sleep events...

I joined my two files in attachment !

Thanks in advance for any help,

Guilugi.

PS : I had some similar code made with Qt3, and it works perfectly, I'm sure that's a silly mistake from me :p

vratojr
13th March 2006, 09:07
Hello there,
I checked the FortuneThreaded in the Qt4 doc, and tried to do something similar...
but it doesn't work that much : it seems I can only connect to server...
And the program seems to close unexpectedly, even if I put some sleep events...

I think that it is because you have to call the:
waitForBytesWritten() and bytesAvailable() functions after read and write.

If you don't use signals/slots to send/receive you have to use these methos to work synchronously.

Another thing could be that you could try to use a longer timeout when trying to connect:

socket.waitForConnected(-1)

sometime I have noticed that the socket hangs if it cannot connect immediatly.

I hope it will help.

Simone

guilugi
13th March 2006, 10:55
Hey !

First, thanks for your reply.
Well, yes, I applied a Qt3 approach that isn't valid with Qt4, I completely forgot about those wait signals...

But I still have some problems with my method writeToSocket()...which doesn't seem to write data...at all
And when I add a waitForReadyRead(-1), the app loops...

Any idea ?

Thanks again,
Guilugi.

vratojr
13th March 2006, 10:58
Hey !
But I still have some problems with my method writeToSocket()...which doesn't seem to write data...at all
And when I add a waitForReadyRead(-1), the app loops...

Any idea ?


Post the new code,please.

guilugi
13th March 2006, 11:07
Here it is !

Thanks.

vratojr
13th March 2006, 11:20
Hrmmm. Try to pass the socket as a pointer and not as a reference, I
mean:


void MailChecker::writeToSocket( QTcpSocket *socket, const QString &str )


Using copy constructor you force the creation of a copy object and destruction of it. Passing pointer you don't. I think that in this process, you close the original socket and create a new,disconnected one.
Before doing this change you can try to call state() on the socket to check if it is connected or nor.

I also advice you to use a statement like:


if( waitForBytesWritten(timeOut) ) return 0;
else return -1;
to control the write event.

guilugi
13th March 2006, 14:32
Okay,

I made some modifications as you told me,...but problem is still here.
Socket seems to get stuck when writing some bytes....

Thanks anyway, I'll check again my code this evening.

Guilugi

vratojr
13th March 2006, 14:51
Okay,

I made some modifications as you told me,...but problem is still here.
Socket seems to get stuck when writing some bytes....

Thanks anyway, I'll check again my code this evening.

Guilugi

Have you tried to call state() on the socket to monitor its dis/connected state?
And,moreover,have you passed the socket as a pointer ALSO to the checkInbox function?


Thanks anyway, I'll check again my code this evening.

Guilugi
You are welcome!;)

guilugi
13th March 2006, 15:19
Yes sir, I changed both functions :)

I also connected my socket to stateChanged() to monitor, and I did a little call to state() in writeToSocket()...
and it returns 3 -> Connection Established...

I attached the new file !

Thanks,
Guilugi.

PS : I'm quite new to QtNetwork with Qt4, it seems far more complex than Qt3...but far more powerful too :)

vratojr
13th March 2006, 16:05
Ohhh, I realized that you used QTextStream to write on the socket...:p
You have to use QDataStream to write on a socket.

guilugi
13th March 2006, 16:36
Well, I used QDataStream at first, and then I switched to see if it made a difference !

But, once again, that doesn't solve my problem ;)
You know, this code exists in a Qt3 version, and it works like a charm...

Meantime, a Qt developer is having a look at it, and if he finds out (I hope so ;) ),
I'll keep you in touch.

Guilugi.

guilugi
16th March 2006, 10:07
Hi,

Finally got some news from the Qt guy :)

It seems there is a bug with QTcpSocket in Qt4.1.1...
He'll try to fix it soon ;)

Guilugi.

vratojr
20th March 2006, 08:51
Hi,

Finally got some news from the Qt guy :)

It seems there is a bug with QTcpSocket in Qt4.1.1...
He'll try to fix it soon ;)

Guilugi.

Keep me updated!:)