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
Printable View
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
Please launch Qt Assistant from your Qt installation and type "shared memory" (or just "shared") to the index tab.
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....
thanks in advance. pls do help me, it is urgentCode:
#include "client.h" #include <QHostAddress> #include <iostream> #include<QString> #include<QTimer> #include <QSharedMemory> #include<QChar> using namespace std; { connect(&client, SIGNAL(connected()),this, SLOT(startTransfer())); connect(&client, SIGNAL(readyRead()),this, SLOT(startRead())); connect(&client, SIGNAL(disconnected()),this,SLOT(serverdisconnected())); } { 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.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; //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; string str1; cout<<"client:"; cin>>str1; client.write(qPrintable(str)); } void Client::serverdisconnected() { cout<<"\n--------------Server disconnected!!!! try again-------------------\n"; client.close(); exit(0); }