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::serverAddress() returning null address

QTcpServer::serverAddress() returning null address

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 2.5k 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.
  • A Offline
    A Offline
    allanba
    wrote on last edited by
    #1

    Hello,

    I have just got a copy of Qt5.1 and configure it for android, just for mere testing purposes I wrote a a widget with three buttons to start,stop and restart a QTcpServer. The server starts correctly and I am then able to handle connections from my android device (a physical device). I then change the ip address of the device, go back to my app and restart the server, the server actually restarts and it's able to handle connections with the old address but not with the new one. Whenever the server starts (or restarts) the code will output a line to the console similar to

    @qDebug() << "Server started at " << server.serverAddress();@

    I would expect the program to print "Server started at QHostAddress(192.168.1.xxx)", it instead prints "Server started at QHostAddress( 0.0.0.0 )" which according the the qt documentation, means that the server is not listening.

    Does anybody know what is wrong? is it something I am doing incorrectly or something to with qt itself?

    Cheers.

    1 Reply Last reply
    0
    • C Offline
      C Offline
      ChrisW67
      wrote on last edited by
      #2

      bq. I would expect the program to print “Server started at QHostAddress(192.168.1.xxx)”, it instead prints “Server started at QHostAddress( 0.0.0.0 )” which according the the qt documentation, means that the server is not listening.

      No, the documentation says that if it is not listening it returns QHostAddress::Null. QHostAddress("0.0.0.0") is QHostAddress::Any... it is listening on all interfaces.

      How are you calling QTcpServer::listen() ?

      1 Reply Last reply
      0
      • A Offline
        A Offline
        allanba
        wrote on last edited by
        #3

        Oops, sorry for that. I call it like
        //Constructor
        @server = new QTcpserver(); // server is a member variable of my class

        ...

        //Start boolean method returns true on success
        if(server->listen(QHostAddress::any,1234))
        {
        acceptConnections = true; //member variable, stops new connections to be handled
        qDebug() << "Server started at " << server->serverAddress();
        return true;
        }
        else
        {
        qDebug() << "Could not start server";
        return false;
        }
        ...

        //Stop
        acceptConnections = false; //member variable, stops new connections to be handled

        while(server->hasPendingConnections())
        {
        QTcpSocket *sock = server->nextPendingConnection();
        sock->close();
        }

        server->close();

        //restart method is start called after stop
        @

        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