[SOLVED]Problem when using second socket.

  • 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

  • Sorry,

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

  • 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.

  • Is write() called just after that??

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


    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();

  • 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

  • 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.

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

  • 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.
    Failed to connect to host! 

    The last comment comes from


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

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

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

  • You're welcome

Log in to reply