0

小さなクライアント/サーバー予約アプリを実行していて、クラスの情報を送信する方法にこだわっています。実際には3つのクラスがあり、次のような情報を送信しています:

VentanaPrincipalS::VentanaPrincipalS(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::VentanaPrincipalS)
{
  //..Methods..//
  connect(conexion,SIGNAL(nuevaConexion(QTcpSocket*)), this, SLOT(enviarDataCliente(QTcpSocket*)));
  connect(conexion,SIGNAL(nuevaConexion(QTcpSocket*)), this, SLOT(enviarDataVuelo(QTcpSocket*)));
  connect(conexion,SIGNAL(nuevaConexion(QTcpSocket*)), this, SLOT(enviarDataReservacion(QTcpSocket*)));
   //..Methods..//
}
void VentanaPrincipalS::enviarDataVuelo(QTcpSocket *sock)
{
  QByteArray buffer;
  QDataStream out(&buffer, QIODevice::ReadWrite);
  out << 1;
  for(int i = 0; i < empresa.cantidadVuelos(); i++){
      out << empresa.getVuelos().at(i)->getDestino() << empresa.getVuelos().at(i)->getIdVuelo() << empresa.getVuelos().at(i)->getPartida();
  }
  if(sock->isValid())
  {
    sock->write(buffer);
  }

}
//2 More methods just like this, switching the out first number
to know which class is...//

クライアント側では、次のように受け取ります。

in>> caracterControl;
    switch(caracterControl){
    case 1:{
        while(!in.atEnd()){
            QString destino;
            QString id;
            QDate fecha;
            in >> destino >> id >> fecha;
            qDebug()<< destino +" "+ id + " " + fecha.toString();
            MVuelo vuelop(id, destino, fecha);
            listaVuelos.append(id);
            vuelosRecibidos.push_back(vuelop);
        }
     }
     case 2:{
        while(!in.atEnd()){
            QString cedula;
            QString correo;
            QString nombre;
            QString telf;
            in >> cedula >> correo >> nombre >> telf;
            MCliente cliente(nombre, cedula, telf, correo);
            qDebug()<< "Cliente: " + cedula;
        }
     }
    case 3:{
       while(!in.atEnd()){
           QString reserva;
           QString vuelo;
           in >> reserva >> vuelo;
           qDebug()<< "Reserva: " + reserva;
       }
    }

}

クラスに応じて1、2、または3。

問題は、情報が不完全であり、別のメソッドがソケットに書き込みを行っているためにソケットがクラッシュしたようなものです。すべての情報を順番に受け取る方法や、ソケットが読み取りを終了したことをサーバーに伝える方法はありますか?

私を助けてください ;(...

PD: はい、サーバーとソケットが正常に接続されていることを確認してください :)

注: 3 つのクライアント (21727090、20350202、および 123) を持つ QList があり、このトラフ qDebug() を受け取っています

2

"顧客: 21727090"

"顧客: 20350202"

"クライアント: 123"

「クライアント:」

「クライアント:」

「クライアント:」

4

1 に答える 1

0

問題は、情報が不完全であり、別のメソッドがソケットに書き込みを行っているためにソケットがクラッシュしたようなものです。すべての情報を順番に受け取る方法や、ソケットが読み取りを終了したことをサーバーに伝える方法はありますか?

最も簡単な回避策は、次のように、書き込みの後にブロック (同期) 待機を配置することです。

if (sock->isValid())
{
    sock->write(buffer);
    if (!sock->waitForBytesWritten(5000))
        qDebug() << QString("Operation timed out or an error occurred for sock, error: %1).arg(sock->errorString());
}
于 2014-01-12T05:04:21.210 に答える