June 9, 2011

Bubbas Bubbas
Lab Rat
14 posts

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

5 replies

June 9, 2011

koahnig koahnig
Dinosaur Breeder
2929 posts

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.

June 9, 2011

Bubbas Bubbas
Lab Rat
14 posts

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
———————————————————————-

  1. void MyWorkerThread::run()
  2. {
  3.    // Create the objects that shall live and prosper in this brave new thread...
  4.    TcpConnection* poTcpConnection = new TcpConnection();
  5.    ComHandler* poComHandler = new ComHandler(poTcpConnection);
  6.    poComHandler->Connect();
  7.  
  8.    // Start the event loop for this thread. Note: This is a blocking call and it will block
  9.    // until the exit() function in the TranspodeThread object is called.
  10.    exec();
  11. }

The class TcpConnection
——————————————————

  1. TcpConnection::TcpConnection(QObject* poParent) :
  2.  QObject(poParent)
  3. {
  4.        m_poTcpSocket = new QTcpSocket(this);
  5. }
  6.  
  7. bool TcpConnection::Open()
  8. {
  9.    connect(m_poTcpSocket, SIGNAL(readyRead()), this, SLOT(OnReadyRead()));
  10.    connect(m_poTcpSocket, SIGNAL(connected()), this, SLOT(OnConnected()));
  11.    connect(m_poTcpSocket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(OnError()));
  12.    m_poTcpSocket->connectToHost("10.0.1.23", 10000);
  13.   return true;
  14. }

The class ComHandler
——————————————————-

  1. // Constructor
  2. ComHandler::ComHandler(Connection* poConnection, QObject* poParent) :
  3.                              QObject(poParent)
  4. {
  5.      m_poConnection = poConnection;
  6. }
  7.  
  8. void ComHandler::Connect()
  9. {
  10.     if (m_poConnection != NULL)
  11.     {
  12.          connect(m_poConnection, SIGNAL(DataToRead()), this, SLOT(OnDataToRead()));
  13.          m_poConnection->Open();
  14.     }
  15. }

Any ideas?

/Bubbas

June 9, 2011

koahnig koahnig
Dinosaur Breeder
2929 posts

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

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

Bubbas wrote:
  1. bool TcpConnection::Open()
  2. {
  3.    connect(m_poTcpSocket, SIGNAL(readyRead()), this, SLOT(OnReadyRead()));
  4.    connect(m_poTcpSocket, SIGNAL(connected()), this, SLOT(OnConnected()));
  5.    connect(m_poTcpSocket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(OnError()));
  6.    m_poTcpSocket->connectToHost("10.0.1.23", 10000);
  7.   return true;
  8. }

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

June 9, 2011

Bubbas Bubbas
Lab Rat
14 posts

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

June 9, 2011

koahnig koahnig
Dinosaur Breeder
2929 posts

Bubbas wrote:

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?


This depends what kind of objects you are creating. Static objects are not just living in that thread. They are shared. But this [developer.qt.nokia.com] might give you more detailed answers.

 
  ‹‹ Qt runtime error on Freescale / same code works on other systems      How to build native rss with project ››

You must log in to post a reply. Not a member yet? Register here!