Results 1 to 13 of 13

Thread: Using QTcpSockt to check an IMAP server

  1. #1
    Join Date
    Jan 2006
    Location
    Paris, France
    Posts
    227
    Thanks
    3
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Red face Using QTcpSockt to check an IMAP server

    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
    Attached Files Attached Files

  2. #2
    Join Date
    Jan 2006
    Location
    La Spezia,Italy
    Posts
    77
    Thanks
    9
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Using QTcpSockt to check an IMAP server

    Quote Originally Posted by guilugi
    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

  3. #3
    Join Date
    Jan 2006
    Location
    Paris, France
    Posts
    227
    Thanks
    3
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Using QTcpSockt to check an IMAP server

    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.

  4. #4
    Join Date
    Jan 2006
    Location
    La Spezia,Italy
    Posts
    77
    Thanks
    9
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Using QTcpSockt to check an IMAP server

    Quote Originally Posted by guilugi
    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.

  5. #5
    Join Date
    Jan 2006
    Location
    Paris, France
    Posts
    227
    Thanks
    3
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Using QTcpSockt to check an IMAP server

    Here it is !

    Thanks.
    Attached Files Attached Files

  6. #6
    Join Date
    Jan 2006
    Location
    La Spezia,Italy
    Posts
    77
    Thanks
    9
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Using QTcpSockt to check an IMAP server

    Hrmmm. Try to pass the socket as a pointer and not as a reference, I
    mean:

    Qt Code:
    1. void MailChecker::writeToSocket( QTcpSocket *socket, const QString &str )
    To copy to clipboard, switch view to plain text mode 

    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:
    Qt Code:
    1. if( waitForBytesWritten(timeOut) ) return 0;
    2. else return -1;
    To copy to clipboard, switch view to plain text mode 
    to control the write event.

  7. #7
    Join Date
    Jan 2006
    Location
    Paris, France
    Posts
    227
    Thanks
    3
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Using QTcpSockt to check an IMAP server

    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

  8. #8
    Join Date
    Jan 2006
    Location
    La Spezia,Italy
    Posts
    77
    Thanks
    9
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Using QTcpSockt to check an IMAP server

    Quote Originally Posted by guilugi
    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?
    Quote Originally Posted by guilugi
    Thanks anyway, I'll check again my code this evening.

    Guilugi
    You are welcome!

  9. #9
    Join Date
    Jan 2006
    Location
    Paris, France
    Posts
    227
    Thanks
    3
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Using QTcpSockt to check an IMAP server

    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
    Attached Files Attached Files

  10. #10
    Join Date
    Jan 2006
    Location
    La Spezia,Italy
    Posts
    77
    Thanks
    9
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Using QTcpSockt to check an IMAP server

    Ohhh, I realized that you used QTextStream to write on the socket...
    You have to use QDataStream to write on a socket.

  11. #11
    Join Date
    Jan 2006
    Location
    Paris, France
    Posts
    227
    Thanks
    3
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Using QTcpSockt to check an IMAP server

    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.

  12. #12
    Join Date
    Jan 2006
    Location
    Paris, France
    Posts
    227
    Thanks
    3
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Using QTcpSockt to check an IMAP server

    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.

  13. #13
    Join Date
    Jan 2006
    Location
    La Spezia,Italy
    Posts
    77
    Thanks
    9
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Using QTcpSockt to check an IMAP server

    Quote Originally Posted by guilugi
    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!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.