Hello everyone. I am trying to test simple message sending and receiving between two programs using QLocalSocket and QLocalServer.
The first program, which listens on a socket, does only this:
- receive the message from a new connected client & send the same message back to the client
The second program, client, does only this:
- connect to server & send the message to the server
- receive message from the server & print it to the console
Problem is, that the client does not receive the same message, it sent only moments ago - the message if full of question marks.
sending the message back to the client code located just after the code which handles the receiving of the message from the client:
|
Source code
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
QByteArray block;
QDataStream out(&block, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_4_0);
out << (quint16)0;
out << "message you sent me: " << message;
qDebug() << (quint16)(block.size() - sizeof(quint16));
out.device()->seek(0);
out << (quint16)(block.size() - sizeof(quint16));
qint64 c = clientConnection->write(block);
clientConnection->waitForBytesWritten();
qDebug() << "number of bytes written" << c;
if (c == -1)
qDebug() << "ERROR:" << clientConnection->errorString();
clientConnection->flush();
|
the code on the client program which handles message received from the server:
|
Source code
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
qDebug() << "new bytes in soket";
QDataStream in(sock);
in.setVersion(QDataStream::Qt_4_0);
if (bytes_to_read == 0) {
if (sock->bytesAvailable() < (int)sizeof(quint16)){
qDebug() << "waiting for message length";
return;
}
in >> bytes_to_read;
qDebug() << "message length received:" << bytes_to_read;
}
if (in.atEnd()){
qDebug() << "waiting for message bytes";
return;
}
in >> received_message;
bytes_to_read = 0;
qDebug() << "received message:" << received_message;
|
attachments -
"dip" is the client program directory