Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED]Problem when using second socket.
QtWS25 Last Chance

[SOLVED]Problem when using second socket.

Scheduled Pinned Locked Moved General and Desktop
smtphtmltcpsocket
12 Posts 2 Posters 3.9k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    ealione
    wrote on 24 Mar 2015, 15:16 last edited by ealione
    #1

    I have created a multithreaded html server that can receive and send data, including images and other files. I wanted to add the ability to send messages so I created a different library that connects to a host (e.g. smtp.google.com) and acts as a simple smtp client

    socket->connectToHost(host, port);
    

    and after logging in it writes my message

    socket->write(text.toUtf8() + "\r\n");
    

    If I test this on its own it works as expected and all messages get send.

    After invoking this from my server though I get an error.

    The server listens for traffic using another socket. When a user connects and visits a link for example I try to use my smtp client to send an email only to get this error

    QIODevice::write: device not open
    
    1 Reply Last reply
    0
    • M Offline
      M Offline
      mcosta
      wrote on 24 Mar 2015, 15:57 last edited by
      #2

      Sorry,

      can you post the code that is not working??
      Do you wait for connected() signal before using write()?

      Once your problem is solved don't forget to:

      • Mark the thread as SOLVED using the Topic Tool menu
      • Vote up the answer(s) that helped you to solve the issue

      You can embed images using (http://imgur.com/) or (http://postimage.org/)

      1 Reply Last reply
      0
      • E Offline
        E Offline
        ealione
        wrote on 25 Mar 2015, 08:13 last edited by
        #3

        Yes I do wait for the socket to connect using

        if (!socket->waitForConnected(connectionTimeout))
        {
            return false;
        }
        

        And its hard to post the code as it has turned out to be quite lengthy. Yet my smtp client is a barebones version of this . Any changes that I have made could not be responsible as the way I connect and send the data is the same, and as I said it works fine by itself. As for the http server it contains a worker class that deals with sockets (moves them into a separate thread, connects to the host etc) and a tcpServer that uses the worker class in order to deal with multiple connections.

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mcosta
          wrote on 25 Mar 2015, 08:21 last edited by
          #4

          Is write() called just after that??

          Once your problem is solved don't forget to:

          • Mark the thread as SOLVED using the Topic Tool menu
          • Vote up the answer(s) that helped you to solve the issue

          You can embed images using (http://imgur.com/) or (http://postimage.org/)

          1 Reply Last reply
          0
          • E Offline
            E Offline
            ealione
            wrote on 25 Mar 2015, 08:42 last edited by ealione
            #5

            No after that I wait for the smtp server to respond using

            waitForResponse();
            

            after that I follow with all the procedure that has to take place in order to communicate with an smtp server, like sending the EHLO/HELO message

            socket->write(text.toUtf8() + "\r\n");
            if (! socket->waitForBytesWritten(sendMessageTimeout))
            {
              throw SendMessageTimeoutException();
            }
            
            1 Reply Last reply
            0
            • M Offline
              M Offline
              mcosta
              wrote on 25 Mar 2015, 08:46 last edited by
              #6

              Seems that socket is not connected when you use write().
              I suggest to handle the QAbstractSocket::stateChanged() signal to understand when the state changes after connection

              Once your problem is solved don't forget to:

              • Mark the thread as SOLVED using the Topic Tool menu
              • Vote up the answer(s) that helped you to solve the issue

              You can embed images using (http://imgur.com/) or (http://postimage.org/)

              1 Reply Last reply
              0
              • E Offline
                E Offline
                ealione
                wrote on 25 Mar 2015, 08:53 last edited by
                #7

                I did and it seems that it never changes state. I do not even connect with smtp.google when I use this class form inside my http server, which is weird as it connects just fine otherwise.

                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  mcosta
                  wrote on 25 Mar 2015, 09:07 last edited by
                  #8

                  Are you sure your socket is not changed is some part in the code??

                  Once your problem is solved don't forget to:

                  • Mark the thread as SOLVED using the Topic Tool menu
                  • Vote up the answer(s) that helped you to solve the issue

                  You can embed images using (http://imgur.com/) or (http://postimage.org/)

                  1 Reply Last reply
                  0
                  • E Offline
                    E Offline
                    ealione
                    wrote on 25 Mar 2015, 09:19 last edited by
                    #9

                    Yup you are correct I should have looked more carefully at my implementation. I had a placed an ! symbol incorrectly. Now as you suggested I can see the states. This is what I get

                    Sending verification email.
                    QAbstractSocket::HostLookupState
                    QAbstractSocket::ConnectingState
                    QAbstractSocket::ConnectedState
                    Failed to connect to host! 
                    

                    The last comment comes from

                    smtp.connectToHost()
                    

                    Seems that it returns false, but at least this is a step forward.

                    1 Reply Last reply
                    0
                    • M Offline
                      M Offline
                      mcosta
                      wrote on 25 Mar 2015, 09:26 last edited by
                      #10

                      Are you sure you don't need to have a SSL socket?? I think at least smtp.gmail.com requires it

                      Once your problem is solved don't forget to:

                      • Mark the thread as SOLVED using the Topic Tool menu
                      • Vote up the answer(s) that helped you to solve the issue

                      You can embed images using (http://imgur.com/) or (http://postimage.org/)

                      1 Reply Last reply
                      0
                      • E Offline
                        E Offline
                        ealione
                        wrote on 25 Mar 2015, 09:31 last edited by
                        #11

                        Yup that was it. Realised it just before I read your comment.
                        I am marking this as solved, thanks for the help mcosta.

                        1 Reply Last reply
                        0
                        • M Offline
                          M Offline
                          mcosta
                          wrote on 25 Mar 2015, 09:33 last edited by
                          #12

                          You're welcome

                          Once your problem is solved don't forget to:

                          • Mark the thread as SOLVED using the Topic Tool menu
                          • Vote up the answer(s) that helped you to solve the issue

                          You can embed images using (http://imgur.com/) or (http://postimage.org/)

                          1 Reply Last reply
                          0

                          9/12

                          25 Mar 2015, 09:19

                          • Login

                          • Login or register to search.
                          9 out of 12
                          • First post
                            9/12
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • Users
                          • Groups
                          • Search
                          • Get Qt Extensions
                          • Unsolved