Solved

NamedPipes Problem

Posted on 1998-06-11
2
1,203 Views
Last Modified: 2013-12-03
why isn't the program working between two diffrent computers ?


This is the server program :

#include <windows.h>
#include <iostream.h>
#include <string.h>

#define BUF_SIZE 257
#define NO_FLAGS 0
#define INSTANCES 1
#define PIPE_NAME "\\\\.\\pipe\\ConvertCase"

void main()
{
      HANDLE hPipe;
      DWORD numRead,converted;
      char buffer[BUF_SIZE];
      BOOL finish;
      

      hPipe = CreateNamedPipe(PIPE_NAME,PIPE_ACCESS_DUPLEX | FILE_FLAG_WRITE_THROUGH ,PIPE_WAIT|PIPE_TYPE_BYTE|PIPE_READMODE_BYTE ,
                                          INSTANCES,BUF_SIZE,BUF_SIZE,0,(LPSECURITY_ATTRIBUTES)NULL);
 
      if (hPipe == INVALID_HANDLE_VALUE )
      {
            cout << "CreateNamedPipe";
            ExitProcess(GetLastError());
      }
      else
            cout << "Pipe Server Created" << flush;

      while(1)
      {
            if (!ConnectNamedPipe(hPipe,(LPOVERLAPPED)NULL))
            {
                  cout << "ConnectNamedPipe" << flush;
                  ExitProcess(GetLastError());
            }

      finish = FALSE;
      cout << "Connected..." << endl;

      while (!finish)
      {
            if (!ReadFile(hPipe,buffer,BUF_SIZE,&numRead,(LPOVERLAPPED)NULL))
            {
                  if(GetLastError() == ERROR_BROKEN_PIPE)
                  {
                        finish = TRUE;
                        continue;
                  }
                  else
                  {
                        cout << "ReadFile";
                        break;
                  }
            }
//            converted = ConvertLine(buffer);
            strcpy(buffer,"This is the return Value from the server");
            
            if (!WriteFile(hPipe,buffer,strlen(buffer)+1,&converted,(LPOVERLAPPED)NULL))
            {
                  cout << "WriteFile";
                  break;
            }
      }
      cout <<"\t....disconnecting" << endl;
      if(!FlushFileBuffers(hPipe))
            cout << "FlushFileBuffers";
      if(!DisconnectNamedPipe(hPipe))
            cout << "DisconnectNamedPipe";
      }


      if(!CloseHandle(hPipe))
            cout << "CloseHadle";
}




This is the client program :

#include <windows.h>
#include <iostream.h>

#define BUF_SIZE 257
#define PIPE_NAME "\\\\ILANM-NT\\pipe\\ConvertCase"

void main(void)
{
      char buffer[BUF_SIZE];
      char obuffer[BUF_SIZE];
      HANDLE hFile;
      HANDLE hPipe;
      DWORD numIO;
      BOOL status,finish;


      hFile = CreateFile("c:\\pipe.txt",GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,0,0);
      if(hFile == INVALID_HANDLE_VALUE)
      {
            cout << "CreateFile- input";
            ExitProcess(GetLastError());
      }

//      wsprintf(pipeName,PIPE_FORMAT,servName);
      hPipe = CreateFile(PIPE_NAME,GENERIC_WRITE|GENERIC_READ,FILE_SHARE_READ|FILE_SHARE_WRITE,(LPSECURITY_ATTRIBUTES)NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
      if(hPipe == INVALID_HANDLE_VALUE)
      {
            cout << "CreateFile - pipe";
            if(!CloseHandle(hFile));
            cout << "CloseHandle#1";
            ExitProcess(GetLastError());
      }


      __try
      {
            finish = FALSE;
            while(!finish)
            {
                  status = ReadFile(hFile,buffer,BUF_SIZE -1,&numIO,(LPOVERLAPPED)NULL);
                  if(!status)
                  {
                        cout << "ReadFile - inpout";
                        __leave;
                  }
                  if(numIO == 0)
                  {
                        finish = TRUE;
                        __leave;
                  }
                  buffer[numIO]='\0';
                  status = WriteFile(hPipe,buffer,numIO +1,&numIO,(LPOVERLAPPED)NULL);
                  if(!status)
                  {
                        cout << "WriteFile - pipe";
                        __leave;
                  }
                  
                  status = ReadFile(hPipe,obuffer,BUF_SIZE,&numIO,(LPOVERLAPPED)NULL);
                  if(!status)
                  {
                        cout << "ReadFile - pipe";
                        __leave;
                  }
                  

                  cout << obuffer << endl;
            }
      }
      __finally
      {
            if(!CloseHandle(hPipe))
                  cout << "CloseHandle-hpipe";
            if(!CloseHandle(hFile))
                  cout << "CloseHandle - hFile";
      }
}

0
Comment
Question by:moshei
[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 Comments
 
LVL 11

Expert Comment

by:alexo
ID: 1407543
CreateNamedPipe() works only on NT.  Are you calling it form a Win95 system?

You must also supply more details.  "Program isn't working" is not enough.
How does the program fail?  On what line?  What does a call to GetLastError() return?

0
 
LVL 2

Accepted Solution

by:
abesoft earned 50 total points
ID: 1407544
alexo:
The fact that the client is connecting to \\\\ILANM-NT\\... makes me think that the server is on NT.  Right moshei?

moshei:
Your problem could be coming from the fact that the client is connecting as a NULL session.  (It's hard to tell from your question if this is the case or not.)

To allow a NULL session to connect to a pipe, check out MS-Knowledge base article Q126645.  Along with a lot of other things, it says:

>You can allow access to a named pipe using the Null Session by adding the pipe name to the following registry entry on the machine that creates the named pipe:
   \HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\LanmanServer\Parameters\NullSessionPipes

The pipe name added to this entry is the name after the last backslash in the string used to open the pipe. For example, if you use the following string to open the pipe:

   \\ILANM-NT\pipe\ConvertCase

you would add ConvertCase to the NullSessionPipes entry on the computer named ILANM-NT.

Hope this helps!
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

This article shows how to make a Windows 7 gadget that extends its U/I with a flyout panel -- a window that pops out next to the gadget.  The example gadget shows several additional techniques:  How to automatically resize a gadget or flyout panel t…
If you have ever found yourself doing a repetitive action with the mouse and keyboard, and if you have even a little programming experience, there is a good chance that you can use a text editor to whip together a sort of macro to automate the proce…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

740 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