<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Interprozesskommunikation zwischen boost und Qt]]></title><description><![CDATA[<p>Hallo allerseits,</p>
<p>ich befasse mich zur Zeit mit dem Thema IPC über shared memory.<br />
Um einen Einstieg zu finden, habe ich mir auch einfache client/server Programme mit boost und Qt 4.8 Untersützung geschrieben.<br />
Die funktionieren soweit auch, solange das darunterliegende Framework gleich ist.<br />
Also boost client &lt;-&gt; boost server<br />
und Qt client &lt;-&gt; Qt server<br />
funktionieren einwandfrei.</p>
<p>Nun möchte ich meinen boost server mit meinem Qt client kommunizieren lassen. Und an diesem Punkt scheitere ich.<br />
Was hab ich übersehen, bzw. was muss ich am Quellcode ändern, damit das funktioniert?</p>
<p>Grüße,<br />
Vic</p>
<p>Anhang:<br />
Boost server:</p>
<pre><code class="language-cpp">#include &lt;boost/interprocess/shared_memory_object.hpp&gt;
#include &lt;boost/interprocess/windows_shared_memory.hpp&gt; 
#include &lt;boost/interprocess/mapped_region.hpp&gt;
#include &lt;boost/interprocess/sync/named_mutex.hpp&gt;  
#include &lt;iostream&gt; 
#include &lt;string&gt;

int main(int argc, char** argv) 
{
	const std::string key = &quot;SharedMemoryTest&quot;;
	boost::interprocess::shared_memory_object shdmem(boost::interprocess::open_or_create, key.c_str(), boost::interprocess::read_write); 
	//boost::interprocess::windows_shared_memory shdmem(boost::interprocess::open_or_create, &quot;Shared Memory Test&quot;, boost::interprocess::read_write); 
	shdmem.truncate(1024); 
	boost::interprocess::mapped_region region(shdmem, boost::interprocess::read_write); 
	boost::interprocess::named_mutex named_mtx(boost::interprocess::open_or_create, &quot;mtx&quot;);
	const std::string content = &quot;This is boost content&quot;;

	char* pOrigin = static_cast&lt;char*&gt;(region.get_address());
	strcpy(pOrigin, content.c_str()); 

	system(&quot;pause&quot;);

	bool removed = boost::interprocess::shared_memory_object::remove(key.c_str());		
	return 0;
}
</code></pre>
<p>Boost client:</p>
<pre><code class="language-cpp">#include &lt;boost/interprocess/shared_memory_object.hpp&gt; 
#include &lt;boost/interprocess/mapped_region.hpp&gt;
#include &lt;boost/interprocess/sync/named_mutex.hpp&gt;  
#include &lt;iostream&gt; 
#include &lt;string&gt;

int main(int argc, char** argv) 
{
	try {
		const std::string key = &quot;SharedMemoryTest&quot;;
		boost::interprocess::shared_memory_object shdmem(boost::interprocess::open_only, key.c_str(), boost::interprocess::read_write); 
		boost::interprocess::mapped_region region(shdmem, boost::interprocess::read_write);    
		boost::interprocess::named_mutex named_mtx(boost::interprocess::open_or_create, &quot;mtx&quot;);

		char* pOrigin = static_cast&lt;char*&gt;(region.get_address());

		std::cout &lt;&lt; &quot;Shared Memory content:&quot; &lt;&lt; std::endl;
		std::cout &lt;&lt; pOrigin;
	} catch (boost::interprocess::interprocess_exception &amp;e) {
		std::cerr &lt;&lt; e.what() &lt;&lt; std::endl;
	}
	return 0;
}
</code></pre>
<p>QT server:</p>
<pre><code class="language-cpp">#include &lt;QtCore/QCoreApplication&gt;
#include &lt;QtCore/QSharedMemory&gt;
#include &lt;QtCore/QBuffer&gt;
#include &lt;QtCore/QDataStream&gt;
#include &lt;iostream&gt;

int main(int argc, char *argv[])
{
	QCoreApplication a(argc, argv);

	QString key = QString::fromStdString(&quot;SharedMemoryTest&quot;);
	QSharedMemory sharedMemory(key);
	sharedMemory.setNativeKey(key);
	QBuffer buffer;

	buffer.open(QBuffer::ReadWrite);
	QDataStream out(&amp;buffer);

	out &lt;&lt; &quot;This is QT content&quot;;
	int size = buffer.size();

	if (!sharedMemory.create(size)) {
         std::cerr &lt;&lt; &quot;Unable to create shared memory&quot; &lt;&lt; std::endl;
    } else {
		sharedMemory.lock();
		char *pSharedMemoryOrigin = (char*)sharedMemory.data();
		const char *pDataToShare = buffer.data().data();

		memcpy(pSharedMemoryOrigin, pDataToShare, qMin(sharedMemory.size(), size));
		sharedMemory.unlock();
	}

	return a.exec();
}
</code></pre>
<p>QT client:</p>
<pre><code class="language-cpp">#include &lt;QtCore/QCoreApplication&gt;
#include &lt;QtCore/QSharedMemory&gt;
#include &lt;QtCore/QBuffer&gt;
#include &lt;QtCore/QDataStream&gt;
#include &lt;iostream&gt;

int main(int argc, char *argv[])
{
	QCoreApplication a(argc, argv);

	QString key = QString::fromStdString(&quot;SharedMemoryTest&quot;);
	QSharedMemory sharedMemory;
	sharedMemory.setNativeKey(key);
	bool attached = sharedMemory.attach(QSharedMemory::ReadOnly);
	if (!attached) {
		std::cerr &lt;&lt; &quot;Unable to attach shared memory&quot; &lt;&lt; std::endl;
		std::cerr &lt;&lt; sharedMemory.errorString().toStdString();
	} else {
		QBuffer buffer;
		QDataStream in(&amp;buffer);
		char* pContent = 0;

		sharedMemory.lock();
		int sharedMemSize = sharedMemory.size();
		pContent = new char[sharedMemSize];
		buffer.setData((char*)sharedMemory.constData(), sharedMemSize);
		buffer.open(QBuffer::ReadOnly);
		in &gt;&gt; pContent;
		sharedMemory.unlock();

		std::cout &lt;&lt; &quot;Shared memory content:&quot; &lt;&lt; std::endl;
		std::cout &lt;&lt; pContent;

		delete pContent;
	}

	return a.exec();
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/topic/308705/interprozesskommunikation-zwischen-boost-und-qt</link><generator>RSS for Node</generator><lastBuildDate>Thu, 06 Aug 2026 17:29:41 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/308705.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 03 Oct 2012 09:44:42 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Interprozesskommunikation zwischen boost und Qt on Wed, 03 Oct 2012 09:45:43 GMT]]></title><description><![CDATA[<p>Hallo allerseits,</p>
<p>ich befasse mich zur Zeit mit dem Thema IPC über shared memory.<br />
Um einen Einstieg zu finden, habe ich mir auch einfache client/server Programme mit boost und Qt 4.8 Untersützung geschrieben.<br />
Die funktionieren soweit auch, solange das darunterliegende Framework gleich ist.<br />
Also boost client &lt;-&gt; boost server<br />
und Qt client &lt;-&gt; Qt server<br />
funktionieren einwandfrei.</p>
<p>Nun möchte ich meinen boost server mit meinem Qt client kommunizieren lassen. Und an diesem Punkt scheitere ich.<br />
Was hab ich übersehen, bzw. was muss ich am Quellcode ändern, damit das funktioniert?</p>
<p>Grüße,<br />
Vic</p>
<p>Anhang:<br />
Boost server:</p>
<pre><code class="language-cpp">#include &lt;boost/interprocess/shared_memory_object.hpp&gt;
#include &lt;boost/interprocess/windows_shared_memory.hpp&gt; 
#include &lt;boost/interprocess/mapped_region.hpp&gt;
#include &lt;boost/interprocess/sync/named_mutex.hpp&gt;  
#include &lt;iostream&gt; 
#include &lt;string&gt;

int main(int argc, char** argv) 
{
	const std::string key = &quot;SharedMemoryTest&quot;;
	boost::interprocess::shared_memory_object shdmem(boost::interprocess::open_or_create, key.c_str(), boost::interprocess::read_write); 
	//boost::interprocess::windows_shared_memory shdmem(boost::interprocess::open_or_create, &quot;Shared Memory Test&quot;, boost::interprocess::read_write); 
	shdmem.truncate(1024); 
	boost::interprocess::mapped_region region(shdmem, boost::interprocess::read_write); 
	boost::interprocess::named_mutex named_mtx(boost::interprocess::open_or_create, &quot;mtx&quot;);
	const std::string content = &quot;This is boost content&quot;;

	char* pOrigin = static_cast&lt;char*&gt;(region.get_address());
	strcpy(pOrigin, content.c_str()); 

	system(&quot;pause&quot;);

	bool removed = boost::interprocess::shared_memory_object::remove(key.c_str());		
	return 0;
}
</code></pre>
<p>Boost client:</p>
<pre><code class="language-cpp">#include &lt;boost/interprocess/shared_memory_object.hpp&gt; 
#include &lt;boost/interprocess/mapped_region.hpp&gt;
#include &lt;boost/interprocess/sync/named_mutex.hpp&gt;  
#include &lt;iostream&gt; 
#include &lt;string&gt;

int main(int argc, char** argv) 
{
	try {
		const std::string key = &quot;SharedMemoryTest&quot;;
		boost::interprocess::shared_memory_object shdmem(boost::interprocess::open_only, key.c_str(), boost::interprocess::read_write); 
		boost::interprocess::mapped_region region(shdmem, boost::interprocess::read_write);    
		boost::interprocess::named_mutex named_mtx(boost::interprocess::open_or_create, &quot;mtx&quot;);

		char* pOrigin = static_cast&lt;char*&gt;(region.get_address());

		std::cout &lt;&lt; &quot;Shared Memory content:&quot; &lt;&lt; std::endl;
		std::cout &lt;&lt; pOrigin;
	} catch (boost::interprocess::interprocess_exception &amp;e) {
		std::cerr &lt;&lt; e.what() &lt;&lt; std::endl;
	}
	return 0;
}
</code></pre>
<p>QT server:</p>
<pre><code class="language-cpp">#include &lt;QtCore/QCoreApplication&gt;
#include &lt;QtCore/QSharedMemory&gt;
#include &lt;QtCore/QBuffer&gt;
#include &lt;QtCore/QDataStream&gt;
#include &lt;iostream&gt;

int main(int argc, char *argv[])
{
	QCoreApplication a(argc, argv);

	QString key = QString::fromStdString(&quot;SharedMemoryTest&quot;);
	QSharedMemory sharedMemory(key);
	sharedMemory.setNativeKey(key);
	QBuffer buffer;

	buffer.open(QBuffer::ReadWrite);
	QDataStream out(&amp;buffer);

	out &lt;&lt; &quot;This is QT content&quot;;
	int size = buffer.size();

	if (!sharedMemory.create(size)) {
         std::cerr &lt;&lt; &quot;Unable to create shared memory&quot; &lt;&lt; std::endl;
    } else {
		sharedMemory.lock();
		char *pSharedMemoryOrigin = (char*)sharedMemory.data();
		const char *pDataToShare = buffer.data().data();

		memcpy(pSharedMemoryOrigin, pDataToShare, qMin(sharedMemory.size(), size));
		sharedMemory.unlock();
	}

	return a.exec();
}
</code></pre>
<p>QT client:</p>
<pre><code class="language-cpp">#include &lt;QtCore/QCoreApplication&gt;
#include &lt;QtCore/QSharedMemory&gt;
#include &lt;QtCore/QBuffer&gt;
#include &lt;QtCore/QDataStream&gt;
#include &lt;iostream&gt;

int main(int argc, char *argv[])
{
	QCoreApplication a(argc, argv);

	QString key = QString::fromStdString(&quot;SharedMemoryTest&quot;);
	QSharedMemory sharedMemory;
	sharedMemory.setNativeKey(key);
	bool attached = sharedMemory.attach(QSharedMemory::ReadOnly);
	if (!attached) {
		std::cerr &lt;&lt; &quot;Unable to attach shared memory&quot; &lt;&lt; std::endl;
		std::cerr &lt;&lt; sharedMemory.errorString().toStdString();
	} else {
		QBuffer buffer;
		QDataStream in(&amp;buffer);
		char* pContent = 0;

		sharedMemory.lock();
		int sharedMemSize = sharedMemory.size();
		pContent = new char[sharedMemSize];
		buffer.setData((char*)sharedMemory.constData(), sharedMemSize);
		buffer.open(QBuffer::ReadOnly);
		in &gt;&gt; pContent;
		sharedMemory.unlock();

		std::cout &lt;&lt; &quot;Shared memory content:&quot; &lt;&lt; std::endl;
		std::cout &lt;&lt; pContent;

		delete pContent;
	}

	return a.exec();
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2256687</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2256687</guid><dc:creator><![CDATA[_Vic_]]></dc:creator><pubDate>Wed, 03 Oct 2012 09:45:43 GMT</pubDate></item><item><title><![CDATA[Reply to Interprozesskommunikation zwischen boost und Qt on Wed, 03 Oct 2012 10:40:02 GMT]]></title><description><![CDATA[<p>Glaubst du jetzt ernsthaft, dass hier jemand deinen Code ausprobiert, um herrauszufinden, <strong>was</strong> nicht &quot;funktioniert&quot;?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2256702</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2256702</guid><dc:creator><![CDATA[manni66]]></dc:creator><pubDate>Wed, 03 Oct 2012 10:40:02 GMT</pubDate></item><item><title><![CDATA[Reply to Interprozesskommunikation zwischen boost und Qt on Wed, 03 Oct 2012 11:28:30 GMT]]></title><description><![CDATA[<p>Ich glaube ernsthaft, dass jemand auf Grund seiner Erfahrungswerte typische Fehler erkennen und darauf hinweisen kann, da es sich hier nur um Minimalbeispiele handelt.</p>
<p>Und da es sich um lauffähigen Code handelt, können auch andere Neulinge von diesen Beispielen lernen.</p>
<p>Wenn du dich nicht mit diesem Thema beschäftigen möchtest, dann sei es so. Aber solche unqualifizierten Kommentare kannst du dir sparen.<br />
Und jetzt zurück zum Thema.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2256713</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2256713</guid><dc:creator><![CDATA[_Vic_]]></dc:creator><pubDate>Wed, 03 Oct 2012 11:28:30 GMT</pubDate></item></channel></rss>