Solved

NamedPipes Problem

Posted on 1998-06-11
2
1,200 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
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Duplicates in Aggregate Query 3 42
Having Trouble with MS Access 2010 CommandBars 19 98
WPF MainWindow update textbox from another class... 6 125
Example of WebAPI 7 33
This article describes how to programmatically preset the "Pages per Sheet" option that's available with most printer drivers.   This setting lets you do "n-Up" printing, where two, four, or more pages are printed on each sheet of paper. If your …
For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
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…

831 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