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 Client QT 5.12 not connecting
Forum Updated to NodeBB v4.3 + New Features

QTcpSocket Client QT 5.12 not connecting

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 736 Views
  • 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.
  • D Offline
    D Offline
    Daddedebad
    wrote on last edited by Daddedebad
    #1

    Hey, some background info: I use the QT extension in visual studio 2022, QT version 5.12.2. I want to send a simple "J" character to a server from my client using TCP. I got this to work using a different TCP socket code that is not part of the QT framework. So the communication and setup outside the code works but now I need it also to work using QT's framework, QTcpSocket.

    I have tested different versions that I found on the internet like the fortune client example, so far I have not gotten any of them to work. I am not too experienced with TCP coding so to keep it short, the simplest way to connect to server at ip=192.168.3.154 and port=4035 and send "J" as I understand could look like this:

        QTcpSocket *socket = new QTcpSocket();
        socket->connectToHost("192.168.3.154", 4035);
        // we need to wait...
        if (!socket->waitForConnected(5000))
        {
            qDebug() << "Error: " << socket->error() << "State: " << socket->state();
        }
        socket->write("J");
    
    

    But in the code above I get a timeout so it seems I cannot make the connection this way. Am I misusing the QTcpSocket object?

    Edit: Added some more error handling. The message from the qDebug() is: Error: QAbstractSocket::SocketTimeoutError State: QAbstractSocket::UnconnectedState

    jsulmJ 1 Reply Last reply
    0
    • D Offline
      D Offline
      Daddedebad
      wrote on last edited by
      #4

      I found the issue, of course I had a digit wrong on the port... The connection seems to succeed now, I get a different error elsewhere but for the purpose of this thread it can be considered solved.

      1 Reply Last reply
      1
      • D Daddedebad

        Hey, some background info: I use the QT extension in visual studio 2022, QT version 5.12.2. I want to send a simple "J" character to a server from my client using TCP. I got this to work using a different TCP socket code that is not part of the QT framework. So the communication and setup outside the code works but now I need it also to work using QT's framework, QTcpSocket.

        I have tested different versions that I found on the internet like the fortune client example, so far I have not gotten any of them to work. I am not too experienced with TCP coding so to keep it short, the simplest way to connect to server at ip=192.168.3.154 and port=4035 and send "J" as I understand could look like this:

            QTcpSocket *socket = new QTcpSocket();
            socket->connectToHost("192.168.3.154", 4035);
            // we need to wait...
            if (!socket->waitForConnected(5000))
            {
                qDebug() << "Error: " << socket->error() << "State: " << socket->state();
            }
            socket->write("J");
        
        

        But in the code above I get a timeout so it seems I cannot make the connection this way. Am I misusing the QTcpSocket object?

        Edit: Added some more error handling. The message from the qDebug() is: Error: QAbstractSocket::SocketTimeoutError State: QAbstractSocket::UnconnectedState

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #2

        @Daddedebad Please add error handling to your code: https://doc.qt.io/qt-6/qabstractsocket.html#error

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        D 1 Reply Last reply
        0
        • jsulmJ jsulm

          @Daddedebad Please add error handling to your code: https://doc.qt.io/qt-6/qabstractsocket.html#error

          D Offline
          D Offline
          Daddedebad
          wrote on last edited by
          #3

          @jsulm I updated the code now and edited in the return from the error.

          1 Reply Last reply
          0
          • D Offline
            D Offline
            Daddedebad
            wrote on last edited by
            #4

            I found the issue, of course I had a digit wrong on the port... The connection seems to succeed now, I get a different error elsewhere but for the purpose of this thread it can be considered solved.

            1 Reply Last reply
            1

            • Login

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