<?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[Problem mit einer Pipe]]></title><description><![CDATA[<p>Hallo allerseits!</p>
<p>Die Aufgabe besteht darin, einen Pipeserver zu erstellen, der seine beiden Client-Prozesse selbst startet und dann mit der Übertragung in die Pipe beginnt.<br />
Die Clients sollen die empfangenen Daten nur noch ausgeben.</p>
<p>Nun hab ich mehrere Schwierigkeiten.<br />
1.Die Client-Prozesse werden zwar gestartet, aber empfangen nur Müll.<br />
2.Der Server lässt sich nicht per Tastendruck beenden.</p>
<p>Ausserdem noch zwei Unklarheiten:<br />
Wird ein Verbindungscheck alla &quot;ConnectNamedPipe()&quot; benötigt, wenn der ChildProcess vom Server aus gestartet wird? Momentan ist er auskommetiert!<br />
Anders wäre es ja, wenn ich den Client manuell starte, dann sehe ich die Notwendigkeit.</p>
<p>Woher weiss der Client, wann keine Daten mehr in die Pipe geschoben werden.<br />
Das ganze an dwbytes(siehe Client-Code) zu messen funktioniert nicht.</p>
<p>Hoffe jemand hat dazu ein paar Vorschläge.<br />
Dank schonmal,<br />
smooth_op</p>
<pre><code class="language-cpp">// Pipe Server, der einen Teststring an die Clients weiter leitet

#include &lt;iostream&gt;	
#include &lt;windows.h&gt;
#include &lt;stdio.h&gt;		// printf()
#include &lt;conio.h&gt;		//_kbhit()

using namespace std;

int main()
{

	// Variablen
	LPCTSTR xPipe = &quot;\\\\.\\NewPipe&quot;;	// Name der Pipe
	DWORD BUFFER = 512;					// Datenpuffer
	LPCTSTR msg = &quot;TESTSTRING&quot;;			// zu verschickende Nachricht 
	DWORD dwbytes;						// (wrotebytes) Parameter fuer WriteFile()

	// Pipe erstellen
	HANDLE hPipe;
	hPipe = CreateNamedPipe(
							xPipe,
							PIPE_ACCESS_OUTBOUND,
							PIPE_WAIT,
							2,
							BUFFER,
							BUFFER,
							NMPWAIT_USE_DEFAULT_WAIT,
							NULL
							);

	// Client- Prozesse starten

	// Strukturen fuer 2 Clients
	STARTUPINFO si[2];
    PROCESS_INFORMATION pi[2];

	// Client 1
	// Struktur initialisieren
    ZeroMemory( &amp;si[0], sizeof(si[0]) );
    si[0].cb = sizeof(si[0]);
    ZeroMemory( &amp;pi[0], sizeof(pi[0]) );

	// Optionen fuer eigenes Konsolenfenster
	si[0].wShowWindow = SW_SHOW;
	si[0].dwFlags = STARTF_USESHOWWINDOW;

    // Start the child process. 
    if( !CreateProcess( NULL,   // No module name (use command line). 
        &quot;client1.exe&quot;,	  // Command line. 
        NULL,             // Process handle not inheritable. 
        NULL,             // Thread handle not inheritable. 
        FALSE,            // Set handle inheritance to FALSE. 
        CREATE_NEW_CONSOLE,  // Creation flags. 
        NULL,             // Use parent's environment block. 
        NULL,             // Use parent's starting directory. 
        &amp;si[0],              // Pointer to STARTUPINFO structure.
        &amp;pi[0] )             // Pointer to PROCESS_INFORMATION structure.
    ) 
    {
        printf( &quot;CreateProcess failed (%d).\n&quot;, GetLastError() );
        return -1;
    }

	// Client 2
	// Struktur initialisieren
    ZeroMemory( &amp;si[1], sizeof(si[1]) );
	si[1].cb = sizeof(si[1]);
    ZeroMemory( &amp;pi[1], sizeof(pi[1]) );

	// Optionen fuer eigenes Konsolenfenster
	si[1].wShowWindow = SW_SHOW;
	si[1].dwFlags = STARTF_USESHOWWINDOW;

    // Start the child process. 
    if( !CreateProcess( NULL,   // No module name (use command line). 
        &quot;client2.exe&quot;,	  // Command line. 
        NULL,             // Process handle not inheritable. 
        NULL,             // Thread handle not inheritable. 
        FALSE,            // Set handle inheritance to FALSE. 
        CREATE_NEW_CONSOLE, // creation flags. 
        NULL,             // Use parent's environment block. 
        NULL,             // Use parent's starting directory. 
		&amp;si[1],              // Pointer to STARTUPINFO structure.
        &amp;pi[1] )             // Pointer to PROCESS_INFORMATION structure.
    ) 
    {
        printf( &quot;CreateProcess failed (%d).\n&quot;, GetLastError() );
        return -1;
    }

	// Auf Clients pruefen
	BOOL fconnect(false);

		fconnect = ConnectNamedPipe(hPipe,NULL);
	//	if(fconnect)										
	//	{												
			cout &lt;&lt; &quot;Uebertragung in die Pipe beginnt&quot; &lt;&lt; endl;

			while(true)
			{
				// Nachricht in die Pipe schreiben
				WriteFile(hPipe,msg,strlen(msg),&amp;dwbytes,NULL);

				// Durch Tastendruck Ende einleiten
				if(_kbhit()) break;			
			}

	//	}

	//Ende

	// Alle Prozesse nacheinander beenden

	// Wait until child process exits.
    WaitForSingleObject( pi[0].hProcess, INFINITE );

    // Close process and thread handles. 
    CloseHandle( pi[0].hProcess );
    CloseHandle( pi[0].hThread );

	// Wait until child process exits.
    WaitForSingleObject( pi[1].hProcess, INFINITE );

    // Close process and thread handles. 
    CloseHandle( pi[1].hProcess );
    CloseHandle( pi[1].hThread );

	// Pipe trennen
	DisconnectNamedPipe(hPipe);

	return 0;
}
</code></pre>
<pre><code class="language-cpp">// Client1, empfaengt Daten von der Pipe

#include &lt;iostream&gt;
#include &lt;windows.h&gt;
#include &lt;stdio.h&gt;

using namespace std;

int main()
{

	// Variablen
	HANDLE hFile;						// HANDLE zur Pipe
	LPCTSTR xPipe = &quot;\\\\.\\NewPipe&quot;;	// Name der Pipe
	char recieve[100];					// char-array fuer empfangene Daten
	DWORD BUFFER = 512;					// Puffer
	DWORD dwbytes;						// Parameter fuer ReadFile()

	// auf Verbindung mit Server warten
	BOOL PipeReady(false);
	PipeReady = WaitNamedPipe(xPipe,NMPWAIT_WAIT_FOREVER);

	if(PipeReady)
	{
		// Pipe oeffnen 
		hFile  = CreateFile(
				   xPipe,				  // file to open
				   GENERIC_READ,          // open for reading
				   0,			          // share for reading
				   NULL,                  // default security
				   OPEN_EXISTING,         // existing file only
				   0,					  // normal file
				   NULL);                 // no attr. template

		if (hFile == INVALID_HANDLE_VALUE) 
		{ 
			printf(&quot;Could not open file (error %d)\n&quot;, GetLastError());
			return -1;
		}
	}

	cout &lt;&lt; &quot; Client beginnt mit der Ausgabe: &quot; &lt;&lt; endl;
	while(true)
	{
		// lesen und ausgeben
		ReadFile(hFile,recieve,BUFFER,&amp;dwbytes,NULL);

		cout &lt;&lt; recieve &lt;&lt; endl;
		//system(&quot;PAUSE&quot;);

		// Datenstrom ende?	
		//if (dwbytes == 0) break;
	}

	// HANDLE schliessen
	CloseHandle(hFile);

	return 0;
	}
</code></pre>
<p>Client 2 ist identisch zu Client 1</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/186033/problem-mit-einer-pipe</link><generator>RSS for Node</generator><lastBuildDate>Sun, 05 Jul 2026 11:01:22 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/186033.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 04 Jul 2007 10:37:20 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Problem mit einer Pipe on Wed, 04 Jul 2007 10:37:20 GMT]]></title><description><![CDATA[<p>Hallo allerseits!</p>
<p>Die Aufgabe besteht darin, einen Pipeserver zu erstellen, der seine beiden Client-Prozesse selbst startet und dann mit der Übertragung in die Pipe beginnt.<br />
Die Clients sollen die empfangenen Daten nur noch ausgeben.</p>
<p>Nun hab ich mehrere Schwierigkeiten.<br />
1.Die Client-Prozesse werden zwar gestartet, aber empfangen nur Müll.<br />
2.Der Server lässt sich nicht per Tastendruck beenden.</p>
<p>Ausserdem noch zwei Unklarheiten:<br />
Wird ein Verbindungscheck alla &quot;ConnectNamedPipe()&quot; benötigt, wenn der ChildProcess vom Server aus gestartet wird? Momentan ist er auskommetiert!<br />
Anders wäre es ja, wenn ich den Client manuell starte, dann sehe ich die Notwendigkeit.</p>
<p>Woher weiss der Client, wann keine Daten mehr in die Pipe geschoben werden.<br />
Das ganze an dwbytes(siehe Client-Code) zu messen funktioniert nicht.</p>
<p>Hoffe jemand hat dazu ein paar Vorschläge.<br />
Dank schonmal,<br />
smooth_op</p>
<pre><code class="language-cpp">// Pipe Server, der einen Teststring an die Clients weiter leitet

#include &lt;iostream&gt;	
#include &lt;windows.h&gt;
#include &lt;stdio.h&gt;		// printf()
#include &lt;conio.h&gt;		//_kbhit()

using namespace std;

int main()
{

	// Variablen
	LPCTSTR xPipe = &quot;\\\\.\\NewPipe&quot;;	// Name der Pipe
	DWORD BUFFER = 512;					// Datenpuffer
	LPCTSTR msg = &quot;TESTSTRING&quot;;			// zu verschickende Nachricht 
	DWORD dwbytes;						// (wrotebytes) Parameter fuer WriteFile()

	// Pipe erstellen
	HANDLE hPipe;
	hPipe = CreateNamedPipe(
							xPipe,
							PIPE_ACCESS_OUTBOUND,
							PIPE_WAIT,
							2,
							BUFFER,
							BUFFER,
							NMPWAIT_USE_DEFAULT_WAIT,
							NULL
							);

	// Client- Prozesse starten

	// Strukturen fuer 2 Clients
	STARTUPINFO si[2];
    PROCESS_INFORMATION pi[2];

	// Client 1
	// Struktur initialisieren
    ZeroMemory( &amp;si[0], sizeof(si[0]) );
    si[0].cb = sizeof(si[0]);
    ZeroMemory( &amp;pi[0], sizeof(pi[0]) );

	// Optionen fuer eigenes Konsolenfenster
	si[0].wShowWindow = SW_SHOW;
	si[0].dwFlags = STARTF_USESHOWWINDOW;

    // Start the child process. 
    if( !CreateProcess( NULL,   // No module name (use command line). 
        &quot;client1.exe&quot;,	  // Command line. 
        NULL,             // Process handle not inheritable. 
        NULL,             // Thread handle not inheritable. 
        FALSE,            // Set handle inheritance to FALSE. 
        CREATE_NEW_CONSOLE,  // Creation flags. 
        NULL,             // Use parent's environment block. 
        NULL,             // Use parent's starting directory. 
        &amp;si[0],              // Pointer to STARTUPINFO structure.
        &amp;pi[0] )             // Pointer to PROCESS_INFORMATION structure.
    ) 
    {
        printf( &quot;CreateProcess failed (%d).\n&quot;, GetLastError() );
        return -1;
    }

	// Client 2
	// Struktur initialisieren
    ZeroMemory( &amp;si[1], sizeof(si[1]) );
	si[1].cb = sizeof(si[1]);
    ZeroMemory( &amp;pi[1], sizeof(pi[1]) );

	// Optionen fuer eigenes Konsolenfenster
	si[1].wShowWindow = SW_SHOW;
	si[1].dwFlags = STARTF_USESHOWWINDOW;

    // Start the child process. 
    if( !CreateProcess( NULL,   // No module name (use command line). 
        &quot;client2.exe&quot;,	  // Command line. 
        NULL,             // Process handle not inheritable. 
        NULL,             // Thread handle not inheritable. 
        FALSE,            // Set handle inheritance to FALSE. 
        CREATE_NEW_CONSOLE, // creation flags. 
        NULL,             // Use parent's environment block. 
        NULL,             // Use parent's starting directory. 
		&amp;si[1],              // Pointer to STARTUPINFO structure.
        &amp;pi[1] )             // Pointer to PROCESS_INFORMATION structure.
    ) 
    {
        printf( &quot;CreateProcess failed (%d).\n&quot;, GetLastError() );
        return -1;
    }

	// Auf Clients pruefen
	BOOL fconnect(false);

		fconnect = ConnectNamedPipe(hPipe,NULL);
	//	if(fconnect)										
	//	{												
			cout &lt;&lt; &quot;Uebertragung in die Pipe beginnt&quot; &lt;&lt; endl;

			while(true)
			{
				// Nachricht in die Pipe schreiben
				WriteFile(hPipe,msg,strlen(msg),&amp;dwbytes,NULL);

				// Durch Tastendruck Ende einleiten
				if(_kbhit()) break;			
			}

	//	}

	//Ende

	// Alle Prozesse nacheinander beenden

	// Wait until child process exits.
    WaitForSingleObject( pi[0].hProcess, INFINITE );

    // Close process and thread handles. 
    CloseHandle( pi[0].hProcess );
    CloseHandle( pi[0].hThread );

	// Wait until child process exits.
    WaitForSingleObject( pi[1].hProcess, INFINITE );

    // Close process and thread handles. 
    CloseHandle( pi[1].hProcess );
    CloseHandle( pi[1].hThread );

	// Pipe trennen
	DisconnectNamedPipe(hPipe);

	return 0;
}
</code></pre>
<pre><code class="language-cpp">// Client1, empfaengt Daten von der Pipe

#include &lt;iostream&gt;
#include &lt;windows.h&gt;
#include &lt;stdio.h&gt;

using namespace std;

int main()
{

	// Variablen
	HANDLE hFile;						// HANDLE zur Pipe
	LPCTSTR xPipe = &quot;\\\\.\\NewPipe&quot;;	// Name der Pipe
	char recieve[100];					// char-array fuer empfangene Daten
	DWORD BUFFER = 512;					// Puffer
	DWORD dwbytes;						// Parameter fuer ReadFile()

	// auf Verbindung mit Server warten
	BOOL PipeReady(false);
	PipeReady = WaitNamedPipe(xPipe,NMPWAIT_WAIT_FOREVER);

	if(PipeReady)
	{
		// Pipe oeffnen 
		hFile  = CreateFile(
				   xPipe,				  // file to open
				   GENERIC_READ,          // open for reading
				   0,			          // share for reading
				   NULL,                  // default security
				   OPEN_EXISTING,         // existing file only
				   0,					  // normal file
				   NULL);                 // no attr. template

		if (hFile == INVALID_HANDLE_VALUE) 
		{ 
			printf(&quot;Could not open file (error %d)\n&quot;, GetLastError());
			return -1;
		}
	}

	cout &lt;&lt; &quot; Client beginnt mit der Ausgabe: &quot; &lt;&lt; endl;
	while(true)
	{
		// lesen und ausgeben
		ReadFile(hFile,recieve,BUFFER,&amp;dwbytes,NULL);

		cout &lt;&lt; recieve &lt;&lt; endl;
		//system(&quot;PAUSE&quot;);

		// Datenstrom ende?	
		//if (dwbytes == 0) break;
	}

	// HANDLE schliessen
	CloseHandle(hFile);

	return 0;
	}
</code></pre>
<p>Client 2 ist identisch zu Client 1</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1318080</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1318080</guid><dc:creator><![CDATA[smooth_op]]></dc:creator><pubDate>Wed, 04 Jul 2007 10:37:20 GMT</pubDate></item><item><title><![CDATA[Reply to Problem mit einer Pipe on Wed, 04 Jul 2007 15:03:13 GMT]]></title><description><![CDATA[<p>ok, zwei neue Erkentnisse!</p>
<p>Die Pipe hatte eine falschen Pfad, richtig müsste es heissen:</p>
<pre><code class="language-cpp">LPTSTR xPipe = &quot;\\\\.\\pipe\\NEWPIPE&quot;;
</code></pre>
<p>Dank eines Tipps von einem Kommilitonen, hab' ich die zu verschickenden und empfangenden Daten jeweils mittels einer struct realisiert und WriteFile/ReadFile, sowie das cout beim Client darauf verwiesen (Pointer).<br />
Damit funktioniert es, wenn auch nur mit integers.<br />
Womit ich aber immer noch nicht verstehe, was bei meiner ursprünglichen Version schief läuft.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1318338</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1318338</guid><dc:creator><![CDATA[smooth_op]]></dc:creator><pubDate>Wed, 04 Jul 2007 15:03:13 GMT</pubDate></item></channel></rss>