P
P
Pavel2013-10-15 10:16:04
Java
Pavel, 2013-10-15 10:16:04

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


qt client code (data sending slot that everything works with):
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);
}


Java client code (method of sending a message that the server does not receive):
private void writeMessage(String message)
    {    
        if (socket != null)
        {
            try {
                outStream.writeUTF(message);
                outStream.flush();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


I think the problem is with strings.

in.setVersion(QDataStream::Qt_4_7);
out.setVersion(QDataStream::Qt_4_7);


in qt server and client. That is, there is a data protocol. and from the java code, I just pass a string.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
B
barker, 2013-10-15
@barker

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.

B
Boris Vanin, 2013-11-20
@fogone

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 question

Ask a Question

731 491 924 answers to any question