How to solve ‘ReadFile hangs’ for named pipe client/server – sample working code for named pipe
ReadFile hangs or get stuck idefintiely in named pipe client or server…
What’s the cause?
If ReadFile gets stuck in named pipe client, it means the Named pipe Server has not written anything and vice-versa. Remember if a named pipe client has written some data to pipe, it can only be read by the named pipe server. The same client or any other instance of named pipe client can not read (using ReadFile) that content.
Similarly for WriteFile, if Server wrote something, then it can only be read by a named pipe client and vice-versa. If Server does a WriteFile and then it calls ReadFile while the content is not yet read by a client then the call get stuck.
Sample named pipe (client/server in one program) working code for your reference:
#include "stdafx.h"
#define BUFSIZE 512
const WCHAR NAMED_PIPE[]=L"\\\\.\\pipe\\mynamedpipe";
int _tmain(int argc, TCHAR *argv[])
{
HANDLE hPipe;
LPTSTR lpvMessage=TEXT("Default message from client.");
TCHAR chBuf[BUFSIZE];
BOOL fSuccess = FALSE;
DWORD cbRead, cbToWrite, cbWritten, dwMode;
HANDLE hNamedPipe=CreateNamedPipe(NAMED_PIPE,
PIPE_ACCESS_DUPLEX,
PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_WAIT,
PIPE_UNLIMITED_INSTANCES,
BUFSIZE,
BUFSIZE,
0,
NULL);
if (hNamedPipe == INVALID_HANDLE_VALUE)
{
printf("failed to create named pipe. Error: %d \n", GetLastError());
return 0;
}
// Try to open a named pipe; wait for it, if necessary.
while (1)
{
hPipe = CreateFile(
NAMED_PIPE, // pipe name
GENERIC_READ | // read and write access
GENERIC_WRITE,
0, // no sharing
NULL, // default security attributes
OPEN_EXISTING, // opens existing pipe
0, // default attributes
NULL); // no template file
// Break if the pipe handle is valid.
if (hPipe != INVALID_HANDLE_VALUE)
break;
// Exit if an error other than ERROR_PIPE_BUSY occurs.
if (GetLastError() != ERROR_PIPE_BUSY)
{
_tprintf( TEXT("Could not open pipe. GLE=%d\n"), GetLastError() );
return -1;
}
// All pipe instances are busy, so wait for 20 seconds.
if ( ! WaitNamedPipe(NAMED_PIPE, 20000))
{
printf("Could not open pipe: 20 second wait timed out.");
return -1;
}
}
cbToWrite = (lstrlen(lpvMessage)+1)*sizeof(TCHAR);
fSuccess = WriteFile(
hPipe, // client pipe handle
lpvMessage, // message
cbToWrite, // message length
&cbWritten, // bytes written
NULL); // not overlapped
if ( ! fSuccess)
{
_tprintf( TEXT("WriteFile to pipe failed. GLE=%d\n"), GetLastError() );
return -1;
}
do
{
// Read from the pipe.
fSuccess = ReadFile(
hNamedPipe, // server pipe handle
chBuf, // buffer to receive reply
BUFSIZE*sizeof(TCHAR), // size of buffer
&cbRead, // number of bytes read
NULL); // not overlapped
if ( ! fSuccess && GetLastError() != ERROR_MORE_DATA )
break;
_tprintf( TEXT("\"%s\"\n"), chBuf );
} while ( ! fSuccess); // repeat loop if ERROR_MORE_DATA
if ( ! fSuccess)
{
_tprintf( TEXT("ReadFile from pipe failed. GLE=%d\n"), GetLastError() );
return -1;
}
printf("\n
CloseHandle(hPipe);
return 0;
}
Recent Entries
- Clicking on Radio button hangs for MFC based dialog Application
- zFeeder – Making PHP 5.3 + compatible, solving ‘Function ereg_replace() is deprecated’
- How to multilingualize or localize URL in Gallery2 – enhancement of multilang module to create multilingual URLs, Multi-language URL for SEO with Multi Language Module
- Fix for mergelog, fails with error “abort due to a problem” for dummy records of apache access logs
- Shepherds life in mountain
- The modern marriage culture among mate
- Do you know that barter system still exists in most villages in uttarakhand, may be true for many other parts of India also
- How to catch copy paste from explorer using shell extension? ICopyHook alternative for files and folder
- How to convert ASCII/UNICODE string to hex string? Function/API to convert to hex string
- How to solve ‘ReadFile hangs’ for named pipe client/server – sample working code for named pipe