如何同时发送多个信息

How to send multiple information trought QTcpSocket at the same time?

本文关键字:信息 何同时      更新时间:2023-10-16

im 做一个小型客户端/服务器预订应用程序,我坚持如何发送类的信息,实际上我有 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:是的,服务器和套接字连接成功,我确定

:)

注意:我有一个 QList,有 3 个客户端(21727090、20350202 和 123),并且我正在接收这个 qDebug()

阿拉伯数字

"客户:21727090"

"客户:20350202"

"客户:123"

"客户: "

"客户: "

"客户: "

问题是信息不完整,就像套接字崩溃一样,

因为另一种方法正在写在他身上,有没有办法按顺序接收所有信息或告诉服务器套接字完成读取?

最快的解决方法是在写入后放置一个阻塞(即同步)等待,如下所示:

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());
}