473,320 Members | 1,848 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Named Pipe hangs

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 );

Nov 8 '05 #1
1 7706
Ac*****@gmail.com 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
Nov 8 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Rajarshi Guha | last post by:
Hi, I'm having a little trouble when I read from a named pipe. I create a pipe by os.mkfifo('/tmp/mypipe') and then open it for reading with fin = open('/tmp/mypipe','r+')
6
by: Srijit Kumar Bhadra | last post by:
Hello, Here is an example of Multithreaded Pipe Server and Client using the excellent ctypes library (Windows). Reference - MSDN:...
5
by: richard | last post by:
I have a simple test to pass information from a client to a server using named pipe. what I really want is: when I type a line on the client, the server will output the line immediately. but to my...
7
by: Greg | last post by:
I am trying to implement the UNIX pipe command using C but with the "->" operator. Everything works fine with 1 pipe, but when I try to use 2 or more, it hangs up when reading the pipe_in...
4
by: Ken Allen | last post by:
Is there any built-in facility for handling named pipes in C#/.Net, or must one use unsafe code to access the WIN32 API directly? There exists some code that uses named pipes heavily and there...
3
by: EricR | last post by:
I am trying to use .NET to "tap into" a named pipe created by a non .NET 3rd party application. Specifically, the application is a table loading utility. It opens a named pipe and waits for input....
0
by: olaf.dietsche | last post by:
Hi, The system is Windows XP and DB2 v8.1.7. I'm trying to load a text file via named pipe into a table. I have two programs: the first program creates the named pipe, waits for a client...
14
by: Rochester | last post by:
Hi, I just found out that the general open file mechanism doesn't work for named pipes (fifo). Say I wrote something like this and it simply hangs python: #!/usr/bin/python import os
7
by: andrewb | last post by:
Hi all, Having some trouble using named pipes and Visual Basic .NET and would appreciate and help you could offer. Essentially I am trying to develop a simple client/server application that...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.