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. Server unable to handle clients properly

Server unable to handle clients properly

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 2.2k Views 1 Watching
  • 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.
  • W Offline
    W Offline
    webmaster.skelton
    wrote on last edited by
    #1

    Ok so here is my issue. I am working on a server that needs to be able to read in data from multiple clients and send it up to a database. Now if i just connect one, it is fine, but when i connect the next client, it does not even acknowledge that one is connected, until around 10 seconds then I am able to connect a new one and read in the data. I would imagine it has something to do with how i am handling my socket, but i am not sure what to do. Here is my server code:

    @
    void Server::runServer()
    {
    Server::server.listen(QHostAddress::Any,9003);

    QTcpSocket *client = new QTcpSocket();

    ///database stuff here

     Server::server.setMaxPendingConnections(20000);
    

    while(1)
    {

    if(!Server::server.isListening())
    {   // needs to be replaced to our error code management
        qDebug("An Error Has Occured, Server not operating");
        server.errorString();
    
    
    
    }
    else
    
    
    
              while(Server::server.waitForNewConnection(0,false))
                  {
    
                      cout<<"waiting for a new connection";
    
    
    
                      while(Server::server.hasPendingConnections()==true)
    
                     {
    
    
    
                          client = Server::server.nextPendingConnection();
    
    
                            Server::clientconnections.append(client);
    
    
    
                             foreach(QTcpSocket *client,Server::clientconnections)
                          {
    
    
    
    
    
                              client->waitForConnected(10000);
    
                              client->write("CONNECTED");
    
                              client->flush();
    
                             cout<<"client(s) connected";
    
                                 }
    
                         client->waitForReadyRead(100);
                         QByteArray clientIn = client->read(800);
    
                        QString input(clientIn);
    
    
                         client->flush();
    
                             if(input == "UPLOAD")
                            {
                             cout<< "Welcome";
                             client->waitForReadyRead(1000);
                            QByteArray in = client->readAll();
    
    
                                client->errorString();
    
                                QString run(in);
                                QVariant x = run;
                                 qDebug() << run;
    
                                  db.open();
    
                                     if(db.isOpenError())
                                     {
                                         qDebug(db.lastError().text().toStdString().c_str());
                                          }
    
            
            
    
             query.exec&#40;"INSERT INTO SocketTest(Title,Description&#41; VALUES('xml','"+in.toHex(&#41;+"'&#41;");
           
                                 db.close();
    
    
                            }
                                else
                              if(input =="DOWNLOAD")
               {
    
                             db.open();
                               
    
    
    
    
    
           }
    
    
    
    
       } } }}
    

    @

    like i said if i have made an obvious mistake i apologize, I am still wrapping my head around this.

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andre
      wrote on last edited by
      #2

      There is a fundamental problem in your design. You should make your server work asynchronously. A while(1) is deadly. You should also avoid things like waitForNewConnection or any of the other synchronous methods.

      In this design, you don't give the eventloop time to do it's thing. The Qt networking stack is designed to be used asynchronically. That means that you just setup your server to listen, connect to its signals, and react when something happens.

      Using Qt classes:

      Setup a QTcpServer instance to listen at the port you want. Connect to it's newConnection() signal. In response to that signal, query your server instance for the new TCP socket using QTcpServer::nextPendingConnection() untill you get a 0 back. You now have QTcpSockets for all requests. Deal with those in the same asynchronous way: read data from the socket in response to the readyRead() signal, don't spin loops or use waitFor* methods.

      1 Reply Last reply
      0
      • W Offline
        W Offline
        webmaster.skelton
        wrote on last edited by
        #3

        Thank you for the direction, I assumed i had done things incorrectly for what i need. This was my first attempt at it.

        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