title: Console Chat Using QTcpSocket and QTcpServer
body: <p>I want to write a console chat program in qt framework.I have a problem with sending messages.</p>
  
  <p>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.</p>
  
  <p>I wrote the codes below.You will see what i want to do if you look at main function of client.</p>
  
  <pre><code>    /*
  
      Created BY : 
      Creation DATE : 26/10/2012
  
      Client interface
  
      */
  
      #ifndef CLIENT_H
      #define CLIENT_H
  
      #include &lt;QtNetwork&gt;
      #include &lt;QObject&gt;
      #include &lt;QtNetwork/QTcpSocket&gt;
  
      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 &lt;QHostAddress&gt;
      #include &lt;iostream&gt;
      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-&gt;connectToHost(QHostAddress(add), port);
      }
  
      Client::~Client(){
          socket-&gt;close();
          delete socket;
      }
  
  
  
      void Client::SendData(QString data)
      {
          if(!data.isEmpty())
          {
              socket-&gt;write(QString(data + "\n").toUtf8());
          }
      }
  
      void Client::ReadData()
      {
          while(socket-&gt;canReadLine())
          {
  
              QString line = QString::fromUtf8(socket-&gt;readLine()).trimmed();
              qDebug() &lt;&lt; line;
          }
      }
  
      void Client::connected()
      {
          socket-&gt;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 &lt;&lt; "Message : ";
              cin &gt;&gt; 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 &lt;QtNetwork&gt;
      #include &lt;QObject&gt;
      #include &lt;QtNetwork/QTcpServer&gt;
      #include &lt;QtNetwork/QTcpSocket&gt;
  
      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 &lt;iostream&gt;
      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() &lt;&lt; "New client from:" &lt;&lt; client-&gt;peerAddress().toString();
      }
  
      void Server::startRead()
      { 
          while(client-&gt;canReadLine())
          {
              QString line = QString::fromUtf8(client-&gt;readLine()).trimmed();
              qDebug() &lt;&lt; "Client :" &lt;&lt; line;
  
              client-&gt;write(QString("Server : I've taken your message (:\n").toUtf8());
          }
  
      }
  
      void Server::disconnected()
      {
  
          qDebug() &lt;&lt; "Client disconnected:" &lt;&lt; client-&gt;peerAddress().toString();
  
          client-&gt;write(QString("Server : I wish you didn't leave ):\n").toUtf8());
  
      }
  
      }
  </code></pre>