Connecting to a named pipe after disconnection from the client

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hello,
I have a server that creates a named pipe and redirects the stdout to the named pipe. the code is here:
<pre class="prettyprint int main(int argc, char* argv[])
{
createPipe(pipeName);

/*For testing purpose*/
for(Buffer_out=0;; Buffer_out++)
{
printf("%d: sending printf to pipen",Buffer_out);
//fflush(stdout);
}

CloseHandle(hPipe);
return 0;
}

void createPipe(LPCTSTR pipeName)
{
hPipe = CreateNamedPipe(pipeName, //this machine
PIPE_ACCESS_OUTBOUND,
PIPE_TYPE_BYTE | PIPE_WAIT,
PIPE_UNLIMITED_INSTANCES, 0, sizeof(Buffer_out),
1000, // timeout in millseconds
NULL); // security descriptor
if(INVALID_HANDLE_VALUE == hPipe)
{
printf("Server Pipe not createdn");
exit(0);
}
else
printf("Successful in creating server pipen");

/*Copy stdout to old, 0: stdin, 1:stdout, 2:stderr*/
old = _dup(1); // this will be used for coping back the stdout, stdout = old;
if( old == -1 )
{
perror( "_dup( 1 ) failure" );
exit( 1 );
}
/*Change HANDLE to FILE*/
stream = _open_osfhandle((long)hPipe, 0);
if(stream != -1)
test = _fdopen(stream, "wt");
/*END of HANDLE to FILE*/
/*Connect stdout to test, test is connected to namedpipe */
if(_dup2(_fileno( test), 1 ) == -1 )
{
perror( "Cant _dup2 stdout" );
exit( 1 );
}

/*Remove the buffer for stdout, MUST do else nothing will go to the pipe */
setvbuf(stdout, NULL, _IONBF, 0);
/*After here all stdouts will be directed to the named pipe*/
}[/code]
<br/>
Now the client
<pre class="prettyprint hPipe = CreateFile(pipeName,
GENERIC_READ, FILE_SHARE_READ ,
NULL, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
(HANDLE) NULL);
if (GetLastError()==0)
{
printf("Handle Found Successfully.n");
}
else if(GetLastError()==231)
{
printf("Instances are Busy; Program will Wait 2 Secs and Then Try again.n");
WaitNamedPipe(pipeName, 2000) ;
}
else
{
printf("Error: %dn",GetLastError());
getchar();
exit(1);
}


while(hPipe) {
ReadFile(hPipe,(LPVOID) &buf, (DWORD) sizeof(buf), (LPDWORD) &count, (LPOVERLAPPED) NULL);
printf("%c", buf);

}
[/code]
<br/>
the first time I connect everything works great, but if I close the client and start it again I will first get the "
<pre class="prettyprint" style="font-size:12px Instances are Busy;[/code]
and after 2 seconds I will get some characters printed on my screen.
Now in this thread
http://social.msdn.microsoft.com/Forums/en-US/vcgeneral/thread/7e5f27a7-0fe6-4c78-b797-1ce33f512109 http://social.msdn.microsoft.com/Forums/en-US/vcgeneral/thread/7e5f27a7-0fe6-4c78-b797-1ce33f512109
The say that the server should use <span style="color:#333333; font-family:Segoe UI,Lucida Grande,Verdana,Arial,Helvetica,sans-serif; font-size:13px; line-height:16px; text-align:left DisconnectNamedPipe() but My question is where should I use
it and is there any other way?
<span style="color:#333333; font-family:Segoe UI,Lucida Grande,Verdana,Arial,Helvetica,sans-serif; font-size:13px; line-height:16px; text-align:left
<span style="color:#333333; font-family:Segoe UI,Lucida Grande,Verdana,Arial,Helvetica,sans-serif; font-size:13px; line-height:16px; text-align:left thanks,
<span style="color:#333333; font-family:Segoe UI,Lucida Grande,Verdana,Arial,Helvetica,sans-serif; font-size:13px; line-height:16px; text-align:left Ehsan http://social.msdn.microsoft.com/Forums/en-US/vcgeneral/thread/7e5f27a7-0fe6-4c78-b797-1ce33f512109
http://social.msdn.microsoft.com/Forums/en-US/vcgeneral/thread/8f97660c-48a7-46aa-bb0e-27b53e6a89c7

View the full article
 
Back
Top