title: bidirectional communication via QTcpSocket
body: <p>I'm writing an application that communicates via QTcpSocket between Server and Client.</p>
  
  <p>Server is an application with gui on standard pc.</p>
  
  <p>Client is a console application on Raspberry pi.</p>
  
  <p><strong>client.cpp</strong></p>
  
  <pre><code>#include "client.h"
  #include &lt;QHostAddress&gt;
  #include &lt;cstdio&gt;
  Client::Client() :
      QObject()
  {
      socket = new QTcpSocket(this);
      printf("try to connect.\n");
      connect(socket, SIGNAL(connected()),
              this, SLOT(onConnected()));
      connect(socket, SIGNAL(readyRead()),
              this, SLOT(readyRead()));
  }
  
  void Client::onConnected()
  {
      printf("Connection established.\n");
  //    char buffer[1024];
  //    forever
  //    {
  //        while(socket-&gt;canReadLine())
  //        {
  //            QByteArray ba = socket-&gt;readLine();
  //            printf("from server: %s", ba.constData());
  //        }
  //        printf("&gt;&gt; ");
  //        gets(buffer);
  //        int len = strlen(buffer);
  //        buffer[len] = '\n';
  //        buffer[len+1] = '\0';
  //        socket-&gt;write(buffer);
  //        socket-&gt;flush();
  //    }
  }
  void Client::readyRead(){
      if(socket-&gt;canReadLine())
      {
          QByteArray ba = socket-&gt;readLine();
          printf("from server: %s", ba.constData());
      }else{
          printf(":(");
      }
  }
  
  void Client::connectToServer()
  {
      socket-&gt;connectToHost(QString("192.168.0.103"), 5100);
  }
  </code></pre>
  
  <p><strong>server.cpp</strong></p>
  
  <pre><code>#include "server.h"
  #include &lt;QTcpServer&gt;
  #include &lt;QTcpSocket&gt;
  #include &lt;cstdio&gt;
  #include &lt;QtDebug&gt;
  
  Server::Server(int listenPort)
      :QObject()
  {
      port = listenPort;
      server = new QTcpServer(this);
      connect(server, SIGNAL(newConnection()),
              this, SLOT(onNewConnection()));
  }
  
  void Server::listen()
  {
      server-&gt;listen(QHostAddress::Any, port);
  }
  void Server::sendMessage(QString message){
      qDebug() &lt;&lt; message;
      QByteArray response;
      response.append(message);
    qDebug() &lt;&lt;   socket-&gt;peerAddress() ;
      socket-&gt;write(response);
      socket-&gt;flush();
  }
  
  void Server::onNewConnection()
  {
      socket = server-&gt;nextPendingConnection();
  
      if(socket-&gt;state() == QTcpSocket::ConnectedState)
      {
  
          qDebug()&lt;&lt;socket-&gt;peerAddress();\
          emit onConnectionEstablished(socket-&gt;peerAddress().toString());
  
      }
      connect(socket, SIGNAL(disconnected()),
              this, SLOT(onDisconnected()));
      connect(socket, SIGNAL(readyRead()),
              this, SLOT(onReadyRead()));
  }
  
  void Server::onReadyRead()
  {
  
      if(socket-&gt;canReadLine())
      {
          QByteArray ba = socket-&gt;readLine();
  
          emit onMessageRecieved(QString(ba));
  
  
  
          QByteArray response("maslO!");
  
          // some code which parses arrived message
          // and prepares response
  
          socket-&gt;write(response);
          socket-&gt;flush();
      }
      //else just wait for more data
  }
  
  void Server::onDisconnected()
  {
      printf("Connection disconnected.\n");
      disconnect(socket, SIGNAL(disconnected()));
      disconnect(socket, SIGNAL(readyRead()));
      socket-&gt;deleteLater();
  }
  </code></pre>
  
  <p>This is the code. So now the problem description:</p>
  
  <p>I'm starting Server application and then Client application on Raspberry.
  Both apps are connecting I see it in "onNewConnection" method in Server.cpp. </p>
  
  <p>But when I try to send something to Client using "sendMessage" in server.cpp, Client  shows nothing.
  I should recieve data from server in "readyRead" method in client.cpp but it is never called. What did i wrong?</p>
  
  <p>My friend told me to check if i got same version of QT on both platforms. So i checked and it is not but maybe it is the problem?</p>
  
  <p><strong>Raspberry pi, Arch linux without x server</strong></p>
  
  <pre><code>QMake version 3.0
  Using Qt version 5.2.1 in /usr/lib
  </code></pre>
  
  <p><strong>Server, linux mint</strong></p>
  
  <pre><code>QMake version 3.0
  Using Qt version 5.2.0 in /home/adam/Qt/5.2.0/gcc_64/lib
  </code></pre>
  
  <p>Can you also give me some tips to improve this code?</p>