0

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 ?

4

1 回答 1

1

使用 tcp 连接,在提交消息之前,您永远无法知道发送了多少包。但是你可以让 Qt 处理这些东西。你需要实现这样的东西:

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();
}
于 2013-09-08T18:06:05.670 回答