Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Named Pipe Server returning NULL on read file

Posted on 2010-09-21
4
Medium Priority
?
353 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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

670 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