You are not logged in.

Dear visitor, welcome to QtForum.org. If this is your first visit here, please read the Help. It explains in detail how this page works. To use all features of this page, you should consider registering. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.

1

Saturday, March 3rd 2012, 9:25pm

[SOLVED] IPC through QLocalSocket and QLocalServer problem

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
juchuchuu has attached the following file:
  • ipc.zip (9.15 kB - 23 times downloaded - latest: Dec 30th 2013, 6:02am)

This post has been edited 1 times, last edit by "juchuchuu" (May 18th 2012, 7:49pm)


mercodus

Trainee

  • "mercodus" is male

Posts: 60

Location: North Wales, UK

Occupation: Software developer

  • Send private message

2

Sunday, March 4th 2012, 12:38pm

The error seems to be caused in the server code by you resetting the device position to 0. I found that if I store the reply and then send the size and reply it works fine.

Your code:

Source code

1
2
3
4
5
6
7
8
9
	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));


My suggested fix for that block:

Source code

1
2
3
4
5
6
QByteArray block;
	QDataStream out(&block, QIODevice::WriteOnly);
	QString returnMsg = "message you sent me: " + message;

	out.setVersion(QDataStream::Qt_4_0);
	out << (quint16)returnMsg.size() << returnMsg;


I have attached my changes to the server code with the erroneous bit commented out.

HTH
mercodus has attached the following file:
  • ipcserver.zip (2 kB - 31 times downloaded - latest: Mar 20th 2014, 7:30pm)

3

Sunday, March 4th 2012, 4:51pm

thank you very much. I think I would not be able to find the cause of this problem in a reasonable time without your help...