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

QTcpSocket Returning Error -1

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 1.2k 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.
  • P Offline
    P Offline
    plafratt
    wrote on last edited by
    #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;
      QObject::connect(tcpSocket,SIGNAL(readyRead()),
                  &h,SLOT(commandRead()));
    
      tcpSocket->connectToHost("localhost",10000);
    
      tcpSocket->waitForConnected(5000);
    
      // returns -1
      cout<<tcpSocket->error()<<"\n";
      return 0;
    

    }
    @

    1 Reply Last reply
    0
    • B Offline
      B Offline
      Bruno13
      wrote on last edited by
      #2

      Didn't you forget to instantiate a QCoreApplication object?

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

      1 Reply Last reply
      0
      • P Offline
        P Offline
        plafratt
        wrote on last edited by
        #3

        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.

        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