Pergunta

Hello everyone i did a server and a client, in loaclHost this programms work perfectly but when i change the localHost to my ip the Client no longer receive all the data, so i decide to rewrite the code. here is a part of my new code:

       void fenPrincipal::test()
      {

qDebug()<<tailleContenu<<"taille Contenu 1";
QDataStream in(actualSocket);
  if (tailleContenu == 0) 
{             
if (actualSocket->bytesAvailable() < (int)sizeof(quint64)) 
               return;

          in >> tailleContenu; 
          qDebug()<<tailleContenu<<"taille Contenu";
  }


  a =  actualSocket->bytesAvailable();
  qDebug()<<actualSocket->bytesAvailable()<<"byte available";

   if ( a <  tailleContenu) 
   {
       return;
   }
   else if (a>tailleContenu){
   QString messageRecue;
   in >> messageRecue;
   qDebug()<<messageRecue<<"message";
tailleContenu=0;

   }
}

and here is the debug informations:

  1448 byte available 
  "Dragon Age: Inquisition -Du gameplay, des images et des détails" titre <- it's the second information send by the server
    0 taille Contenu 1 
   2812 taille Contenu 
   1310 byte available 
   2812 taille Contenu 1 
   1460 byte available 
   2812 taille Contenu 1 
   2920 byte available 
   "" message
   0 taille Contenu 1 
   30962754250670195 taille Contenu 
   1452 byte available 
   30962754250670195 taille Contenu 1 
   2912 byte available 
   30962754250670195 taille Contenu 1 
   4372 byte available 
   30962754250670195 taille Contenu 1 
   5832 byte available 
   30962754250670195 taille Contenu 1 
   6806 byte available 

somebody can help tell my why it does'nt work and how make my client work ?

Foi útil?

Solução

Using a tcp connection, you can never know how many packages are send until a message is submitted. But you can let Qt handle that stuff. You nedd to implement something like this:

connect( m_pTcpServer, SIGNAL( newConnection()), SLOT( solt_newConnection()) );

void CTcpManager::solt_newConnection() {
  m_pTcpSocket = m_pTcpServer->nextPendingConnection();
  connect( m_pTcpSocket, SIGNAL(readyRead()), this, SLOT( slot_startRead() ) );
}

void CTcpManager::slot_startRead() {
  QByteArray grDatagram;
  grDatagram = m_pTcpSocket->readAll();

  // Process data

  m_pTcpSocket->close();
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top