title: How to write Client-Server application and implement simple protocol in Qt
body: <p>Maybe this is stupid question, actually it's appeal, or Qt is just to complicated for me.
  Here's the thing:
  I'm used to java when writing client-server application, and it's very simple. I would like to do same things in C++ (I'm very familiar with C++ itself), and I choose to learn Qt. I tried to write some applications in qt, but with partial success.</p>
  
  <p>First thing that bothers me is signals and slots. I know how to use them in GUI programming but it confuses me with networking. And there's problem with blocking. When I call BufferedReader's readLine() method in java it blocks until it receives line from socket connection. In Qt I must make sure that there is line available every time, and handle it when there isn't one.</p>
  
  <p>And when I connect QSocket's error signal to some of my custom slots, the signal is emitted when server sends last line and closes the connection, and in client's slot/function that reads I never read that last line. That are some problems I faced so far.</p>
  
  <p>Slots and checking if there is data available makes me confused when I had to implements even the simplest protocols.</p>
  
  <p>Important part:</p>
  
  <p>I tried to find good example on the internet, but problem is that all examples are to complicated an big. Is there anyone how can show me how to write simple client-server application. Server accepts only one client. Client sends textual line containing command. If command is "ADD" or "SUB", server sends "SUP" indicating that command is supported. Otherwise it sends "UNS" and closes the connection. If client receives "SUP" it sends to more lines containing numbers to be subtracted or added. Server responds with result and closes connection.</p>
  
  <p>I know that C++ requires more coding, but in Java this would take only 5 minutes, so it shouldn't take to long to write it in C++ either.</p>
  
  <p>I'm sure this example would be very valuable to anyone who wants to learn networking in Qt.</p>
  
  <p><strong>edit:</strong>
  This is my try to make the application (described above):
  here is the server part:</p>
  
  <pre><code> #ifndef TASK_H
  #define TASK_H
  
  #include &lt;QObject&gt;
  #include &lt;QTcpServer&gt;
  
  class Task : public QObject
  {
      Q_OBJECT
  public:
      Task(QObject *parent = 0) : QObject(parent) {}
  
  
  
  public slots:
      void run();
      void on_newConnection();
      void on_error(QAbstractSocket::SocketError);
  
  signals:
      void finished();
  
   private:
      QTcpServer server;
  };
  
  
  #endif // TASK_H
  
  
  
  void Task::run()
  {
      connect(&amp;server,SIGNAL(newConnection()),this,SLOT(on_newConnection()));
      connect(&amp;server,SIGNAL(acceptError(QAbstractSocket::SocketError)),this,SLOT(on_error(QAbstractSocket::SocketError)));
      if(server.listen(QHostAddress::LocalHost, 9000)){
          qDebug() &lt;&lt; "listening";
      }else{
          qDebug() &lt;&lt; "cannot listen";
          qDebug() &lt;&lt; server.errorString();
      }
  
  }
  
      void Task::on_newConnection(){
          std::cout &lt;&lt; "handeling new connection...\n";
          QTcpSocket* socket = server.nextPendingConnection();
          QTextStream tstream(socket);
  
  
          while(!socket-&gt;canReadLine()){
              socket-&gt;waitForReadyRead((-1));
          }
  
          QString operation = tstream.readLine();
          qDebug() &lt;&lt; "dbg:" &lt;&lt; operation;
          if(operation != "ADD" &amp;&amp; operation != "SUB"){
              tstream &lt;&lt; "UNS\n";
              tstream.flush();
              socket-&gt;disconnect();
              return;
          }
          tstream &lt;&lt; "SUP\n";
          tstream.flush();
          double op1,op2;
          while(!socket-&gt;canReadLine()){
              socket-&gt;waitForReadyRead((-1));
          }
          op1 = socket-&gt;readLine().trimmed().toDouble();
          qDebug() &lt;&lt; "op1:" &lt;&lt; op1;
          while(!socket-&gt;canReadLine()){
              socket-&gt;waitForReadyRead(-1);
          }
          op2 = socket-&gt;readLine().trimmed().toDouble();
          qDebug() &lt;&lt; "op2:" &lt;&lt; op2;
          double r;
          if(operation == "ADD"){
              r = op1 + op2;
          }else{
              r = op1 - op2;
          }
  
          tstream &lt;&lt; r &lt;&lt; "\n";
          tstream.flush();
          qDebug() &lt;&lt; "result is: " &lt;&lt; r;
          socket-&gt;disconnect();
  
      }
  
      void Task::on_error(QAbstractSocket::SocketError ){
          qDebug() &lt;&lt; "server error";
          server.close();
      }
  </code></pre>
  
  <p>This is client side (header is similar to server's so I wont post it):</p>
  
  <pre><code>void Task::run()
  {
  
  
      QTcpSocket socket;
      std::string temp;
      socket.connectToHost(QHostAddress::LocalHost,9000);
  
      if(socket.waitForConnected(-1))
          qDebug() &lt;&lt; "connected";
      else {
          qDebug() &lt;&lt; "cannot connect";
          return;
      }
  
      QTextStream tstream(&amp;socket);
  
      QString op;
      std::cout &lt;&lt; "operation: ";
      std::cin &gt;&gt; temp;
      op = temp.c_str();
      tstream &lt;&lt; op &lt;&lt; "\n";
      tstream.flush();
      qDebug() &lt;&lt; "dbg:" &lt;&lt; op &lt;&lt; "\n";
  
      while(!socket.canReadLine()){
          socket.waitForReadyRead(-1);
      }
      QString response = tstream.readLine();
      qDebug() &lt;&lt; "dbg:" &lt;&lt; response;
      if(response == "SUP"){
  
          std::cout &lt;&lt; "operand 1: ";
          std::cin &gt;&gt; temp;
          op = temp.c_str();
          tstream &lt;&lt; op + "\n";
  
          std::cout &lt;&lt; "operand 2: ";
          std::cin &gt;&gt; temp;
          op = temp.c_str();
          tstream &lt;&lt; op + "\n";
  
          tstream.flush();
  
          while(!socket.canReadLine()){
              socket.waitForReadyRead(-1);
          }
          QString result = tstream.readLine();
  
          std::cout &lt;&lt; qPrintable("result is: " + result);
  
      }else if(response == "UNS"){
          std::cout &lt;&lt; "unsupported operatoion.";
      }else{
          std::cout &lt;&lt; "unknown error.";
      }
      emit finished();
  }
  </code></pre>
  
  <ol>
  <li>What I could do better?</li>
  <li>What are some good practices in similar situations?</li>
  <li>When using blocking (not signal/slot mechanism), what is the best way to handle event when other side closes the connection?</li>
  <li>Can someone rewrite this to make it look more professional (I just what to see how it supposed to look like, because I think that my solution is far from perfect) ?</li>
  <li>Can someone rewrite this using signals and slots?</li>
  </ol>
  
  <p>Thanks you.
  Sorry for my English, and probably stupidity :)</p>