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 6357051
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T23:05:24+00:00 2026-05-24T23:05:24+00:00

I’ve modified the threaded fortune-server from Qt examples. The client connects to the server

  • 0

I’ve modified the threaded fortune-server from Qt examples.
The client connects to the server and then sends a header to authenticate.

tcpSocket = new QTcpSocket();
tcpSocket->connectToHost(addr, port);
QByteArray block = "someheader";
int x = tcpSocket->write(block);
qDebug() << x;

The client seems OK here and qDebug prints the actual size of block.

On the server side I’ve predefined incomingConnection and I start thread to each new connection.

void Server::incomingConnection(int socketDescriptor) {
    const QString &str = vec[qrand() % vec.size()];
    SpellThread *thread = new SpellThread(socketDescriptor, str);
    connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
    qDebug() << " -- incoming connection";
    thread->start();
}

I’m connecting sock to check is there something to read. (sock here is QTcpServer*)

void SpellThread::run() {
    qDebug() << " -- in spellthread";
    connect(sock, SIGNAL(readyRead()), this, SLOT(checkBytes()));
    //....
    qDebug() << " -- end spellthread";
}

The first problem is that when I’m sending data from the client, readyRead is not fired. (I’ve added debug message in checkBytes)
Messages are:

 -- incoming connection 
 -- in spellthread 
 -- end spellthread

Although the client prints the actual size of header length.

The second problem is that checkBytes currently is very bad-designed. First it checks is header OK and sets a flag, then it gets the size of message and sets another flag and finally it gets the real message. This is very clumsy. I first tried to escape signals and instead use sock->waitForReadyRead(). However it always returns false. (From the docs: “Reimplement this function to provide a blocking API for a custom device. The default implementation does nothing, and returns false.”).

So how to really make a client/server application in Qt with multiple clients and multiple reads/writes? I really want suggestions to improve design of my application and to solve my current two problems.

  • 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-05-24T23:05:24+00:00Added an answer on May 24, 2026 at 11:05 pm

    You can’t use slots or socket signals with a thread without calling QThread::exec() to start an event loop within that thread/the run function.

    Since your checkBytes slot belongs to QThread, it wouldn’t be executed by the thread (there is a detailed article about QThreads here)

    The closest example that seems to already do what you want is the Network Chat (particularly the two classes Server and Connection).

    ———-
    Edit

    If you need to use threads (without any slot), the QTcpSocket object must belongs to the same thread as the one where you call waitForReadyRead. For example, with:

    SpellThread::SpellThread(int socketDescriptor, const QString & str) {
        tcpSocket = new QTcpSocket(); // There should be no parent to be able
                                      // to move it to the thread
        tcpSocket->moveToThread(this);
        ...
    

    Or by creating the QTcpSocket object inside the run function so that it automatically belongs to that thread (it was briefly explained in the fortune example).
    If you allocate the QTcpSocket dynamically, and because it won’t have a parent, you should also delete it manually.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I want use html5's new tag to play a wav file (currently only supported
I am currently running into a problem where an element is coming back from
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function

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.