Results 1 to 5 of 5

Thread: Problem when excluding clients from connecting to QTcpServer

  1. #1
    Join Date
    Aug 2006
    Posts
    44
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Problem when excluding clients from connecting to QTcpServer

    I wanted to exclude certain clients from connecting to a QTcpServer. In my server, I use the following code:

    Qt Code:
    1. void Server::incomingConnection(int socketDescriptor)
    2. {
    3. socket_ = new QTcpSocket(this);
    4.  
    5. //Only perform a host look-up, do NOT establish a connection
    6. if (!socket_->setSocketDescriptor(socketDescriptor, QAbstractSocket::HostLookupState))
    7. {
    8. emit error(socket_->error());
    9. return;
    10. }
    11.  
    12. QString name = socket_->peerName();
    13.  
    14. if (socket_->peerName() == QString("A certain hostname"))
    15. {
    16. emit finished();
    17. socket_->close();
    18. return;
    19. }
    20.  
    21. //Close socket anyway to be able to establish a connection
    22. socket_->close();
    23.  
    24. if (!socket_->setSocketDescriptor(socketDescriptor))
    25. {
    26. emit error(socket_->error());
    27. return;
    28. }
    29. }
    To copy to clipboard, switch view to plain text mode 

    On the client side:

    Qt Code:
    1. void Client::establishConnection()
    2. {
    3. foreach(QString server, servers_)
    4. {
    5. socket_->connectToHost(server, port_);
    6.  
    7. if (socket_->waitForConnected(100))
    8. {
    9. connect(socket_, SIGNAL(readyRead()), this, SLOT(readyRead()));
    10.  
    11. emit connectionEstablished(server);
    12.  
    13. break;
    14. }
    15. }
    To copy to clipboard, switch view to plain text mode 

    Now it seems that waitForConnected returns true when only performing the host look-up. I do not want a connection to be established at the client side, if there is no real connection. This does not seem to be the behavior one would expect.

    I can off course check of the socket is open afterwards, but shouldn't waitForConnected() only return true when a complete connection has been established?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,014 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Problem when excluding clients from connecting to QTcpServer

    You should establish a connection and close it immediately. The connection is already established when incomingConnection() is called. The way you are trying to validate the client is... weird You can't create and close sockets as you want and expect the connection not to suffer on that.

  3. #3
    Join Date
    Aug 2006
    Posts
    44
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem when excluding clients from connecting to QTcpServer

    If so, then why is it possible to set the descriptor with the state QAbstractSocket::HostLookupState? I thought that was the perfect way to prevent a connection from being established .

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,014 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Problem when excluding clients from connecting to QTcpServer

    Because you might set an arbitrary descriptor on a socket, so Qt doesn't know what state it is in. But this exact descriptor is in the connecting state when you get it (so you can't make a step back).

  5. The following user says thank you to wysota for this useful post:

    Raistlin (23rd May 2008)

  6. #5
    Join Date
    Aug 2006
    Posts
    44
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem when excluding clients from connecting to QTcpServer

    Ok, good to know .

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.