如何同时发送多个信息
How to send multiple information trought QTcpSocket at the same time?
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());
}
相关文章:
- 在执行其他功能的同时播放动画(LED矩阵和Arduino/ESP8266)
- 正在查找文档以获得PS4平台的C++中的设备信息
- 如何在linux终端中同时编译和运行c++代码
- 为什么在运行时没有向我们提供有关分段错误的更多信息?
- 奇怪的(对我来说)返回声明 - 在谷歌上找不到任何关于它的信息
- 同时具有"聚合初始化"和"模板推导"
- 函数何时会在c++中包含stack_Unwind_Resume调用
- 是否可以使用winusb同时与多个相同的usb设备进行通信
- Python中的for循环与C++有何不同
- 如何设置一个范围来提取我想要获得的信息
- 维护unordered_map但同时每一步都需要最低的映射值
- 是否可以同时声明一个类成员的常量/非常量?
- 如何将C++闭包与变量参数同时重用——类似于JavaScript
- 如何以优化的方式同时迭代两个间距不相等的数组
- 如何在对<char>C++程序进行逆向工程的同时将 std::basic_string 转换为 Rust 可读值?
- 在前进的同时更改FOV(视野)
- 系统参数信息A 与 SPI_GETMOUSE 返回 0
- 为什么 gcovr 会生成空覆盖率统计信息?
- 如何同时发送多个信息
- ASIO :服务器如何在侦听客户端的同时主动向客户端发送信息