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 ?

Dan Snow
  • 127
  • 1
  • 11
  • I think, your IP address is invalid. check ipconfig and confirm. if it is invalid, continue with localhost/127.0.0.1. And please also improve your sample code with more information. – Ashif Sep 05 '13 at 16:07
  • it's not a ip problem the client have a loggin page so if the client don't find the server the client will stay to the loggin page moreover the server send 4 information to the client, the first is the type of the message(ex: chat message or news message), the second the headline of the new (a QString) the third is the content of the news (a QString) and the last is the source (a QString), the client server receive the 2 first information but don't receive the third information (a long text) – Dan Snow Sep 05 '13 at 16:18
  • After writing 2nd headline , set Server by server->waitForBytesWritten(4000) delay, then server->write(data); – Ashif Sep 05 '13 at 17:16
  • your solution half works, the client receive all the data but when i try whith an over content (a long text, the packet) it doesn't work anymore – Dan Snow Sep 05 '13 at 18:04

1 Answers1

1

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();
}
Tob
  • 286
  • 1
  • 17