Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Named Pipe hangs

Reply
Thread Tools

Named Pipe hangs

 
 
AckMike@gmail.com
Guest
Posts: n/a
 
      11-08-2005
I am trying to write client and server programs that communicate with
named pipes. There is one multithreaded server that handles all of the

requests and multiple multithread clients that make the requests. The
problem is that the client hangs at the WriteFile request when running
under load. All the processes are running on the same box. I am
wondering if there is something I am doing wrong fundamentally.

One thing I am doing that is questionable is opening the pipe on the
server side in Overlapped mode and but not using overlapped mode on the

client side. I don't know if this would cause any problems since they
are on the same box.

If I change the server to open the pipe with a inital pipebuffer of 120

for read and write buffers the client then hangs at the readfile().
Any ideas?


SERVER MAIN:
----------------------
HANDLE hPipe = INVALID_HANDLE_VALUE;
HANDLE hEvents[2] = {NULL, NULL};
HANDLE hServerStopEvent = CreateEvent(NULL,TRUE,FALSE,NULL);
hEvents[0] = hServerStopEvent;
while (1)
{
OVERLAPPED *pOS = NULL;
hEvents[1] = CreateEvent(
NULL, // no security attributes
TRUE, // manual reset event
FALSE, // not-signalled
NULL); // no name
// Create the pipe
hPipe = CreateNamedPipe(
szPipeName,
PIPE_ACCESS_DUPLEX |
FILE_FLAG_OVERLAPPED,
PIPE_TYPE_MESSAGE |
PIPE_READMODE_MESSAGE |
PIPE_WAIT,
PIPE_UNLIMITED_INSTANCES,
0,
0,
NMPWAIT_USE_DEFAULT_WAIT,
&sa); // security attributes
pOS = (OVERLAPPED *) calloc (1, sizeof(OVERLAPPED) );
pOS->hEvent = hEvents[1];
ResetEvent( hEvents[1] );
// wait for a connection...
bool bError = false;
bConnected = ConnectNamedPipe(hPipe, pOS);
if (!bConnected)
{
if (GetLastError() == ERROR_IO_PENDING)
{
// Wait for the data to be ready
DWORD dwWait = WaitForMultipleObjects( 2, hEvents, FALSE,
INFINITE );
if ( dwWait != WAIT_OBJECT_0+1 )
break; // Server stop or error
}
else
bError=true;
}
if (!bError)
{
//Create a thread to handle the request
CloseHandle(pOS->hEvent);
free(pOS);
}
}


SERVER WORKER THREAD:
----------------------
// create a Overlapped structure for read
OVERLAPPED *pOS = NULL;
HANDLE hOS = INVALID_HANDLE_VALUE;
// init the overlapped structure
hOS = CreateEvent(NULL,TRUE,FALSE,NULL);
pOS = (OVERLAPPED *) calloc (1, sizeof(OVERLAPPED) );
pOS->hEvent = hOS;
ResetEvent( hOS );
// Get the size of the message
PeekNamedPipe(hPipe,NULL,0,NULL,&dwTotalBytesAvail ,&dwBytesLeftInMessage);


// create a buffer based on the size from peek
bResult = ReadFile( hPipe,szBuffer,dwTotalBytesAvail,&dwBytesRead,pOS) ;

if ( !bResult )
{
if (ERROR_IO_PENDING == GetLastError())
{
DWORD dwNumBytesTransferred = 0;
// The data is not ready yet, wait for the operation to be
complete
GetOverlappedResult(hPipe,pOS, &dwNumBytesTransferred,true);
ResetEvent(pOS->hEvent);
}
}
CloseHandle(pOS->hEvent);
free(pOS);
pOS=NULL;

// Do stuff with the message and create a response

// create a Overlapped structure for write
*pOS = NULL;
hOS = INVALID_HANDLE_VALUE;
// init the overlapped structure
hOS = CreateEvent(NULL,TRUE,FALSE,NULL);
pOS = (OVERLAPPED *) calloc (1, sizeof(OVERLAPPED) );
pOS->hEvent = hOS;
ResetEvent( hOS );
// Send the response
bResult = WriteFile( this->hPipe,
szOutMsg,
iOutMsgSize,
&dwBytesWritten,
pOverlapped);
if (!bResult)
{
int iWriteFileError = GetLastError();
if (ERROR_IO_PENDING == iWriteFileError)
{
DWORD dwNumBytesTransferred = 0;
// The data is not ready yet, wait for the operation to be
complete

GetOverlappedResult(hPipe,pOverlapped,&dwNumBytesT ransferred,true);
ResetEvent(pOS->hEvent);
bResult = true;
}
}


CloseHandle(pOS->hEvent);
free(pOS);
pOS=NULL;

// Clean up
FlushFileBuffers(hPipe);
DisconnectNamedPipe(hPipe);
CloseHandle( hPipe );

----------------------
CLIENT THREAD:
DWORD dwPipeMode = PIPE_READMODE_MESSAGE;
hPipe = CreateFile( szPipeName,
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL, OPEN_EXISTING, 0, NULL );
if (hPipe == INVALID_HANDLE_VALUE)
WaitNamedPipe(szPipeName, 20000); // Then try again (not
shown here)
SetNamedPipeHandleState( this->hPipe, &dwPipeMode,NULL, NULL );
// Write the message
bResult = WriteFile( this->hPipe,
szOutMsg,
iOutMsgSize,
&dwBytesWritten,
NULL);
// Read the answer

bResult = PeekNamedPipe
(hPipe,NULL,0,NULL,&dwTotalBytesAvail,&dwBytesLeft InMessage);
if (dwTotalBytesAvail == 0)
{
// We don't know how big to make the buffer so default to 120
// since that is bigger than twice the size of
// most messages in testing.
dwTotalBytesAvail = 120;
}
ReadFile( hPipe, // file to read from
szBuffer, // address of input
buffer
dwTotalBytesAvail, // number of bytes to read
&dwBytesRead, // number of bytes read
NULL); // not overlapped
CloseHandle( hPipe );

 
Reply With Quote
 
 
 
 
John Ratliff
Guest
Posts: n/a
 
      11-08-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> I am trying to write client and server programs that communicate with
> named pipes. There is one multithreaded server that handles all of the
>
> requests and multiple multithread clients that make the requests. The
> problem is that the client hangs at the WriteFile request when running
> under load. All the processes are running on the same box. I am
> wondering if there is something I am doing wrong fundamentally.
>
> One thing I am doing that is questionable is opening the pipe on the
> server side in Overlapped mode and but not using overlapped mode on the
>
> client side. I don't know if this would cause any problems since they
> are on the same box.
>
> If I change the server to open the pipe with a inital pipebuffer of 120
>
> for read and write buffers the client then hangs at the readfile().
> Any ideas?
>


This is a better question for a OS newsgroup. Something in the area of
linux, unix, or posix. There are no "pipes" as defined by Standard C++.

See http://www.parashift.com/c++-faq-lite/ for more information about
comp.lang.c++.

--John Ratliff
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
named pipe in java Bruintje Beer Java 2 09-27-2005 09:37 PM
[named pipe] i wanna know about validate of pipe handle of client lee, wonsun C++ 1 11-02-2004 04:29 AM
c++ named pipe example rajnish C++ 5 10-25-2004 08:29 AM
Why does IO::Pipe::END generate an EXCEPT pipe message? lvirden@gmail.com Perl Misc 1 06-02-2004 02:17 PM
Clear hangs up - & hangs up - & hangs up Sue Bilstein NZ Computing 26 03-07-2004 01:33 AM



Advertisments