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, how to set local port
Qt 6.11 is out! See what's new in the release blog

QTcpSocket, how to set local port

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 3 Posters 3.0k 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
    Svirac
    wrote on last edited by
    #1

    Hi guys,

    I am working on TCPIP Client side.
    The requirement is to connect to Server port: 8302 … but Client should use port: 8267.
    I am aware that essentially server can establish connection only by listening his 8302 (regardless of client port number)… but in this case they require specific client port number.
    For test purposes I have server application which accepts any client port.

    Please see attached my source code, I am using using Qt 4.6.1.
    I am assuming that setting local port should be done inside tcpclient.cpp, line 43 (slot_connectToServer() function).
    Cannot access function bind or setLocalPort since they are private.

    I am not advanced user, I apologize in advance if question is stupid :/

    Any help is greatly appreciated…
    [0_1550838726717_tcp_client_v0.zip](Uploading 100%)

    aha_1980A JonBJ 2 Replies Last reply
    0
    • S Svirac

      Hi guys,

      I am working on TCPIP Client side.
      The requirement is to connect to Server port: 8302 … but Client should use port: 8267.
      I am aware that essentially server can establish connection only by listening his 8302 (regardless of client port number)… but in this case they require specific client port number.
      For test purposes I have server application which accepts any client port.

      Please see attached my source code, I am using using Qt 4.6.1.
      I am assuming that setting local port should be done inside tcpclient.cpp, line 43 (slot_connectToServer() function).
      Cannot access function bind or setLocalPort since they are private.

      I am not advanced user, I apologize in advance if question is stupid :/

      Any help is greatly appreciated…
      [0_1550838726717_tcp_client_v0.zip](Uploading 100%)

      aha_1980A Offline
      aha_1980A Offline
      aha_1980
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Svirac

      I was not aware you can set the "outgoing" port for a TCP connection. The operating system uses one of the free ports for this purpose.

      Reading https://idea.popcount.org/2014-04-03-bind-before-connect it should be possible to do so at least on Linux. I have not searched for other operating systems, and I don't think there is something ready for that in Qt.

      So if you get this to work, you will most likely have to use native API.

      Regards

      Qt has to stay free or it will die.

      1 Reply Last reply
      0
      • S Svirac

        Hi guys,

        I am working on TCPIP Client side.
        The requirement is to connect to Server port: 8302 … but Client should use port: 8267.
        I am aware that essentially server can establish connection only by listening his 8302 (regardless of client port number)… but in this case they require specific client port number.
        For test purposes I have server application which accepts any client port.

        Please see attached my source code, I am using using Qt 4.6.1.
        I am assuming that setting local port should be done inside tcpclient.cpp, line 43 (slot_connectToServer() function).
        Cannot access function bind or setLocalPort since they are private.

        I am not advanced user, I apologize in advance if question is stupid :/

        Any help is greatly appreciated…
        [0_1550838726717_tcp_client_v0.zip](Uploading 100%)

        JonBJ Online
        JonBJ Online
        JonB
        wrote on last edited by JonB
        #3

        @Svirac
        Have a read through https://forum.qt.io/topic/32596/set-a-local-port-to-a-qtcpsocket

        Although the OP experienced some problems, I think that by the end he got it working from Qt?

        From 2008(!), https://www.qtcentre.org/threads/11082-forcing-local-port-number-from-TcpSocket

        This is currently not possible using the Qt API, no.
        What you'd have to do if you really need to do this is to do the local
        binding of a socket descriptor yourself and then set the socket
        descriptor on the QTcpSocket afterwards using the setSocketDescriptor()
        function.
        We do plan to introduce this functionality at a later point though.

        Of course, they may have introduced the functionality in the last ten years... ;-)

        1 Reply Last reply
        0
        • S Offline
          S Offline
          Svirac
          wrote on last edited by
          #4

          Hi,

          So is it possible to use bind or set local port on some latter Qt versions?

          JonBJ 1 Reply Last reply
          0
          • S Svirac

            Hi,

            So is it possible to use bind or set local port on some latter Qt versions?

            JonBJ Online
            JonBJ Online
            JonB
            wrote on last edited by
            #5

            @Svirac
            Is this question addressed to me?

            If it looks like further support was never added, I am suggesting those links are still valid and show you how you may achieve it from Qt.

            1 Reply Last reply
            0
            • S Offline
              S Offline
              Svirac
              wrote on last edited by
              #6

              OK I will try those links...
              Appreciate your help guys...

              If you find some more info please drop me a note....

              1 Reply Last reply
              0
              • JonBJ JonB referenced this topic on

              • Login

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