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. QTcpServer listens on a different port than specified
Forum Updated to NodeBB v4.3 + New Features

QTcpServer listens on a different port than specified

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 2 Posters 407 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.
  • I Offline
    I Offline
    Idodoqdo
    wrote on last edited by
    #1

    Hello, I am writing a simple client server application using QTcpServer and doing the following

    Server::Server()
    {
        quint16 port = quint16(2323);
        if (listen(QHostAddress::Any), port) {
            qDebug() << "start" << this->isListening() << this->serverAddress() << this->serverPort();
        } else {
            qDebug() << "error";
        }
    }
    
    

    But it doesn't listen to the specified port and

    this->serverPort();
    

    displays a different port each time. Port 2323 is not busy

    1 Reply Last reply
    0
    • B Offline
      B Offline
      Bonnie
      wrote on last edited by Bonnie
      #2

      Because you've put port in the wrong position.
      You didn't even pass it to the listen funtion, so it will take port=0 as the default parameter, that means choosing a port automatically
      It should be

      listen(QHostAddress::Any, port)
      
      1 Reply Last reply
      1
      • I Offline
        I Offline
        Idodoqdo
        wrote on last edited by
        #3

        I don't quite understand you. I have exactly the same line

        B 1 Reply Last reply
        0
        • I Idodoqdo

          I don't quite understand you. I have exactly the same line

          B Offline
          B Offline
          Bonnie
          wrote on last edited by Bonnie
          #4

          @Idodoqdo No, you do not :) Please look carefully

          I 1 Reply Last reply
          1
          • B Bonnie

            @Idodoqdo No, you do not :) Please look carefully

            I Offline
            I Offline
            Idodoqdo
            wrote on last edited by
            #5

            @Bonnie

            if (listen(QHostAddress::Any), port) {
            
            B 1 Reply Last reply
            0
            • I Idodoqdo

              @Bonnie

              if (listen(QHostAddress::Any), port) {
              
              B Offline
              B Offline
              Bonnie
              wrote on last edited by Bonnie
              #6

              @Idodoqdo Still have not found your mistake :)

              if (listen(QHostAddress::Any), port) {

              Wrong right bracket position, so wrong port position...

              1 Reply Last reply
              0
              • I Offline
                I Offline
                Idodoqdo
                wrote on last edited by
                #7

                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