QTcpSocket Returning Error -1

  • I am trying to create a very simple TCP connection using QTcpSocket and QTcpServer. The server appears to be receiving the connection fine. The client, though, is returning error -1. Does anyone know what might be causing this? The source code for the client is below.

    I am able to write a working TCP client-server program program in Python that connects over port 10000. It works on the same machine on which this Qt program is failing.

    I can provide the server code if needed. But, like I said, the server appears to be working. It just listens for a connection, and when the newConnection signal comes in, it sends some data. The client fails whether or not I send any data.

    Any help is appreciated.

    @int main( int argc, char * argv[] ) {
    handler h;

      QTcpSocket *tcpSocket = new QTcpSocket;
      // returns -1
      return 0;


  • Didn't you forget to instantiate a QCoreApplication object?

    I'll start main function with:
    @QCoreApplication app(argc, argv);@

  • Bruno13:

    Thank you for the reply. You are right, I had forgotten to do that. I hadn't given much attention to setting up and running the main loop, because I thought that I needed to deal with the error code that was being returned by the socket.

    But it appears that if I go ahead and run app.exec() and ignore the error code, it can receive data just fine. I should've just ignored the error code, I guess. According to the QT documentation, though, this code means that "An unidentified error occurred."

    Thank you again.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.