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 can not bind to specified net card
Forum Updated to NodeBB v4.3 + New Features

QTcpSocket can not bind to specified net card

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 3 Posters 582 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.
  • C Offline
    C Offline
    coderKnight
    wrote on last edited by
    #1

    I am using QT 5.9 in a embeded board that has to net card. When I use QTcpSocket 's "bind" function to bind the net card by IP addr ,the QTcpSocket dose not send data using the net card specified,instead ,it send data rely on the route,I dont know why. The board is running linux3.10,is it possible the linux reason?Please help me,thanks

    1 Reply Last reply
    0
    • Kent-DorfmanK Offline
      Kent-DorfmanK Offline
      Kent-Dorfman
      wrote on last edited by
      #2

      data is always sent based on the routing table. bind() binds for listenning, not for sending.

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

        If you want your socket bind to a specific interface you should look for SO_BINDTODEVICE - Qt does not provide a high-level api for this (for good reasons when you search enough)

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

        C 1 Reply Last reply
        2
        • Christian EhrlicherC Christian Ehrlicher

          If you want your socket bind to a specific interface you should look for SO_BINDTODEVICE - Qt does not provide a high-level api for this (for good reasons when you search enough)

          C Offline
          C Offline
          coderKnight
          wrote on last edited by
          #4

          @Christian-Ehrlicher thank for your reply. From your reply, I realized I have a wrong way.But Qt seems to not support this option,SO,I create socket alone and the use "setSocketDescriptor" function set the socket to QTcpSocket.At start it's worked,but sometimes Qt report a problem ,just like :
          QNativeSocketEngine::bytesAvailable() was called in QAbstractSocket::UnconnectedState
          QNativeSocketEngine::read() was called not in QAbstractSocket::ConnectedState or QAbstractSocket::BoundState
          but I don't know what I have done make this happen

          1 Reply Last reply
          0
          • Kent-DorfmanK Kent-Dorfman

            data is always sent based on the routing table. bind() binds for listenning, not for sending.

            C Offline
            C Offline
            coderKnight
            wrote on last edited by
            #5

            @Kent-Dorfman I create socket alone and the use "setSocketDescriptor" function set the socket to QTcpSocket.At start it's worked,but sometimes Qt report a problem ,just like :
            QNativeSocketEngine::bytesAvailable() was called in QAbstractSocket::UnconnectedState
            QNativeSocketEngine::read() was called not in QAbstractSocket::ConnectedState or QAbstractSocket::BoundState
            but I don't know what I have done make this happen

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

              Why not simply use QTcpSocket::socketDescriptor() and call setsockopt there?

              Your problem is that you don't call setSocketDescriptor() correctly - you either have to connect by low-level ai or pass the correct second argument to setSocketDescriptor()

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

              C 1 Reply Last reply
              1
              • Christian EhrlicherC Christian Ehrlicher

                Why not simply use QTcpSocket::socketDescriptor() and call setsockopt there?

                Your problem is that you don't call setSocketDescriptor() correctly - you either have to connect by low-level ai or pass the correct second argument to setSocketDescriptor()

                C Offline
                C Offline
                coderKnight
                wrote on last edited by
                #7

                @Christian-Ehrlicher I have sovled the problem by your suggestion,thanks a lot

                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