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. How to use QTcpSocket stays in ConnectingState after reconnection.

How to use QTcpSocket stays in ConnectingState after reconnection.

Scheduled Pinned Locked Moved Unsolved General and Desktop
11 Posts 3 Posters 1.8k 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.
  • S Offline
    S Offline
    swansorter
    wrote on last edited by
    #1

    Hai ,
    I am doing tcp client application and in my application QTcpSocket stays in ConnectingState after reconnection.
    How use "setsockopt " "SO_REUSEADDR" in qt

    JonBJ 1 Reply Last reply
    0
    • S swansorter

      Hai ,
      I am doing tcp client application and in my application QTcpSocket stays in ConnectingState after reconnection.
      How use "setsockopt " "SO_REUSEADDR" in qt

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

      @swansorter
      Use setsockopt() on the native socket descriptor returned by qintptr QAbstractSocket::socketDescriptor() const.

      For this case of SO_REUSEADDR it is a server-side flag (not client), and you should instead follow the advice from @Christian-Ehrlicher below.

      S 1 Reply Last reply
      0
      • JonBJ JonB

        @swansorter
        Use setsockopt() on the native socket descriptor returned by qintptr QAbstractSocket::socketDescriptor() const.

        For this case of SO_REUSEADDR it is a server-side flag (not client), and you should instead follow the advice from @Christian-Ehrlicher below.

        S Offline
        S Offline
        swansorter
        wrote on last edited by
        #3

        @JonB Sir used that one
        data_socket.setSocketOption(QTcpSocket::ReuseAddressHint,1);
        it is showing
        : error: no matching function for call to ‘QTcpSocket::setSocketOption(QAbstractSocket::BindFlag, int)’
        data_socket.setSocketOption(QTcpSocket::ReuseAddressHint,1);
        ^

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

          QTcpSocket::ReuseAddressHint is as per documentation a QAbstractSocket::BindFlag so it can't be use by the function setSocketOption() since this needs a SocketOption flag.
          If you need this flag you have to pass it to bind() as written in the documentation to this flag.

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

          S 1 Reply Last reply
          2
          • Christian EhrlicherC Christian Ehrlicher

            QTcpSocket::ReuseAddressHint is as per documentation a QAbstractSocket::BindFlag so it can't be use by the function setSocketOption() since this needs a SocketOption flag.
            If you need this flag you have to pass it to bind() as written in the documentation to this flag.

            S Offline
            S Offline
            swansorter
            wrote on last edited by
            #5

            @Christian-Ehrlicher I am developing client side application.can i use bind in client application

            Christian EhrlicherC 1 Reply Last reply
            0
            • S swansorter

              @Christian-Ehrlicher I am developing client side application.can i use bind in client application

              Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @swansorter said in How to use QTcpSocket stays in ConnectingState after reconnection.:

              can i use bind in client application

              Why should you need it there?

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

              S 1 Reply Last reply
              1
              • Christian EhrlicherC Christian Ehrlicher

                @swansorter said in How to use QTcpSocket stays in ConnectingState after reconnection.:

                can i use bind in client application

                Why should you need it there?

                S Offline
                S Offline
                swansorter
                wrote on last edited by swansorter
                #7

                @Christian-Ehrlicher As i mentioned earlier when i try to reconnect to the same port its showing connectingstate

                JonBJ 1 Reply Last reply
                0
                • S swansorter

                  @Christian-Ehrlicher As i mentioned earlier when i try to reconnect to the same port its showing connectingstate

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

                  @swansorter
                  SO_REUSEADDR should only be set on the server side of a socket. It allows multiple clients to bind to the same server address. Your client side connections should not need this, instead they should be allowed to use a "random" client-side-generated address.

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

                    Don't see what this has to do with SO_REUSEADDR... please read the docs about this option - it's not a client option at all.

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

                    1 Reply Last reply
                    0
                    • JonBJ JonB

                      @swansorter
                      SO_REUSEADDR should only be set on the server side of a socket. It allows multiple clients to bind to the same server address. Your client side connections should not need this, instead they should be allowed to use a "random" client-side-generated address.

                      S Offline
                      S Offline
                      swansorter
                      wrote on last edited by
                      #10

                      @JonB @Christian-Ehrlicher then why i am getting connectingstate after reconnection,is ther any solution for this ?

                      JonBJ 1 Reply Last reply
                      0
                      • S swansorter

                        @JonB @Christian-Ehrlicher then why i am getting connectingstate after reconnection,is ther any solution for this ?

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

                        @swansorter
                        If you have not set QTcpSocket::ReuseAddressHint at the server side, prior to bind(), the server will not accept a second connection from any client (on the same client IP address), while a client is connected to the server. (There may be a period of time after "reported disconnection" when the client is still connected so the server cannot reuse the address.)

                        So.... have you set this at the server side, in the correct place?

                        Otherwise you may show your client connection code to verify you are not specifying a client-side port.

                        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