Solved

NamedPipes Problem

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In this article, I will show how to use the Ribbon IDs Tool Window to assign the built-in Office icons to a ribbon button.  This tool will help us to find the OfficeImageId that corresponds to our desired built-in Office icon. The tool is part of…
What my article will show is if you ever had to do processing to a listbox without being able to just select all the items in it. My software Visual Studio 2008 crystal report v11 My issue was I wanted to add crystal report to a form and show…
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…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

728 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now