Results 1 to 6 of 6

Thread: QTcpServer - how to refuse incomming connection

  1. #1
    Join Date
    Jan 2014
    Posts
    76
    Thanks
    17
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default QTcpServer - how to refuse incomming connection

    Hi,

    I write simply tcpserver and I have problem with incomming connections...

    Qt Code:
    1. void Server::incomingConnection(int socketId)
    2. {
    3. qDebug()<< "incomming connection";
    4.  
    5. if( mClients.size() < 10 )
    6. addClient(socketId);
    7. else
    8. qDebug()<< "server is overloaded";
    9. }
    To copy to clipboard, switch view to plain text mode 

    so server accept only 10 connections but next clients can connect to server ( its socket state is QAbstractSocket::ConnectedState ).

    How I can refuse next connection? I would like advise new clients, that server is overloaded and maybe prepare some timeout for waiting.

    How I can do that?
    Thanks,

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QTcpServer - how to refuse incomming connection

    Perhaps call QTcpServer::setMaxPendingConnections() to the number of free slots initially (i.e. 10), reducing the number every time you accept a connect, and increasing the number every time you close a connection.

  3. #3
    Join Date
    Jan 2014
    Posts
    76
    Thanks
    17
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QTcpServer - how to refuse incomming connection

    Yes, this blocks new incoming connection on server, but new clients still gets connected state and connected signal, this is strange.

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QTcpServer - how to refuse incomming connection

    You can delete the QTcpServer object and thus stop if from listening.

    Cheers,
    _

  5. #5
    Join Date
    Jan 2014
    Posts
    76
    Thanks
    17
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QTcpServer - how to refuse incomming connection

    No, I can not delete QTcpServer, because I must operate with other accepted connections.
    I want only say to the next incomming connection, that they should wait because server is overloaded, maybe something like this:

    Qt Code:
    1. void Server::incomingConnection(int socketId)
    2. {
    3.  
    4. if( canAcceptConnection() )
    5. addClient(socketId);
    6. else {
    7. QTcpSocket *socket = new QTcpSocket(this);
    8. socket->setSocketDescriptor(socketId);
    9. socket->write("Server is overloaded, try again later...\r\n");
    10. socket->close();
    11. socket->deleteLater();
    12. }
    13.  
    14. }
    To copy to clipboard, switch view to plain text mode 

    that is a good idea?

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QTcpServer - how to refuse incomming connection

    Quote Originally Posted by atomic View Post
    No, I can not delete QTcpServer, because I must operate with other accepted connections.
    The QTcpServer is not involved in already accepted connections, so if you reparent the QTcpSockets to a different object they won't be affected by the server's deletion.

    Quote Originally Posted by atomic View Post
    I want only say to the next incomming connection, that they should wait because server is overloaded, maybe something like this:
    This is of course something entirely different than not accepting connections.
    In comment #3 you complained about clients getting connected.

    Quote Originally Posted by atomic View Post
    that is a good idea?
    If you have a protocol between server and client then it might even make sense to specify such a "return later" exchange.
    E.g. HTTP's 503 status

    Cheers,
    _

  7. The following user says thank you to anda_skoa for this useful post:

    atomic (20th August 2015)

Similar Threads

  1. Qt 4.8.3 and ptlib 2.10.8 refuse to work together
    By BigFastAndrey in forum Qt Programming
    Replies: 0
    Last Post: 5th December 2012, 08:54
  2. [QTcpServer] Lost Connection
    By Markus_AC in forum Qt Programming
    Replies: 2
    Last Post: 12th January 2012, 12:08
  3. Replies: 0
    Last Post: 1st August 2011, 13:06
  4. Replies: 5
    Last Post: 23rd May 2010, 01:09
  5. Drag incomming localurl !=linux not work kde
    By patrik08 in forum Qt Programming
    Replies: 6
    Last Post: 11th December 2006, 15:23

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.