Solved

Named Pipe Server returning NULL on read file

Posted on 2010-09-21
4
338 Views
Last Modified: 2012-05-10
Hi Guys,

I have a named pipe server in c++ and a named pipe client in c++, but whenever the client sends something to the server with the WriteFile funcion, the server doesent detect what it received. It just returns a NULL like:

Client sent the following message: (NULL)

Here is the Named Pipe SERVER code if you can spot something wrong with it:

#include "stdafx.h"
#include <stdio.h>
#include "windows.h"

#define BUFSIZE 1024 //1k

int main(){

    BOOL fConnected;
    LPTSTR lpszPipename = "\\\\.\\pipe\\MainStream";
    CHAR chRequest[BUFSIZE];
    DWORD cbBytesRead;
    BOOL fSuccess;
    HANDLE hPipe;

     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 
          NMPWAIT_USE_DEFAULT_WAIT, // client time-out 
          NULL);                    // default security attribute 

     if (hPipe == INVALID_HANDLE_VALUE){
         return true;
     }

    for (;;)
     {

          fConnected = ConnectNamedPipe(hPipe, NULL) ? TRUE : (GetLastError() == ERROR_PIPE_CONNECTED);

          if (fConnected)
          {

              LPVOID  szBuffer;
              DWORD cbBytes;

                fSuccess = 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 

                
                printf("\nClient sent the following message: %s", szBuffer);

                
                if (! fSuccess || cbBytesRead == 0)
                //break;

                FlushFileBuffers(hPipe);                  
                DisconnectNamedPipe(hPipe);    

          }else{
              CloseHandle(hPipe); 
          }

    }
    
}

Open in new window


I can post the client code aswell, but i think that the issue is located on the server side. Thank you for all your help.


0
Comment
Question by:xNejX
  • 2
  • 2
4 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 33729711
What does 'GetLastError()' report in that case?
0
 

Author Comment

by:xNejX
ID: 33729872
its:

Invalid access to memory location.
0
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 33729921
You are using a locally declared and uninitialized

LPVOID  szBuffer;

in your loop. Either make that

LPVOID  szBuffer = chRequest;

or allocate memory for it.
0
 

Author Closing Comment

by:xNejX
ID: 33729976
It solved the problem. Thank you.
0

Featured Post

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question