title: File tranfer between two machines using QTcpSocket
body: <p>My class link has all the funcionts for file transfer between two machines. Here is the code and the order in which it is called.</p>
  
  <p>Machine A sends a Request to send a file to Machine B. If B accepts it creates a TCPServer:</p>
  
  <pre><code>quint16 Link::createServerToReceiveFile(QString savefile, qint64 size){
  
      // Initializing control variables.
      saveFile = savefile;
      numberOfBytesToReceive = size;
      bytesWritten = 0;
  
      // Initializing server for Download
      transferServer = new QTcpServer();
      transferServer-&gt;listen(QHostAddress::Any);
      connect(transferServer,SIGNAL(newConnection()),this,SLOT(on_transferServerHasNewConnection()));
      return transferServer-&gt;serverPort();
  }
  </code></pre>
  
  <p>Then Machine A uses the server port to create socket for the transfer:</p>
  
  <pre><code>void Link::createTransferSocket(quint32 ipnumber, quint16 TCPport, QString file2Transfer){
      fileToTransfer = file2Transfer;
      transferSocket = new QTcpSocket();
      transferSocket-&gt;connectToHost(QHostAddress(ipnumber),TCPport);
      connect(transferSocket,SIGNAL(connected()),this,SLOT(on_transferSocketConnected()));
      connect(transferSocket,SIGNAL(readyRead()),this,SLOT(on_readyDataFromTransfer()));
      connect(transferSocket,SIGNAL(disconnected()),this,SLOT(on_transferSocketDisconnected()));
  
      qWarning() &lt;&lt; "Created transfer socket on sender";
  
  }
  </code></pre>
  
  <p>This will eventually give the newConnection signal to Machine B's TCPServer:</p>
  
  <pre><code>void Link::on_transferServerHasNewConnection(){
      transferSocket = transferServer-&gt;nextPendingConnection();
      connect(transferSocket,SIGNAL(readyRead()),this,SLOT(on_readyDataFromTransfer()));    
      connect(transferSocket,SIGNAL(disconnected()),this,SLOT(on_transferSocketDisconnected()));
      qWarning() &lt;&lt; "Created transfer socket on receiver";
      printSocketInfo();
  }
  </code></pre>
  
  <p>Once the connection is done on B, A receives the connected signal and the transfer can start like this:</p>
  
  <pre><code>void Link::on_transferSocketConnected(){
  
      qWarning() &lt;&lt; "Now there is a connection so the sender is sending the info";
      printSocketInfo();
  
      QFileInfo info(fileToTransfer);
      if (info.size() &gt; 0){
  
          qint64 bytesSent = 0;
          qint64 dataWritten;
          QFile input(fileToTransfer);
          QByteArray buffer;
  
          qWarning() &lt;&lt; "Sending data for file" &lt;&lt; fileToTransfer &lt;&lt; "Bytes:" &lt;&lt; info.size()  &lt;&lt; "Status" &lt;&lt; transferSocket-&gt;state();
  
          if (input.open(QIODevice::ReadOnly)){
              while (bytesSent &lt; info.size()){
  
                  buffer = input.read(TRANSFER_BUFFER_SIZE);
  
                  // Adding the header to the data block
                  //buffer.prepend(header.toUtf8());
  
                  if (buffer.size() &gt; 0){
  
                      dataWritten = transferSocket-&gt;write(buffer);
  
                      if (dataWritten != buffer.size()){
                          input.close();
                          transferError =  "There was an error transfering the file. " + transferSocket-&gt;errorString()  + " . Aborting";
                          break;
                      }
                      bytesSent = bytesSent + dataWritten;
                      //qWarning() &lt;&lt; "The number of bytes written is" &lt;&lt; bytesSent &lt;&lt; "out of" &lt;&lt; info.size();
                      qint32 update = bytesSent*100/info.size();
                      emit(updateTransferStatus(update,transferID));
  
                  }
                  else{
                      transferError =  "Error reading from file";
                      break;
                  }
  
              }
          }
          else{
              transferError = "Could not open file " + fileToTransfer + " for reading";
          }
  
          // Disconnecting the socket and deleting the socket. Also making sure that no transfer is now underway.
          qWarning() &lt;&lt; "Disconnecting after having supposedly finished. Transfer error is" &lt;&lt; transferError &lt;&lt; "Socket error" &lt;&lt; transferSocket-&gt;error()
                     &lt;&lt; "y el normal" &lt;&lt; socket-&gt;error() &lt;&lt; "state" &lt;&lt; transferSocket-&gt;state();
          transferID = -1;
          transferSocket-&gt;disconnectFromHost();
          transferSocket-&gt;deleteLater();
          input.close();
  
      }
      else{
          transferError = "File " + fileToTransfer + " could not be found. Aborting transfer";
      }
  }
  </code></pre>
  
  <p>Every time A writes to it's socket B should get the readyRead() signal and save the received data to a file like this:</p>
  
  <pre><code>void Link::on_readyDataFromTransfer(){
  
      qWarning() &lt;&lt; "Aca toy";
  
      QByteArray buffer;
      QFile output(saveFile);
  
      if (output.open(QIODevice::Append)){
          buffer.append(socket-&gt;readAll());
          output.write(buffer);
          bytesWritten = bytesWritten + buffer.size();
      }
  
      output.close();
  
      qWarning() &lt;&lt; "Received" &lt;&lt; bytesWritten  &lt;&lt; "of the file" &lt;&lt; saveFile;
  
      emit(updateTransferStatus(bytesWritten*100/numberOfBytesToReceive,transferID));
  
      // When all the bytes were received the transfer mode is finished.
      if (bytesWritten &gt;= numberOfBytesToReceive){
          transferID = -1;
      }
  
  }
  </code></pre>
  
  <p>The problem is that the last function is never called and I cannot detect any error neither in the socket values (IP's and ports) and every debug message is written except the on_readyDataFromTransfer() messages. This slot is just never called I can't figure out why. Also since this falls outside my actual code, I have no idea how to debug it, either.</p>
  
  <p>Anyone see anything wrong?</p>