PDA

View Full Version : LocalSocket and Data Loss



ManuMies
28th May 2009, 09:32
I'm using QLocalSocket (and QLocalServer) to create IPC in Windows and it works pretty nicely. But when there is lot of traffic, I'm starting to lose messages.

How I should implement IPC with local socket properly to prevent data loss?

ManuMies
29th May 2009, 10:54
Any ideas?

Some good example of local socket IPC would be very nice.

ManuMies
24th August 2009, 13:06
The problem still occurs.

How do you usually manage IPC in windows with Qt and so that it can endure some traffic? Alternatives to QLocalSocket? Or problem code example how to implement QLocalSocket for windows?

yogeshgokul
24th August 2009, 13:31
Kindly try to set QLocalServer::setMaxPendingConnections() higher up to 300.

ManuMies
24th August 2009, 15:27
What's the default value for maxpendingconnections in windows?

I doubt if this really is the problem, since isn't count of connections the number of local sockets connected to the local server? Because the problem exists even though I only have single use local socket sending and receiving lot of packages from the server.

This is how my code looks like:

QLocalSocket

Initialization:

iSocket = new QLocalSocket(this);
iSocket->connectToServer(aCallbackAddress);

Sending:

iSocket->flush();
int write = iSocket->write(aData.toAscii());
if (write == -1)
{
success = false;
}
iSocket->waitForBytesWritten(MSG_TIMEOUT);
iSocket->flush();

Receiving:

QString data = iSocket->readAll();


QLocalServer

Initialization:

iServer = new QLocalServer(this);
iSockets = new QMap<QLocalSocket*,QString>();
iServer->listen(aCallbackAddress);
connect(iServer, SIGNAL(newConnection()), SLOT(newConnectonHandle()));

Sending:

if (socket->write(aData.toAscii()) == WRITE_ERROR)
{
success = false;
}
socket->waitForBytesWritten(MSG_TIMEOUT);
socket->flush();


Receiving:

QLocalSocket* socket = 0;
for (int i=0; i < iSockets->keys().count(); i++)
{
if (iSockets->keys().at(i)->bytesAvailable() >= (int)sizeof(quint16))// Socket has some data to read
{
socket = iSockets->keys().at(i);
QString data = socket->readAll();
QStringList messageArgs = data.split(MSG_DELIMETER);
if (messageArgs.count()>2)
{
iSockets->insert(socket,messageArgs.at(2));
}
emit received(data);
}
}

Do you see anything out of place here?

ManuMies
31st August 2009, 15:31
It feels a bit like having a monologue here, but I continue...

Got it working finally. :) I feel a bit silly to say, that I didn't take the Windows buffer in account. So to get it working, the message must have a some kind of delimiter at end, because when readyRead is called, the might be more than one message to be read.

One question though. How secure is QLocalSocket?

ManuMies
8th September 2009, 15:32
Up.

Question is still open.