Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9248777
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T10:01:28+00:00 2026-06-18T10:01:28+00:00

I am writing a server as a Qt console application. I have the server

  • 0

I am writing a server as a Qt console application. I have the server set up to wait for a socket connection, but I also need to allow a user to input commands into the server for managing it. Both are working independently. However, the problem I ran into is that when I’m in a while loop accepting and processing input commands, the server doesn’t accept connections.

I have a Socket class, and in its constructor, I have:

connect(server,SIGNAL(newConnection()),this, SLOT(newConnection()));

Right under that in the constructor, I call a function that has a more in-depth version of this for getting commands from the user:

QTextStream qin(stdin, QIODevice::ReadOnly);
QString usrCmd;

while(usrCmd != "exit" && usrCmd != "EXIT") {   
    //Get command input and process here
}

Inside newConnection(), I just accept the next connection and then use the socket.

QTcpSocket *serverSocket = server->nextPendingConnection();

How can I make it so the socket can wait for connections and wait for user-inputed commands at the same time?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T10:01:29+00:00Added an answer on June 18, 2026 at 10:01 am

    Problem with your code is because you are blocking event loop with your while loop. So, the solution to your problem is to read from stdin asynchronously. On Linux (and on Mac, I guess), you can use QSocketNotifier to notify when the data is arrived on stdin, and to read it manually), as per various internet sources.

    As I am using Windows, I would suggest you to do it in this way (which should work on all platforms):

    1. Open the thread for reading data from stdin
    2. Once you get some data (perhaps line?) you can use Qt signal-slot mechanism to pass the data to main thread for processing without blocking the event loop.

    So, this is the pseudocode. MainAppClass should your existing server class, just edit the constructor to create new thread, and add new slot for processing the data.

    class Reader: public QThread
    {
        Q_OBJECT
     public:
        Reader(QObject * parent = 0 ): QThread(parent){}
    
        void run(void)
        {
             forever{
                 std::string data;
                 std::getline (std::cin, data);
    
                 if(data == "exit")
                 {
                     emit exitServer();
                     return;
                 }
    
    
                 emit dataReady(QString::fromStdString(data));
             }
         }
    
    signals:
        void dataReady(QString data);
        void exitServer();
    
    };
    
    
    
    class MainAppClass: public QObject
    {
        Q_OBJECT
    public:
        MainAppClass()
        {
            Reader * tr = new Reader(this);
            connect(tr, SIGNAL(dataReady(QString)), this, SLOT(processData(QString)));
            connect(tr, SIGNAL(exitServer()), this, SLOT(exitServer()));
            tr->start();
        }
    
    public slots:
        void processData(QString data)
        {
    
            std::cout << "Command: " << data.toStdString() << std::endl;
        }
    
        void exitServer()
        {
           std::cout << "Exiting..." << std::endl;
        }
    };
    
    int main(int argc, char *argv[])
    {
        QApplication app(argc, argv);
        MainAppClass myapp; //your server
    
        app.exec();
        return 0;
    }
    

    Since I wrote simple guidelines how to use QTcpSocket, here is the brief

    When you get client QTcpSocket, connect readyRead() signal to some slot, and read data from sender() object. You don’t need to read anything in the constructor.

    For reading you can use standard QIODevice functions.

    Note: this is pseudo code, and you may need to change few things (check the state of the stream on reading, save pointer to sockets in some list, subscribe to disconnected() signal, call listen() in constructor, check if QTcpServer is listening, etc).

    So, you need to have slot onReadyRead() in your class which will have the following code:

    void Server::readyReadSlot()
    {
        QTcpSocket *client = (QTcpSocket*)sender(); // get socket which emited the signal
        while(client->canReadLine()) // read all lines! 
                                     // If there is not any lines received (you may not always receive 
                                     // whole line as TCP is stream based protocol),
                                     // you will not leave data in the buffer for later processing.
        {
            QString line = client->readLine(); 
            processLine(line); // or emit new signal if you like
        }
    }
    

    Inside newConnection() you need to connect readyRead() signal with your slot.

    void Server::newConnection()
    {
        QTcpSocket *clientSocket = server->nextPendingConnection(); 
        connect(clientSocket, SIGNAL(readyRead()), this, SLOT(readyReadSlot()));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing a C# console application that connects to a server trough TCP,
I'm currently writing a jdbc client-server socket application in java. I've been having a
I'm writing a small c# console application that needs to interogate Websphere Application Server
I'm writing an application where I open up a socket when a user visits
We are writing an application which needs a dummy user as a socket.io client
I have just finished writing my c# console application, and I am contemplating embedding
I have a c# console application that I invoke with a server call in
I'm writing a C# console application that connects to a server via TCP and
I am writing a .NET Remoting application. I have my dll, server, and client
When writing server-side code you need to explicitly stop execution after sending a Location:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.