Answer the question
In order to leave comments, you need to log in
Qt server and java client. Why is the server not reading data from the client?
Good afternoon.
the task is to write a client-server application. Server in Qt, client in Java for android. The server has so far taken from qt examples.
The problem is this: if I send a string from a java client, the server accepts an empty string, that is, "". if you send a string from the qt client (from the qt examples), then everything is accepted normally. Tell me, what could be the matter?
Server code (slot that reads data from a client socket):
void MyServer::slotReadClient()
{
QTcpSocket* pClientSocket = (QTcpSocket*)sender();
QDataStream in(pClientSocket);
in.setVersion(QDataStream::Qt_4_7);
QTime time;
QString str;
while (1)
{
if (!m_nNextBlockSize)
{
if (pClientSocket->bytesAvailable() < (int)sizeof(quint16))
{
break;
}
in >> m_nNextBlockSize;
}
if (pClientSocket->bytesAvailable() < m_nNextBlockSize)
{
break;
}
in >> time >> str;
m_nNextBlockSize = 0;
}
qDebug() << str;
}
void MyClient::slotSendToServer()
{
QByteArray arrBlock;
QDataStream out(&arrBlock, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_4_7);
out << quint16(0) << QTime::currentTime() << m_ptxtInput->text();
out.device()->seek(0);
out << quint16(arrBlock.size() - sizeof(quint16));
m_pTcpSocket->write(arrBlock);
}
private void writeMessage(String message)
{
if (socket != null)
{
try {
outStream.writeUTF(message);
outStream.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
}
in.setVersion(QDataStream::Qt_4_7);
out.setVersion(QDataStream::Qt_4_7);
Answer the question
In order to leave comments, you need to log in
You have to look at what's really coming. And somehow you send in different ways from the client to qt and the client to java. Well, keep in mind that writeUTF writes not just a stream of encoded bytes of a string to a stream, but first two bytes of length and then the string itself in modified UTF.
See a cross-platform solution for passing data in the right format, like protobuff . The link has an implementation for c++ and java.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question