Results 1 to 3 of 3

Thread: waitForNewConnections doesn't block

  1. #1
    Join Date
    Mar 2007
    Posts
    8
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default waitForNewConnections doesn't block

    Hi folks,

    I've some problems with the TcpServer::waitForNewConnections(int, bool&). Searching the archives or the net didn't help, so I hope somebody here can help me...

    I made a small test program:
    Qt Code:
    1. #include <QTcpServer>
    2.  
    3. int main ()
    4. {
    5. bool lConAvailable = true;
    6. QTcpServer lServer;
    7. QTcpSocket lSocket;
    8.  
    9. lSocket.connectToHost("172.16.140.32", 5000);
    10. lServer.setSocketDescriptor(lSocket.socketDescriptor());
    11.  
    12.  
    13. while (true) {
    14. if (lServer.waitForNewConnection(1000, &lConAvailable)) {
    15. printf("\na Connection is available\n");
    16. }
    17. else {
    18. printf(".");
    19. fflush(stdout);
    20. }
    21. }
    22. }
    To copy to clipboard, switch view to plain text mode 
    I expect this to block in the waitForNewConnection call for 1 second. As nothing is available there should be a print of a dot every seconds. This is not what happens: the dots are printed like crazy.

    What I am doing wrong?

    My .pro file looks like:
    Qt Code:
    1. TEMPLATE = app
    2. CONFIG += qt
    3. TARGET = server
    4. QT += network thread
    5. SOURCES += main.cpp
    To copy to clipboard, switch view to plain text mode 
    Last edited by KoosKoets; 27th March 2007 at 16:49.

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: waitForNewConnections doesn't block

    I assume that the socket is looking at hasPendingConnections(), and waits the timeout period only if there are pending connections, if not, there is no need to wait, and it returns immidietly.
    This is only a guess.

    And plese use the code tags for code.

  3. #3
    Join Date
    Aug 2006
    Location
    Switzerland
    Posts
    52
    Thanked 13 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: waitForNewConnections doesn't block

    Quote Originally Posted by KoosKoets View Post
    Qt Code:
    1. // cut
    2. lSocket.connectToHost("172.16.140.32", 5000);
    3. lServer.setSocketDescriptor(lSocket.socketDescriptor());
    4.  
    5. while (true) {
    6. if (lServer.waitForNewConnection(1000, &lConAvailable)) {
    7. printf("\na Connection is available\n");
    8. }
    9. else {
    10. printf(".");
    11. fflush(stdout);
    12. }
    13. }
    14. }
    To copy to clipboard, switch view to plain text mode 
    [...]
    What I am doing wrong?
    Hm... What to begin with . Why do you connect to and then use the socket in server? Such socket is in a connected state and server require socket in a listening state to be able to accept incoming connections. You should use something like this:
    Qt Code:
    1. if (!lServer.listen(QHostAddress::LocalHost, 5000)) {
    2. printf("Failed to bind socket");
    3. exit(1);
    4. }
    5.  
    6. while (true) {
    7. if (lServer.waitForNewConnection(1000, &lConAvailable)) {
    8. printf("\na Connection is available\n");
    9. }
    10. else {
    11. printf(".");
    12. fflush(stdout);
    13. }
    14. }
    To copy to clipboard, switch view to plain text mode 
    The Wheel weaves as the Wheel wills.

Similar Threads

  1. QSyntaxHighlighter and faked currentParagraph()
    By mcostalba in forum Qt Programming
    Replies: 4
    Last Post: 11th December 2008, 00:36
  2. Block close dialog
    By Mrdata in forum Newbie
    Replies: 2
    Last Post: 12th March 2007, 15:39
  3. Cryptography : is it me ???
    By fullmetalcoder in forum General Discussion
    Replies: 49
    Last Post: 2nd March 2007, 08:07
  4. Replies: 5
    Last Post: 23rd February 2007, 10:23
  5. QCoreApplication!!!!
    By fellobo in forum Qt Programming
    Replies: 6
    Last Post: 17th January 2007, 00:56

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
  •  
Qt is a trademark of The Qt Company.