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. QUdpSocket->state() is always QAbstractSocket::ConnectedState

QUdpSocket->state() is always QAbstractSocket::ConnectedState

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 4 Posters 1.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.
  • A Offline
    A Offline
    alxdef
    wrote on last edited by
    #1

    Hi there!
    I have a piece of code that trying to connect some device via UDP.

    QUdpSocket* udp = new QUdpSocket();
    udp->connectToHost(IP, PORT, QIODeviceBase::ReadWrite);
    

    It doesn't matter device is present or not in LAN and switched it on or not, the result of

    if (udp->state() == QAbstractSocket::ConnectedState)
    

    is always true.

    Are there other methods to obtain connection state more precisely?

    (Qt 6.1.1 with Clang based on MinGW 6.1.1 x64, Windows 10 Pro x64)

    1 Reply Last reply
    0
    • qtprogrammer123Q Offline
      qtprogrammer123Q Offline
      qtprogrammer123
      wrote on last edited by
      #2

      @alxdef said in QUdpSocket->state() is always QAbstractSocket::ConnectedState:

      QAbstractSocket::ConnectedState

      You forgot about socket bind

      Mam moc jak Harry Potter, w zębach mogę przenieść hotel.

      A 1 Reply Last reply
      0
      • qtprogrammer123Q qtprogrammer123

        @alxdef said in QUdpSocket->state() is always QAbstractSocket::ConnectedState:

        QAbstractSocket::ConnectedState

        You forgot about socket bind

        A Offline
        A Offline
        alxdef
        wrote on last edited by
        #3

        @qtprogrammer123, bind() is for servers. I tried to use it but the result is same. And my code is more client than server.

        JonBJ 1 Reply Last reply
        0
        • A alxdef

          @qtprogrammer123, bind() is for servers. I tried to use it but the result is same. And my code is more client than server.

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4

          @alxdef
          It does indeed not sound right if it always reports "connected" and you are sure it cannot be!

          Maybe this is a Qt 6 issue. I might also try a rubbish IP/PORT to see if that still reports connection.

          Can only suggest you slot onto QAbstractSocket::stateChanged(QAbstractSocket::SocketState socketState) to report what states it claims to go through, and see if that reveals anything. If you haven't already, you should also catch QAbstractSocket::errorOccurred(QAbstractSocket::SocketError socketError) to see if any error occurs.

          1 Reply Last reply
          0
          • qtprogrammer123Q Offline
            qtprogrammer123Q Offline
            qtprogrammer123
            wrote on last edited by
            #5

            Just try add udp->bind(QHostAddress::LocalHost, 1234) before connection

            Mam moc jak Harry Potter, w zębach mogę przenieść hotel.

            1 Reply Last reply
            0
            • qtprogrammer123Q Offline
              qtprogrammer123Q Offline
              qtprogrammer123
              wrote on last edited by qtprogrammer123
              #6

              ouuu if host close connection, you dont see that. Sory its early ;] You should catch errorOccurred signal, it contains informations about host problems

              Mam moc jak Harry Potter, w zębach mogę przenieść hotel.

              1 Reply Last reply
              0
              • Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by
                #7

                I don't see what's wrong here. QUdpSocket is stateless so as soon as the socket is opened it's connected - what else should it report?

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                qtprogrammer123Q 1 Reply Last reply
                0
                • Christian EhrlicherC Christian Ehrlicher

                  I don't see what's wrong here. QUdpSocket is stateless so as soon as the socket is opened it's connected - what else should it report?

                  qtprogrammer123Q Offline
                  qtprogrammer123Q Offline
                  qtprogrammer123
                  wrote on last edited by qtprogrammer123
                  #8

                  @Christian-Ehrlicher connectToHost set state - connected, udp is stateless protocol, state is parent method.

                  Hmm it can be set manualy so try, i write in notepad, so some errors may occure :D

                  connect(udp, &QUDPSocket::errorOccurred, onErr);
                  
                  void onErr(){
                      
                      udp.setSocketState(QAbstractSocket::UnconnectedState);
                      
                  }
                  
                  

                  Mam moc jak Harry Potter, w zębach mogę przenieść hotel.

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    alxdef
                    wrote on last edited by
                    #9

                    So I decided for me such algorithm:

                    1. No way to check connection state because it's useless.
                    2. Try to work with device right after connectToHost and decide what to do next - set self error state or continue my job.
                    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