Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QTcpSocket signals Error -1



  • Hi,

    I have a problem that I can't seem to figure out. When I do a connectToHost on my QTcpSocket it always signals error -1. However, the server side registers a client connection but my client always signal an unknown error. The server is a Windows application (not Qt). I have followed the example code for creating a client/server application.

    I'm running embedded Linux on a platform with an ARM Cortex-A8 processor. Apart from including QT += network in the .pro file, is there anythig else I need to do?

    Best Regards,
    Bubbas



  • Hi Bubbas
    I have done already tcp connections in a similar (equal) constellation. There was nothing special to do in the creation for the ARM platform.

    You might want to connect to a Qt based server on the windows side. This would help you to see, if there is a problem with your current windows server application.



  • Hi,

    I have tried to connect to different types of servers; Linux server, Windows server but the result is always the same. The QTcpSocket always signals Error -1.

    Do I need to use a Qt based server in order to communicate with a Qt based client? It seems very limiting.

    The QTcpsocket is running in a separate QThread. And the thread is running its own event loop by calling exec(). The strange thing is that the server detects the connection made by the client, but the client always reports the Error -1.

    The run-call for the workerThread

    @void MyWorkerThread::run()
    {
    // Create the objects that shall live and prosper in this brave new thread...
    TcpConnection* poTcpConnection = new TcpConnection();
    ComHandler* poComHandler = new ComHandler(poTcpConnection);
    poComHandler->Connect();

    // Start the event loop for this thread. Note: This is a blocking call and it will block
    // until the exit() function in the TranspodeThread object is called.
    exec();
    }
    @

    The class TcpConnection

    @TcpConnection::TcpConnection(QObject* poParent) :
    QObject(poParent)
    {
    m_poTcpSocket = new QTcpSocket(this);
    }

    bool TcpConnection::Open()
    {
    connect(m_poTcpSocket, SIGNAL(readyRead()), this, SLOT(OnReadyRead()));
    connect(m_poTcpSocket, SIGNAL(connected()), this, SLOT(OnConnected()));
    connect(m_poTcpSocket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(OnError()));
    m_poTcpSocket->connectToHost("10.0.1.23", 10000);
    return true;
    }
    @

    The class ComHandler

    @// Constructor
    ComHandler::ComHandler(Connection* poConnection, QObject* poParent) :
    QObject(poParent)
    {
    m_poConnection = poConnection;
    }

    void ComHandler::Connect()
    {
    if (m_poConnection != NULL)
    {
    connect(m_poConnection, SIGNAL(DataToRead()), this, SLOT(OnDataToRead()));
    m_poConnection->Open();
    }
    }
    @

    Any ideas?

    /Bubbas



  • [quote author="Bubbas" date="1307608077"]Hi,
    Do I need to use a Qt based server in order to communicate with a Qt based client? It seems very limiting.

    [/quote]

    No. It was more a question of limiting the possibilities. I have already connected to other servers.

    [quote author="Bubbas" date="1307608077"]
    @bool TcpConnection::Open()
    {
    connect(m_poTcpSocket, SIGNAL(readyRead()), this, SLOT(OnReadyRead()));
    connect(m_poTcpSocket, SIGNAL(connected()), this, SLOT(OnConnected()));
    connect(m_poTcpSocket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(OnError()));
    m_poTcpSocket->connectToHost("10.0.1.23", 10000);
    return true;
    }
    @
    [/quote]

    You might want to check the returns of connect. Your third connect should fail. The parameter lists should be identical for signals and slots.



  • Hi,

    I moved the QTcpSocket and ComHandler classes to the main gui thread and ...voila┬┤ everything worked. So, I'm doing something wrong when creating the QTCpSocket in the run() function in a workerthread (e.g. a QThread).

    Is it correct that all objects created in the run() function of my worker-thread actually "lives" in that thread?

    Is it even possible to have a QTcpSocket in a separate thread which runs its own event loop?

    // Bubbas



  • [quote author="Bubbas" date="1307616741"]
    Is it correct that all objects created in the run() function of my worker-thread actually "lives" in that thread?

    Is it even possible to have a QTcpSocket in a separate thread which runs its own event loop?
    [/quote]
    This depends what kind of objects you are creating. Static objects are not just living in that thread. They are shared. But "this ":http://developer.qt.nokia.com/doc/qt-4.7/threads-reentrancy.html might give you more detailed answers.


Log in to reply