i want to sending json object over tcp socket and, i serializing the json object like this :
void message_shooter::send_packet(QJsonObject packet)
{
QJsonDocument json_doc(packet);
//QByteArray packet_bytes=json_doc.toBinaryData();
m_client_socket->write(packet_bytes);
m_client_socket->waitForBytesWritten();
m_client_socket->flush();
qDebug()<<"Byte Written!";
}
void message_shooter::send_packet(QJsonObject packet)
{
QJsonDocument json_doc(packet);
//QByteArray packet_bytes=json_doc.toBinaryData();
QByteArray packet_bytes=json_doc.toJson();
m_client_socket->write(packet_bytes);
m_client_socket->waitForBytesWritten();
m_client_socket->flush();
qDebug()<<"Byte Written!";
}
To copy to clipboard, switch view to plain text mode
and i reading data like this :
void connectivity_layer::on_socket_ready_read()
{
qDebug()<<"Ready read...";
qint64 incomming_packet_size=m_socket->bytesAvailable();
if (incomming_packet_size > 0)
{
QByteArray received_bytes
=m_socket
->read
(incomming_packet_size
);
m_buffer.append(received_bytes);
m_buffer.simplified();
QJsonParseError parse_error;
QJsonDocument json_doc=QJsonDocument::fromJson(m_buffer,&parse_error);
if (parse_error.error == QJsonParseError::NoError)
{
QJsonObject i_packet=json_doc.object();
m_buffer.clear();
analys_packet(i_packet);
}
else
{
qDebug()<<"Error!";
qDebug()<<QString::fromUtf8(m_buffer);
qDebug()<<parse_error.errorString();
}
}
}
void connectivity_layer::on_socket_ready_read()
{
qDebug()<<"Ready read...";
qint64 incomming_packet_size=m_socket->bytesAvailable();
if (incomming_packet_size > 0)
{
QByteArray received_bytes=m_socket->read(incomming_packet_size);
m_buffer.append(received_bytes);
m_buffer.simplified();
QJsonParseError parse_error;
QJsonDocument json_doc=QJsonDocument::fromJson(m_buffer,&parse_error);
if (parse_error.error == QJsonParseError::NoError)
{
QJsonObject i_packet=json_doc.object();
m_buffer.clear();
analys_packet(i_packet);
}
else
{
qDebug()<<"Error!";
qDebug()<<QString::fromUtf8(m_buffer);
qDebug()<<parse_error.errorString();
}
}
}
To copy to clipboard, switch view to plain text mode
But this doesn't work and print 'Garbage at the end of the document' error! [when the data received completely]
how can i solve this problem?
Bookmarks