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. TCP Client static port
Forum Update on Monday, May 27th 2025

TCP Client static port

Scheduled Pinned Locked Moved Solved General and Desktop
tcptcpsocketportsocketsockets
5 Posts 2 Posters 474 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.
  • J Offline
    J Offline
    Joe von Habsburg
    wrote on last edited by
    #1

    Hi, I can connect to TCP server but I want to give spesific port number to client.

    REMOTE_PORT = 5001;
    _client->connectToHost(REMOTE_ADDRESS, REMOTE_PORT);
    

    remote port number is 5001 and I can connect it. But every time my port number is change.
    How can I set spesefic port number to my client ?

    JonBJ 1 Reply Last reply
    0
    • J Joe von Habsburg

      Hi, I can connect to TCP server but I want to give spesific port number to client.

      REMOTE_PORT = 5001;
      _client->connectToHost(REMOTE_ADDRESS, REMOTE_PORT);
      

      remote port number is 5001 and I can connect it. But every time my port number is change.
      How can I set spesefic port number to my client ?

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

      @Joe-von-Habsburg
      Before answering this, why do you want to pick a specific port number at client side or care about what "random" port number TCP will pick for it without you doing so? Usually one does not, server specifies a port, clients connect using some local free port and that's all good. Yes, the client side port number will likely change each time, again that's fine.

      J 1 Reply Last reply
      1
      • JonBJ JonB

        @Joe-von-Habsburg
        Before answering this, why do you want to pick a specific port number at client side or care about what "random" port number TCP will pick for it without you doing so? Usually one does not, server specifies a port, clients connect using some local free port and that's all good. Yes, the client side port number will likely change each time, again that's fine.

        J Offline
        J Offline
        Joe von Habsburg
        wrote on last edited by
        #3

        @JonB because I want to watch message communication on my port

        I create client like that. And I connect to server.

        _client = new QTcpSocket(this);
        _client->connectToHost(REMOTE_ADDRESS, REMOTE_PORT);
        
        JonBJ 1 Reply Last reply
        0
        • J Joe von Habsburg

          @JonB because I want to watch message communication on my port

          I create client like that. And I connect to server.

          _client = new QTcpSocket(this);
          _client->connectToHost(REMOTE_ADDRESS, REMOTE_PORT);
          
          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4

          @Joe-von-Habsburg
          You may be out luck, at least easily! Looks like I answered this in https://forum.qt.io/topic/100018/qtcpsocket-how-to-set-local-port in 2019!

          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.

          There I suggested how it might be achieved with low-level calls, you could try.

          Also perhaps https://gist.github.com/atomass/fadbc33039aa22991cfef1ea65ae9334

          Most people who want to see traffic still manage without knowing client side port number, diagnostics show which server port it is connected to and you just work with that.

          J 1 Reply Last reply
          1
          • JonBJ JonB

            @Joe-von-Habsburg
            You may be out luck, at least easily! Looks like I answered this in https://forum.qt.io/topic/100018/qtcpsocket-how-to-set-local-port in 2019!

            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.

            There I suggested how it might be achieved with low-level calls, you could try.

            Also perhaps https://gist.github.com/atomass/fadbc33039aa22991cfef1ea65ae9334

            Most people who want to see traffic still manage without knowing client side port number, diagnostics show which server port it is connected to and you just work with that.

            J Offline
            J Offline
            Joe von Habsburg
            wrote on last edited by
            #5

            @JonB thank you so much

            1 Reply Last reply
            0
            • J Joe von Habsburg has marked this topic as solved on

            • Login

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