void writeMessage(QTcpSocket* socket, QByteArray& data1, QByteArray& data2) { socket->write(IntToArray(data1.size() + data2.size())); socket->write(data1); socket->write(data2); socket->waitForBytesWritten(); }
void TCPServer::sendData(const QByteArray &data) { if (!server) return; // Do nothing if there are no active connections if (socketMap.size() <= 0) return; // Build the message QByteArray message; message.append("TNO"); // Header message.append(IntToArray((quint16) data.size())); message.append(data); // Send the message QMap<QTcpSocket *, QTcpSocket *>::iterator ism; for (ism = socketMap.begin(); ism != socketMap.end(); ++ism) { QTcpSocket *socket = ism.value(); socketMap[socket]->write(message); if (!socketMap[socket]->flush()) emit warning(classname, "TCP server socket flush failed"); } emit info(classname, "send data: " + QString(data)); }
foreach (QTcpSocket *socket, sockets) { #ifdef OPUS socket->write(IntToArray(data.size())); #endif socket->write(data); socket->flush(); }
bool Client::writeData(QByteArray data) { if(socket->state() == QAbstractSocket::ConnectedState) { socket->write(IntToArray(data.size())); //write size of data socket->write(data); //write the data itself return socket->waitForBytesWritten(); } else return false; }
bool TcpClient::writeData(QByteArray data) { if(socket->state() == QAbstractSocket::ConnectedState) { qDebug() << "sending data: " + QString::fromUtf8(data); socket->write(IntToArray(data.size())); //write size of data socket->write(data); //write the data itself return socket->waitForBytesWritten(); } else return false; }