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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T10:00:39+00:00 2026-06-13T10:00:39+00:00

I want to write a console chat program in qt framework.I have a problem

  • 0

I want to write a console chat program in qt framework.I have a problem with sending messages.

Client sends messages to server but server doesn’t take the messages until client program is closed.When client is closed, server displays all messages.I don’t want that.I want server to get my messages when i send to it.

I wrote the codes below.You will see what i want to do if you look at main function of client.

    /*

    Created BY : 
    Creation DATE : 26/10/2012

    Client interface

    */

    #ifndef CLIENT_H
    #define CLIENT_H

    #include <QtNetwork>
    #include <QObject>
    #include <QtNetwork/QTcpSocket>

    namespace NetworkArdic
    {

    class Client : public QObject
    {
        Q_OBJECT
        public:

            Client(QObject * obj = 0,QString add="localhost", quint16 port = 4000);


            void SendData(QString data);

            virtual ~Client();

        private slots:


            void ReadData();

            void connected();

        private:

            QTcpSocket *socket;
    };

    }

    #endif


 /*

    Created BY : 
    Creation DATE : 26/10/2012

    Client source file

    */

    #include "Client.h"
    #include <QHostAddress>
    #include <iostream>
    using namespace std;

    namespace NetworkArdic{

    Client::Client(QObject * obj, QString add, quint16 port) : QObject(obj)
    {

        socket = new QTcpSocket(this);


        connect(socket, SIGNAL(readyRead()), this, SLOT(ReadData()));
        connect(socket, SIGNAL(connected()), this, SLOT(connected()));

        socket->connectToHost(QHostAddress(add), port);
    }

    Client::~Client(){
        socket->close();
        delete socket;
    }



    void Client::SendData(QString data)
    {
        if(!data.isEmpty())
        {
            socket->write(QString(data + "\n").toUtf8());
        }
    }

    void Client::ReadData()
    {
        while(socket->canReadLine())
        {

            QString line = QString::fromUtf8(socket->readLine()).trimmed();
            qDebug() << line;
        }
    }

    void Client::connected()
    {
        socket->write(QString("Client : Server connection has been made (: \n").toUtf8());
    }

    }

    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);

        Client cli(0,"127.0.0.1",4000);

        string line;
        while(line!="exit"){
            cout << "Message : ";
            cin >> line;
            cli.SendData(QString(line.c_str()));
        }

        return a.exec();
    }

     /*

    Created BY : 
    Creation DATE : 26/10/2012

    Server interface


    */

    #ifndef SERVER_H
    #define SERVER_H

    #include <QtNetwork>
    #include <QObject>
    #include <QtNetwork/QTcpServer>
    #include <QtNetwork/QTcpSocket>

    namespace NetworkArdic
    {

        class Server: public QTcpServer
        {

            Q_OBJECT
            public:

              Server(QObject * parent = 0 , quint16 port = 4000);
              virtual  ~Server();

            private slots:

              void acceptConnection();
              void startRead();
              void disconnected();

            private:

              QTcpSocket * client;

        };

    }

    #endif // SERVER_H



   /*

    Created BY : 
    Creation DATE : 26/10/2012

    Server source file

    */


    #include "Server.h"
    #include <iostream>
    using namespace std;

    namespace NetworkArdic{

    Server::Server(QObject* parent , quint16 port): QTcpServer(parent)
    {
      connect(this, SIGNAL(newConnection()),this, SLOT(acceptConnection()));

      listen(QHostAddress::Any, port );
    }

    Server::~Server()
    {
      delete client;
      close();
    }

    void Server::acceptConnection()
    {
      client = nextPendingConnection();

      connect(client, SIGNAL(readyRead()), this, SLOT(startRead()));
      connect(client, SIGNAL(disconnected()), this, SLOT(disconnected()));

      qDebug() << "New client from:" << client->peerAddress().toString();
    }

    void Server::startRead()
    { 
        while(client->canReadLine())
        {
            QString line = QString::fromUtf8(client->readLine()).trimmed();
            qDebug() << "Client :" << line;

            client->write(QString("Server : I've taken your message (:\n").toUtf8());
        }

    }

    void Server::disconnected()
    {

        qDebug() << "Client disconnected:" << client->peerAddress().toString();

        client->write(QString("Server : I wish you didn't leave ):\n").toUtf8());

    }

    }
  • 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-13T10:00:40+00:00Added an answer on June 13, 2026 at 10:00 am

    Try using socket->flush() after you write the data.

    http://doc.qt.digia.com/qt/qabstractsocket.html#flush

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

Sidebar

Related Questions

I have to write a simple chat in C. One program is a server
I have a console application in which I want to write the name of
I want to write a simple program, that writes console input to file with
I want to write a console application that have a different behavior depending if
I want to write quick console apps and compile them with VS 2010, but
I want to write a console program for mouse events (Only mouse scroll). How
I have a byte[] array and want to write it to stdout: Console.Out.Write(arr2str(arr)) .
I want to write english transcription in console. In debugger I have this səˈdʒest
I want to write a PHP script similar to Enterprise Network Console , but
I want the logs from the Selenium Server Console write to file. I know

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.