PDA

View Full Version : Named Pipe Implementation with QLocalSocket - Windows XP



ustulation
11th July 2012, 08:03
{using Qt 4.7.4 with both MinGW 4.4.0 and Visual Studio 2010}

Requirement: An Application shares its data via overlapped, write-only, message mode pipe. It only writes to the pipe if update is required. I need to read this pipe if data is available and process the updated data else if no new data is available then process the last read data.

I'm new to the concepts of pipes in windows. From whatever little that i'v come to know by reading a few articles this is what i am doing:

//QLocalSocket namedPipe, QByteArray byteArray and QTimer timer are available as member variables.
QString pipeName="Get it from Registry";
connect(&namedPipe,SIGNAL(connected()),this,SLOT(sloConnect ed()));
connect(&namedPipe,SIGNAL(error(QLocalSocket::LocalSocketEr ror)),this,SLOT(sloConnectionError()));
namedPipe.connectToServer(pipeName,QIODevice::Read Only);

void ClassName::sloConnected()
{
connect(&timer,SIGNAL(timeout()),this,SLOT(sloProcess()));
timer.start(200); //i need to update my clients with the changed/unchanged data at regular intervals
}

void ClassName::sloProcess()
{
if(namedPipe.bytesAvailable())
byteArray=namedPipe.readAll();
//deal with byteArray. if no bytes were available assume it'll contain valid data from one of the previous reads when data was available
}

void ClassName::sloConnectionError()
{
qDebug() << "ERROR!!";
}

The Application that creates and writes to the pipe keeps running. The problem is that when i close my program and re-run it, it prints "ERROR!!".
<1> Is it normal for the pipe to vanish after i've connected-to-it and/or attempt-to-read-it once?
<2> Am i doing something conceptually wrong here?
<3> Can you please give me a hint on better solution if this is wrong/poor?

Thanks !!

amleto
11th July 2012, 10:24
surely you just need to cleanup the socket when your app exits

http://doc.qt.nokia.com/4.7-snapshot/qlocalsocket.html#disconnectFromServer

?

ustulation
11th July 2012, 11:12
surely you just need to cleanup the socket when your app exits

http://doc.qt.nokia.com/4.7-snapshot/qlocalsocket.html#disconnectFromServer

?

i didn't get you. What i'm asking is, does the pipe gets destroyed when my QLocalSocket object that connected to it got destroyed? Or is it just the connection between my application and the application that created the pipe that gets destroyed? I want multiple threads in my application to access the pipe that was created by the other application. If one of the thread ends, the pipe should not get destroyed and let other threads access it. Is this possible with pipes?

amleto
11th July 2012, 11:23
"does the pipe gets destroyed when my QLocalSocket object that connected to it got destroyed?"
Depends on the software running the server, I guess, but I wouldn't have thought so.

"If one of the thread ends, the pipe should not get destroyed and let other threads access it. Is this possible with pipes?"
Yes.


http://msdn.microsoft.com/en-us/library/windows/desktop/aa365780(v=vs.85).aspx

ustulation
11th July 2012, 11:41
thank you. Just one quick question while i read the link you gave me more. QLocalSocket can be used as a full-fledged pipe in the same way as the functions for named pipes in the link you gave right? Or is it recommended to use the windows (non-qt) way?