PDA

View Full Version : QT shared memory



sweetsubha2020
16th January 2009, 05:07
iam a begginer to QT,... can somebody tell me how to create a shared memory in Qt,
pls give me some sample coding..
i am using Fedora and QT4.5
thanks in advance

jpn
16th January 2009, 07:23
Please launch Qt Assistant from your Qt installation and type "shared memory" (or just "shared") to the index tab.

sweetsubha2020
18th January 2009, 05:30
My project has the following thing,
---->Network connection
---->Retreive data from server and store it in shared memory in client side

Network connection is working good.
regarding shared memory, i retreive data from server and store it in Buffer1 in client side and

from buffer1 it is then saved into shared memory so that it can be used by other processes.Later

client application reads the data with the help of buffer2.

It goes like this


server<------------------>client
(buffer1)--sharedmemory--(buffer2)


First i read the data from server into a char array with the help of read()

client.read(buffer,client.bytesAvailable());

and then i write it to buffer1 with write()

qbuffer.write(buffer);

Next i have created a shared memory and wrote that data into shared memory.

My problem is :
>>>>i get some garbage values from both buffer1 and shared memory.i dont know
how to allocate memory dynamically without garbage value
>>>>Also when i read data from buffer2 i get segmentation error.

//writing data from shared memory to buffer2
qbuffer1.setData((char*)sharedMemory.constData(), sharedMemory.size());

//reading data from buffer2
const char *cha=qbuffer1.data().data();

i have attached my code with this pls do help me....



#include "client.h"
#include <QHostAddress>
#include <iostream>
#include<QString>
#include<QTimer>
#include <QSharedMemory>
#include<QChar>
using namespace std;
Client::Client(QObject* parent)
{
connect(&client, SIGNAL(connected()),this, SLOT(startTransfer()));
connect(&client, SIGNAL(readyRead()),this, SLOT(startRead()));
connect(&client, SIGNAL(disconnected()),this,SLOT(serverdisconnecte d()));
}

void Client::start(QString address, quint16 port)
{
QHostAddress addr(address);
client.connectToHost(addr, port);
}
void Client::startRead()
{
//READING DATA INTO CHARACTER ARRAY
char buffer[1024] = {0};
client.read(buffer,client.bytesAvailable());
cout <<"server:"<< buffer << endl;
qint64 si=112;
//WRITING IT INTO BUFFER1
QBuffer qbuffer;
char ch[]="";
qbuffer.open(QBuffer::ReadWrite);
qbuffer.write(buffer);
qbuffer.seek(0);
qbuffer.read(ch,si);
cout<<ch<<"\n";

int size = qbuffer.size();
//SHARED MEMORY
QSharedMemory sharedMemory;
sharedMemory.setKey ("552");
if(!sharedMemory.create(size))
{
cout<<"Unable to create shared memory segment.";
exit(0);
}
if (sharedMemory.isAttached())
cout<<"\nAttached.......\n";
cout<<"shared mem segment key :"<<sharedMemory.key().toStdString();

//WRITING INTO SHARED MEMORY
sharedMemory.lock();
char *to = (char*)sharedMemory.data();
const char *from = qbuffer.data().data();
memcpy(to, from, qMin(sharedMemory.size(), size));
sharedMemory.unlock();


QBuffer qbuffer1;
qbuffer1.open(QBuffer::ReadWrite);
//WRITING DATA INTO BUFFER2
sharedMemory.lock();
qbuffer1.setData((char*)sharedMemory.constData(), sharedMemory.size());
sharedMemory.unlock();
sharedMemory.detach();
const char *cha=qbuffer1.data().data() ;
cout<<"\nread data:"<<cha<<endl;
}
void Client::startTransfer()
{
cout<<"connected\n";
QDataStream out;
QString str="hi";
string str1;
cout<<"client:";
cin>>str1;
str=QString::fromStdString(str1);
client.write(qPrintable(str));
}
void Client::serverdisconnected()
{
cout<<"\n--------------Server disconnected!!!! try again-------------------\n";
client.close();
exit(0);
}

thanks in advance. pls do help me, it is urgent