Named Pipes Client Error 5 (C++) - c++

Ok so below I have the code for named pipe server/client. I have the server placed on my Windows 8 computer and it creates a pipe and then waits for the client to connect. Then I start the client on my Windows 7 but it returns error 5 (Access denied, I think??) Can someone explain to me why it is giving me error 5 please?
Thnx in advance for all the answers
Server Code
#include "stdafx.h"
#include "windows.h"
#include <iostream>
using namespace std;
#define g_szPipeName "\\\\.\\pipe\\pipename" //Name given to the pipe
#define BUFFER_SIZE 1024 //1k
#define ACK_MESG_RECV "Message received successfully"
#define _CRT_SECURE_NO_WARNINGS
HANDLE hPipe;
int repeate() {
char szBuffer[BUFFER_SIZE];
DWORD cbBytes;
//We are connected to the client.
//To communicate with the client we will use ReadFile()/WriteFile()
//on the pipe handle - hPipe
//Read client message
BOOL bResult = ReadFile(
hPipe, // handle to pipe
szBuffer, // buffer to receive data
sizeof(szBuffer), // size of buffer
&cbBytes, // number of bytes read
NULL); // not overlapped I/O
if ((!bResult) || (0 == cbBytes))
{
printf("\nError occurred while reading from the client: %d", GetLastError());
CloseHandle(hPipe);
system("Pause");
return 1; //Error
}
else
{
printf("\nReadFile() was successful.");
}
printf("\nClient sent the following message: %s", szBuffer);
strcpy(szBuffer, ACK_MESG_RECV);
//Reply to client
bResult = WriteFile(
hPipe, // handle to pipe
szBuffer, // buffer to write from
strlen(szBuffer) + 1, // number of bytes to write, include the NULL
&cbBytes, // number of bytes written
NULL); // not overlapped I/O
if ((!bResult) || (strlen(szBuffer) + 1 != cbBytes))
{
printf("\nError occurred while writing to the client: %d", GetLastError());
CloseHandle(hPipe);
system("Pause");
return 1; //Error
}
else
{
printf("\nWriteFile() was successful.");
}
repeate();
}
int main(int argc, char* argv[])
{
SECURITY_DESCRIPTOR sd;
InitializeSecurityDescriptor(&sd, SECURITY_DESCRIPTOR_REVISION);
SECURITY_ATTRIBUTES sa;
sa.nLength = sizeof(SECURITY_ATTRIBUTES);
sa.lpSecurityDescriptor = &sd;
hPipe = CreateNamedPipe(
g_szPipeName, // pipe name
PIPE_ACCESS_DUPLEX, // read/write access
PIPE_TYPE_MESSAGE | // message type pipe
PIPE_READMODE_MESSAGE | // message-read mode
PIPE_WAIT, // blocking mode
PIPE_UNLIMITED_INSTANCES, // max. instances
BUFFER_SIZE, // output buffer size
BUFFER_SIZE, // input buffer size
NMPWAIT_USE_DEFAULT_WAIT,
&sa);
if (INVALID_HANDLE_VALUE == hPipe)
{
printf("\nError occurred while creating the pipe: %d", GetLastError());
system("Pause");
return 1; //Error
}
else
{
printf("\nCreateNamedPipe() was successful.");
}
printf("\nWaiting for client connection...");
//Wait for the client to connect
BOOL bClientConnected = ConnectNamedPipe(hPipe, NULL);
if (FALSE == bClientConnected)
{
printf("\nError occurred while connecting to the client: %d", GetLastError());
CloseHandle(hPipe);
system("Pause");
return 1; //Error
}
else
{
printf("\nConnectNamedPipe() was successful.");
}
repeate();
}
Client Code
#include "stdafx.h"
#include "windows.h"
#include <iostream>
#define g_szPipeName "\\\\MyComputerName\\pipe\\pipename" //Name given to the pipe
#define BUFFER_SIZE 1024 //1k
#define ACK_MESG_RECV "Message received successfully"
HANDLE hPipe;
int repeate() {
char szBuffer[BUFFER_SIZE];
printf("\nEnter a message to be sent to the server: ");
gets(szBuffer);
DWORD cbBytes;
//Send the message to server
BOOL bResult = WriteFile(
hPipe, // handle to pipe
szBuffer, // buffer to write from
strlen(szBuffer) + 1, // number of bytes to write, include the NULL
&cbBytes, // number of bytes written
NULL); // not overlapped I/O
if ((!bResult) || (strlen(szBuffer) + 1 != cbBytes))
{
printf("\nError occurred while writing to the server: %d", GetLastError());
CloseHandle(hPipe);
system("Pause");
return 1; //Error
}
else
{
printf("\nWriteFile() was successful.");
}
//Read server response
bResult = ReadFile(
hPipe, // handle to pipe
szBuffer, // buffer to receive data
sizeof(szBuffer), // size of buffer
&cbBytes, // number of bytes read
NULL); // not overlapped I/O
if ((!bResult) || (0 == cbBytes))
{
printf("\nError occurred while reading from the server: %d", GetLastError());
CloseHandle(hPipe);
system("Pause");
return 1; //Error
}
else
{
printf("\nReadFile() was successful.");
}
printf("\nServer sent the following message: %s", szBuffer);
repeate();
}
int main(int argc, char* argv[])
{
//Connect to the server pipe using CreateFile()
hPipe = CreateFile(
g_szPipeName, // 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
if (INVALID_HANDLE_VALUE == hPipe)
{
printf("\nError occurred while connecting to the server: %d", GetLastError());
//One might want to check whether the server pipe is busy
//This sample will error out if the server pipe is busy
//Read on ERROR_PIPE_BUSY and WaitNamedPipe() for that
system("Pause");
return 1; //Error
}
else
{
printf("\nCreateFile() was successful.");
}
//We are done connecting to the server pipe,
//we can start communicating with the server using ReadFile()/WriteFile()
//on handle - hPipe
repeate();
}

Configure both computers by adding ip address, subnet mask, default gateway. From Network and Sharing Center, turn on all sharing and turn off password protected sharing. Test it by pinging ip address from cmd. User account of the both computers should not be password protected. That's why i fixed the Client Error 5 and set up named pipe communication between two remote computers

The client should use OpenFile(), not CreateFile(). The client doesn't want to create anything new, it wants to talk to an existing pipe.

Related

Microsoft VC++, vsnprintf, and Pipes (IO) Bug

I am using a DLL injection on to start the client end of a file pipe, which talks to a server that logs the messages. The problem is that the server only receives a buffer filled with question mark ('?') characters.
Client / Injectable DLL:
#include <windows.h>
#include <stdio.h>
#include <tchar.h>
#include <strsafe.h>
#define BUFSIZE 1024*1024
HANDLE hPipe;
BOOL fSuccess = FALSE;
DWORD cbToWrite, cbWritten, dwMode;
const wchar_t* lpszPipename = TEXT("\\\\.\\pipe\\listen");
char write_buffer[BUFSIZE];
void init()
{
hPipe = CreateFile(
lpszPipename, // 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
// The pipe connected; change to message-read mode.
dwMode = PIPE_READMODE_MESSAGE;
fSuccess = SetNamedPipeHandleState(
hPipe, // pipe handle
&dwMode, // new pipe mode
NULL, // don't set maximum bytes
NULL); // don't set maximum time
}
#pragma warning(disable:4996)
void report(const char* frmt, ...)
{
va_list args;
va_start(args, frmt);
vsnprintf(write_buffer, BUFSIZE, frmt, args);
va_end(args);
// Send a message to the pipe server.
fSuccess = WriteFile(
hPipe, // pipe handle
write_buffer, // message
strlen(write_buffer), // message length
&cbWritten, // bytes written
NULL); // not overlapped
return;
}
Server:
#include <windows.h>
#include <stdio.h>
#include <tchar.h>
#include <strsafe.h>
#define BUFSIZE 1024*1024
BOOL fConnected = FALSE;
DWORD dwThreadId = 0;
HANDLE hPipe = INVALID_HANDLE_VALUE, hThread = NULL;
const wchar_t* lpszPipename = TEXT("\\\\.\\pipe\\listen");
// The main loop creates an instance of the named pipe and
// then waits for a client to connect to it. When the client
// connects, a thread is created to handle communications
// with that client, and this loop is free to wait for the
// next client connect request. It is an infinite loop.
for (;;)
{
_tprintf(TEXT("\nPipe Server: Main thread awaiting client connection on %s\n"), lpszPipename);
hPipe = CreateNamedPipe(
lpszPipename, // pipe name
PIPE_ACCESS_DUPLEX, // read/write access
PIPE_TYPE_MESSAGE | // message type pipe
PIPE_READMODE_MESSAGE | // message-read mode
PIPE_WAIT, // blocking mode
PIPE_UNLIMITED_INSTANCES, // max. instances
BUFSIZE, // output buffer size
BUFSIZE, // input buffer size
0, // client time-out
NULL); // default security attribute
if (hPipe == INVALID_HANDLE_VALUE)
{
_tprintf(TEXT("CreateNamedPipe failed, GLE=%d.\n"), GetLastError());
return -1;
}
// Wait for the client to connect; if it succeeds,
// the function returns a nonzero value. If the function
// returns zero, GetLastError returns ERROR_PIPE_CONNECTED.
fConnected = ConnectNamedPipe(hPipe, NULL) ?
TRUE : (GetLastError() == ERROR_PIPE_CONNECTED);
if (fConnected)
{
printf("Client connected, creating a processing thread.\n");
// Create a thread for this client.
hThread = CreateThread(
NULL, // no security attribute
0, // default stack size
InstanceThread, // thread proc
(LPVOID)hPipe, // thread parameter
0, // not suspended
&dwThreadId); // returns thread ID
if (hThread == NULL)
{
_tprintf(TEXT("CreateThread failed, GLE=%d.\n"), GetLastError());
return -1;
}
else CloseHandle(hThread);
}
else
// The client could not connect, so close the pipe.
CloseHandle(hPipe);
}
DWORD WINAPI InstanceThread(LPVOID lpvParam)
// This routine is a thread processing function to read from and reply to a client
// via the open pipe connection passed from the main loop. Note this allows
// the main loop to continue executing, potentially creating more threads of
// of this procedure to run concurrently, depending on the number of incoming
// client connections.
{
HANDLE hHeap = GetProcessHeap();
TCHAR* pchRequest = (TCHAR*)HeapAlloc(hHeap, 0, BUFSIZE * sizeof(TCHAR));
TCHAR* pchReply = (TCHAR*)HeapAlloc(hHeap, 0, BUFSIZE * sizeof(TCHAR));
DWORD cbBytesRead = 0, cbReplyBytes = 0, cbWritten = 0;
BOOL fSuccess = FALSE;
HANDLE hPipe = NULL;
// Print verbose messages. In production code, this should be for debugging only.
printf("InstanceThread created, receiving and processing messages.\n");
// The thread's parameter is a handle to a pipe object instance.
hPipe = (HANDLE)lpvParam;
// Loop until done reading
while (1)
{
// Read client requests from the pipe. This simplistic code only allows messages
// up to BUFSIZE characters in length.
fSuccess = ReadFile(
hPipe, // handle to pipe
pchRequest, // buffer to receive data
BUFSIZE, // size of buffer
&cbBytesRead, // number of bytes read
NULL); // not overlapped I/O
// Process the incoming message.
_tprintf(TEXT("Client Request String:\"%s\"\n"), pchRequest);
}
FlushFileBuffers(hPipe);
DisconnectNamedPipe(hPipe);
CloseHandle(hPipe);
HeapFree(hHeap, 0, pchRequest);
HeapFree(hHeap, 0, pchReply);
printf("InstanceThread exitting.\n");
return 1;
}
P.S. If is some how possible to use a debugger with a injectable DLL please let me know!
There are a few problems with this code, which I will come to at the end. First, some code that works. Please note that I have simplified things slightly by making it all into a single application (so that I could easily test it) and by getting rid of the thread, but none of that matters in the context of your question.
#define _CRT_SECURE_NO_WARNINGS
#include <windows.h>
#include <stdio.h>
#include <tchar.h>
#include <strsafe.h>
#define BUFSIZE 1024*1024
const TCHAR* lpszPipename = TEXT("\\\\.\\pipe\\listen");
char write_buffer [BUFSIZE];
HANDLE init()
{
HANDLE hPipe = CreateFile(
lpszPipename, // 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
if (hPipe == INVALID_HANDLE_VALUE)
{
printf ("CreateFile returned error %d\n", GetLastError ());
return INVALID_HANDLE_VALUE;
}
// The pipe connected; change to message-read mode.
DWORD dwMode = PIPE_READMODE_MESSAGE;
BOOL fSuccess = SetNamedPipeHandleState(
hPipe, // pipe handle
&dwMode, // new pipe mode
NULL, // don't set maximum bytes
NULL); // don't set maximum time
if (!fSuccess)
{
printf ("SetNamedPipeHandleState returned error %d\n", GetLastError ());
CloseHandle (hPipe);
return INVALID_HANDLE_VALUE;
}
return hPipe;
}
void report(HANDLE hPipe, const char *frmt, ...)
{
va_list args;
va_start(args, frmt);
_vsnprintf(write_buffer, BUFSIZE, frmt, args);
va_end(args);
// Send a message to the pipe server.
DWORD cbWritten;
BOOL fSuccess = WriteFile(
hPipe, // pipe handle
write_buffer, // message
(DWORD) strlen (write_buffer) + 1, // message length, including EOS
&cbWritten, // bytes written
NULL); // not overlapped
if (!fSuccess)
printf ("WriteFile returned error %d\n", GetLastError ());
}
int _tmain (int argc, TCHAR **argv)
{
if (argc > 1 && _tcscmp (argv [1], __T ("send")) == 0)
{
// send
HANDLE hPipe = init ();
if (hPipe != INVALID_HANDLE_VALUE)
{
report (hPipe, "A message to you, Rudi");
CloseHandle (hPipe);
}
return 0;
}
// receive
for (;;)
{
_tprintf(TEXT("\nPipe Server: Main thread awaiting client connection on %s\n"), lpszPipename);
HANDLE hPipe = CreateNamedPipe(
lpszPipename, // pipe name
PIPE_ACCESS_DUPLEX, // read/write access
PIPE_TYPE_MESSAGE | // message type pipe
PIPE_READMODE_MESSAGE | // message-read mode
PIPE_WAIT, // blocking mode
PIPE_UNLIMITED_INSTANCES, // max. instances
BUFSIZE, // output buffer size
BUFSIZE, // input buffer size
0, // client time-out
NULL); // default security attribute
if (hPipe == INVALID_HANDLE_VALUE)
{
printf ("CreateNamedPipe failed, GLE=%d.\n", GetLastError());
return -1;
}
// Wait for the client to connect; if it succeeds,
// the function returns a nonzero value. If the function
// returns zero, GetLastError returns ERROR_PIPE_CONNECTED.
BOOL fConnected = ConnectNamedPipe(hPipe, NULL) ?
TRUE : (GetLastError() == ERROR_PIPE_CONNECTED);
if (!fConnected)
{
printf ("Error %d connecting named pipe\n", GetLastError());
return 255;
}
printf ("Client connected\n");
HANDLE hHeap = GetProcessHeap();
char* pchRequest = (char*) HeapAlloc(hHeap, 0, BUFSIZE);
// Loop until done reading
while (1)
{
// Read client requests from the pipe. This simplistic code only allows messages
// up to BUFSIZE characters in length.
DWORD cbBytesRead = 0;
BOOL fSuccess = ReadFile(
hPipe, // handle to pipe
pchRequest, // buffer to receive data
BUFSIZE, // size of buffer
&cbBytesRead, // number of bytes read
NULL); // not overlapped I/O
if (!fSuccess)
break;
// Process the incoming message.
printf("Client Request String:\"%s\"\n", pchRequest);
}
FlushFileBuffers(hPipe);
DisconnectNamedPipe(hPipe);
CloseHandle(hPipe);
HeapFree(hHeap, 0, pchRequest);
}
return 0;
}
To run this in "send" mode, specify send on the command line. Otherwise it runs as a server. My server runs for ever. Kill it with Ctrl+C.
So what was wrong with your code? Well, mostly, it was a bit of a mishmash of ANSI and UNICODE strings. You need to be much more careful with this kind of thing and you also need to calculate buffer sizes appropriately. This is all fixed in the above code, which is why I posted it. Also, in terms of good programming practise:
You should check for errors much more thoroughly.
As written, the server assumes that the string sent to it is NUL-terminated but the client doesn't send it that way (so I fixed the client).
The server needs to break out of its receive loop when the sender closes its end of the pipe.
Declare local variables locally! (And pass them around as parameters, when appropriate.) Don't use unnecessary globals.
Using #define _CRT_SECURE_NO_WARNINGS is better than explicitly disabling the warning that you get if you don't.
My code fixes all of these issues. HTH.

Unable to read write on serial port in C++

I am writing a serial communication code in c++ on Visual Studio 2010. For this I try this example, which is working fine. Now, I try to send a string and read it back by shorting Rx and Tx pins of my hardware. Here is my code for sending string:-
char *buffer ="hello";
DWORD bytesSend;
DWORD errors;
COMSTAT status;
DWORD numWritten;
WriteFile(hCom, buffer, strlen(buffer), &numWritten, NULL);
printf("send succesfully\n");
and here is my receiving code:-
DWORD numRead;
BOOL ret = ReadFile(hCom, buffer, 5, &numRead, NULL);
if(!ret)
{
printf("Read Fail\n");
}
else
{
printf("%s\n",buffer);
}
On running my code, receive is getting failed. So, please tell me where is the problem. here is my complete code.
#include "stdafx.h"
#include <windows.h>
#include <tchar.h>
#include <stdio.h>
void PrintCommState(DCB dcb)
{
// Print some of the DCB structure values
_tprintf( TEXT("\nBaudRate = %d, ByteSize = %d, Parity = %d, StopBits = %d\n"),
dcb.BaudRate,
dcb.ByteSize,
dcb.Parity,
dcb.StopBits );
}
int _tmain( )
{
DCB dcb;
HANDLE hCom;
BOOL fSuccess;
TCHAR *pcCommPort = TEXT("COM1"); // Most systems have a COM1 port
// Open a handle to the specified com port.
hCom = CreateFile( pcCommPort,
GENERIC_READ | GENERIC_WRITE,
0, // must be opened with exclusive-access
NULL, // default security attributes
OPEN_EXISTING, // must use OPEN_EXISTING
0, // not overlapped I/O
NULL ); // hTemplate must be NULL for comm devices
if (hCom == INVALID_HANDLE_VALUE)
{
// Handle the error.
printf ("CreateFile failed with error %d.\n", GetLastError());
return (1);
}
// Initialize the DCB structure.
SecureZeroMemory(&dcb, sizeof(DCB));
dcb.DCBlength = sizeof(DCB);
// Build on the current configuration by first retrieving all current
// settings.
fSuccess = GetCommState(hCom, &dcb);
if (!fSuccess)
{
// Handle the error.
printf ("GetCommState failed with error %d.\n", GetLastError());
return (2);
}
PrintCommState(dcb); // Output to console
// Fill in some DCB values and set the com state:
// 57,600 bps, 8 data bits, no parity, and 1 stop bit.
dcb.BaudRate = CBR_57600; // baud rate
dcb.ByteSize = 8; // data size, xmit and rcv
dcb.Parity = NOPARITY; // parity bit
dcb.StopBits = ONESTOPBIT; // stop bit
fSuccess = SetCommState(hCom, &dcb);
if (!fSuccess)
{
// Handle the error.
printf ("SetCommState failed with error %d.\n", GetLastError());
return (3);
}
// Get the comm config again.
fSuccess = GetCommState(hCom, &dcb);
if (!fSuccess)
{
// Handle the error.
printf ("GetCommState failed with error %d.\n", GetLastError());
return (2);
}
PrintCommState(dcb); // Output to console
_tprintf (TEXT("Serial port %s successfully reconfigured.\n"), pcCommPort);
char *buffer ="hello";
DWORD bytesSend;
DWORD errors;
COMSTAT status;
DWORD numWritten;
WriteFile(hCom, buffer, strlen(buffer), &numWritten, NULL);
printf("send succesfully\n");
DWORD numRead;
BOOL ret = ReadFile(hCom, buffer, 5, &numRead, NULL);
if(!ret)
{
printf("Read Fail\n");
}
else
{
printf("%s",buffer);
}
return (0);
}
Here is my output:-
Try flushing the file buffer after write.
https://msdn.microsoft.com/en-us/library/windows/desktop/aa364439%28v=vs.85%29.aspx

Windows named pipe client WriteFile fails with error 232 ERROR_NO_DATA only with first client [duplicate]

I've got a C++ program that is creating a named pipe to write data to. Some customers have reported a situation where the client connects to the named pipe but the server end fails to write the data (with ERROR_NO_DATA).
This error code isn't really explained in any MSDN page that I could find; does anyone have any ideas on how to fix this? Or what the cause is?
Open code:
ostringstream pipeName;
pipeName << "\\\\.\\pipe\\unique-named-pipe-" << GetCurrentProcessId();
pipeHandle = CreateNamedPipeA(
pipeName.str().c_str(), // pipe name
PIPE_ACCESS_DUPLEX, // open mode
PIPE_TYPE_BYTE | PIPE_READMODE_BYTE, // pipe mode
PIPE_UNLIMITED_INSTANCES, // max instances
512, // output buffer size
512, // input buffer size
0, // use default timeouts
NULL); // security attributes
if (INVALID_HANDLE_VALUE == pipeHandle)
{
THROW("Failed to create named pipe", GetLastError());
}
cout << "Pipe ready" << endl;
// Wait for a client to connect to the pipe
BOOL status = ConnectNamedPipe(pipeHandle, NULL);
if (!status)
{
DWORD lastError = GetLastError();
if (ERROR_PIPE_CONNECTED != lastError)
{
THROW("Failed to wait for client to open pipe", lastError);
}
else
{
// Ignore, see MSDN docs for ConnectNamedPipe() for details.
}
}
Writing code:
// response is a std::string
int writeOffset = 0;
int length = response.length();
while ((int) response.length() > writeOffset)
{
DWORD bytesWritten;
BOOL status = WriteFile(
pipeHandle,
response.c_str() + writeOffset,
length - writeOffset,
&bytesWritten,
NULL);
if (!status)
{
// This sometimes fails with ERROR_NO_DATA, why??
THROW("Failed to send via named pipe", GetLastError());
}
writeOffset += bytesWritten;
}
Throw macro
#define THROW(message, errorCode) \
{ \
fprintf(stderr, "%s: line: %d file: %s error:0x%x\n", \
message, __LINE__, __FILE__, errorCode); \
fflush(stderr); \
throw message; \
} \
Thanks!
Looking at WinError.h, which is where this and other error codes are defined:
//
// MessageId: ERROR_NO_DATA
//
// MessageText:
//
// The pipe is being closed.
//
#define ERROR_NO_DATA 232L
Sounds like the client has already closed their end of the pipe - perhaps the client code thinks it has already got the full string, closes their end, while the code above continues to try to write?

multiple writefile and readfile in named-pipe

I need to pass some string by pipe. we can't pass a pointer by pipe and we must pass data. for passing a string we can send an array of chars. but I don't want to use array. I need a way to send strings in variable size.
I used msdn samples for creating pipe server and pipe client:
but inestead of only one writeFile and readFile function in pipe client and pipe server I used them three time in this way:
I used a structure for saving my strings size. first of all this structure is sent. then my two string will be sent. so in pipe server at first, the size of strings will be read and after that the two strings will be received.
i defined a structure like this in both client and server programs:
typedef struct
{
int fileNameLen;
int commandArgLen;
}pipeData,*PpipeData;
pipeData dataToWrite;
pipeData *pdataToWrite = &dataToWrite;
in pipe client I want to send this strings:
LPTSTR s1 = TEXT("file1");
LPTSTR s2 = TEXT("startCmd");
dataToWrite.commandArgLen = sizeof(s1);
dataToWrite.fileNameLen = sizeof(s2);
I sent the structure by pipe client in this way.
fSuccess = WriteFile(
hPipe, // pipe handle
pdataToWrite, // message
sizeof(dataToWrite), // message length
&cbWritten, // bytes written
NULL); // not overlapped
if ( ! fSuccess)
{
_tprintf( TEXT("WriteFile to pipe failed. GLE=%d\n"), GetLastError() );
return -1;
}
fSuccess = WriteFile(
hPipe, // pipe handle
s1, // message
sizeof(s1), // message length
&cbWritten, // bytes written
NULL); // not overlapped
if ( ! fSuccess)
{
_tprintf( TEXT("WriteFile to pipe failed. GLE=%d\n"), GetLastError() );
return -1;
}
fSuccess = WriteFile(
hPipe, // pipe handle
s2, // message
sizeof(s2), // message length
&cbWritten, // bytes written
NULL); // not overlapped
if ( ! fSuccess)
{
_tprintf( TEXT("WriteFile to pipe failed. GLE=%d\n"), GetLastError() );
return -1;
}
in pipe server for read the pipe i use 3 readFile like this:
fSuccess = ReadFile(
hPipe, // handle to pipe
pdataToWrite, // buffer to receive data
sizeof(pdataToWrite), // size of buffer
&cbBytesRead, // number of bytes read
NULL); // not overlapped I/O
if (!fSuccess || cbBytesRead == 0)
{
if (GetLastError() == ERROR_BROKEN_PIPE)
{
_tprintf(TEXT("InstanceThread: client disconnected.\n"), GetLastError());
break;
}
else
{
_tprintf(TEXT("InstanceThread ReadFile failed, GLE=%d.\n"), GetLastError());
break;
}
}
// Process the incoming message.
GetAnswerToRequest(TEXT("structure recieved"), pchReply, &cbReplyBytes);
fSuccess = ReadFile(
hPipe, // handle to pipe
s1, // buffer to receive data
dataToWrite.commandArgLen, // size of buffer
&cbBytesRead, // number of bytes read
NULL); // not overlapped I/O
if (!fSuccess || cbBytesRead == 0)
{
if (GetLastError() == ERROR_BROKEN_PIPE)
{
_tprintf(TEXT("InstanceThread: client disconnected.\n"), GetLastError());
}
else
{
_tprintf(TEXT("InstanceThread ReadFile failed, GLE=%d.\n"), GetLastError());
}
break;
}
GetAnswerToRequest(s1, pchReply, &cbReplyBytes);
fSuccess = ReadFile(
hPipe, // handle to pipe
s2, // buffer to receive data
dataToWrite.fileNameLen, // size of buffer
&cbBytesRead, // number of bytes read
NULL); // not overlapped I/O
if (!fSuccess || cbBytesRead == 0)
{
if (GetLastError() == ERROR_BROKEN_PIPE)
{
_tprintf(TEXT("InstanceThread: client disconnected.\n"), GetLastError());
}
else
{
_tprintf(TEXT("InstanceThread ReadFile failed, GLE=%d.\n"), GetLastError());
}
break;
}
GetAnswerToRequest(s2, pchReply, &cbReplyBytes);
this way doesn't work properly. when pipe server read data in first readFile, it may return this error : ERROR_MORE_DATA (if I used PIPE_TYPE_MESSAGE in createNamedPipe)
I don't know how can i use multiple writeFile and readFile in pipe client and server.
If a named pipe is being read in message mode and the next message is longer than the nNumberOfBytesToRead parameter specifies, ReadFile returns FALSE and GetLastError returns ERROR_MORE_DATA. The remainder of the message can be read by a subsequent call to the ReadFile or PeekNamedPipe function. By MSDN
http://msdn.microsoft.com/en-us/library/windows/desktop/aa365467%28v=vs.85%29.aspx
FYI

WriteFile on a named pipe sometimes returns ERROR_NO_DATA

I've got a C++ program that is creating a named pipe to write data to. Some customers have reported a situation where the client connects to the named pipe but the server end fails to write the data (with ERROR_NO_DATA).
This error code isn't really explained in any MSDN page that I could find; does anyone have any ideas on how to fix this? Or what the cause is?
Open code:
ostringstream pipeName;
pipeName << "\\\\.\\pipe\\unique-named-pipe-" << GetCurrentProcessId();
pipeHandle = CreateNamedPipeA(
pipeName.str().c_str(), // pipe name
PIPE_ACCESS_DUPLEX, // open mode
PIPE_TYPE_BYTE | PIPE_READMODE_BYTE, // pipe mode
PIPE_UNLIMITED_INSTANCES, // max instances
512, // output buffer size
512, // input buffer size
0, // use default timeouts
NULL); // security attributes
if (INVALID_HANDLE_VALUE == pipeHandle)
{
THROW("Failed to create named pipe", GetLastError());
}
cout << "Pipe ready" << endl;
// Wait for a client to connect to the pipe
BOOL status = ConnectNamedPipe(pipeHandle, NULL);
if (!status)
{
DWORD lastError = GetLastError();
if (ERROR_PIPE_CONNECTED != lastError)
{
THROW("Failed to wait for client to open pipe", lastError);
}
else
{
// Ignore, see MSDN docs for ConnectNamedPipe() for details.
}
}
Writing code:
// response is a std::string
int writeOffset = 0;
int length = response.length();
while ((int) response.length() > writeOffset)
{
DWORD bytesWritten;
BOOL status = WriteFile(
pipeHandle,
response.c_str() + writeOffset,
length - writeOffset,
&bytesWritten,
NULL);
if (!status)
{
// This sometimes fails with ERROR_NO_DATA, why??
THROW("Failed to send via named pipe", GetLastError());
}
writeOffset += bytesWritten;
}
Throw macro
#define THROW(message, errorCode) \
{ \
fprintf(stderr, "%s: line: %d file: %s error:0x%x\n", \
message, __LINE__, __FILE__, errorCode); \
fflush(stderr); \
throw message; \
} \
Thanks!
Looking at WinError.h, which is where this and other error codes are defined:
//
// MessageId: ERROR_NO_DATA
//
// MessageText:
//
// The pipe is being closed.
//
#define ERROR_NO_DATA 232L
Sounds like the client has already closed their end of the pipe - perhaps the client code thinks it has already got the full string, closes their end, while the code above continues to try to write?