Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. QTcpSocket signals Error -1
Forum Updated to NodeBB v4.3 + New Features

QTcpSocket signals Error -1

Scheduled Pinned Locked Moved Mobile and Embedded
6 Posts 2 Posters 5.3k Views 1 Watching
  • 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.
  • B Offline
    B Offline
    Bubbas
    wrote on last edited by
    #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

    1 Reply Last reply
    0
    • K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

      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.

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      0
      • B Offline
        B Offline
        Bubbas
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        • K Offline
          K Offline
          koahnig
          wrote on last edited by
          #4

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

          Vote the answer(s) that helped you to solve your issue(s)

          1 Reply Last reply
          0
          • B Offline
            B Offline
            Bubbas
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            • K Offline
              K Offline
              koahnig
              wrote on last edited by
              #6

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

              Vote the answer(s) that helped you to solve your issue(s)

              1 Reply Last reply
              0

              • Login

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