Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Named Pipe Server returning NULL on read file

Posted on 2010-09-21
4
Medium Priority
?
358 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 2000 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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

879 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