Solved

NamedPipes Problem

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

Suggested Solutions

Title # Comments Views Activity
Access Query (QBE) Using VBA Function for Criteria 13 62
Winform not working on 64 bit machine 31 102
What namespace do I need to import? 2 38
SSRS Deployment problem 5 64
This article describes how to add a user-defined command button to the Windows 7 Explorer toolbar.  In the previous article (http://www.experts-exchange.com/A_2172.html), we saw how to put the Delete button back there where it belongs.  "Delete" is …
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
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…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

789 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