Solved

Named Pipe Server returning NULL on read file

Posted on 2010-09-21
4
340 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

828 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