QTcpServer::serverAddress() returning null address



  • 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.



  • 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() ?



  • 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
    @


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.