Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hello Fellow Programmers,
I'm developing two programs in 'C'. One is for sending data and second is for receiving(not simultaneously) Using 'NamedPipes'.

Here i really want to explain you that what i did in both programs:

* Sending Program--> lets say server.c
Here i created pipe with CreateNamedPipe(); 
create a file with CreateFile(); 
write data with WriteFile(); 
close file handle with Closehandle();



** Receiving Program--> Client.c
Create a file with CreateFile(); //Error :- 231 ERROR_PIPE_BUSY 
[ When i used WaitNamedPipe(), than it start waiting infinitely ] 

//The code written below will be run when the CreateFile() run successfully.....
Set pipe state to 'PIPE_READMODE_MESSAGE' with SetNamedPipeHandleState();
Reading data with ReadFile(); 
closehandle();



So now Please Tell me, why there is Error: 231 ERROR_PIPE_BUSY ???????


Thanks a lot in Advance.
:)

Sender.c:
C++
//------------> Sender.c
 
#define buffersize 500
int main()
{
 
HANDLE mypipe;
HANDLE myfile;
HANDLE glerr;
LPCTSTR pipe_name=TEXT("\\\\.\\pipe\\mynewmsngr");
TCHAR inbuf[buffersize];
LPTSTR outbuf = TEXT("AMS_NDIS");
LPTSTR cmptrname;
char temp_out[buffersize];
DWORD nbytes,dwmode,nsend;
BOOL success=0;
printf("Program Started.\n");
//Creating New Pipe.
mypipe = CreateNamedPipe(/* lpName*/ pipe_name,                     
/* dwOpenMode */               PIPE_ACCESS_DUPLEX ,
/* dwPipeMode */               PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT,
/* nMaxInstances */            PIPE_UNLIMITED_INSTANCES,
/* nOutBufferSize */         buffersize,
/* nInBufferSize */            buffersize,
/* nDefaultTimeOut */         5000,
/* lpSecurityAttributes */ NULL);
 
if(mypipe!=NULL)
{
      printf("Named pipe created successfully.\n");
}
else
{
   printf("Program EXIT.\n");
   sleep(5000);
   exit(1);
}   
 
//-------------> Creating New File.
myfile = CreateFile(pipe_name,                                             //lpFileName
                              GENERIC_READ | GENERIC_WRITE,               //dwDesiredAccess
                              FILE_SHARE_READ | FILE_SHARE_WRITE,      //dwShareMode
                              NULL,                                             //lpSecurityAttributes
                              CREATE_ALWAYS,                              //dwCreationDisposition
                              0,                                             //dwFlagsAndAttributes
                              NULL);                                       //hTemplateFile
                              
// printf("GetLastError after File Creation on Sender Side: %d\n",GetLastError());
 
if( myfile == INVALID_HANDLE_VALUE )
   printf("Error in Creating File. ErrorCode:%d\n",GetLastError());
else
      {
      printf("Pipe File created successfully.\n");   
      }
 
      sleep(5000);
 
      
while(      ConnectNamedPipe(mypipe,NULL) )
{
      if(GetLastError()==535)         //ERROR_PIPE_CONNECTED.
      {
                                                   
 
/*                                                   
//Receiving Message.
         success = 0;
         success = ReadFile(mypipe,inbuf, buffersize, &nbytes, NULL);
                  if(success!=0)
                  {
                        printf("Message Received: %s\n",inbuf);
                        sleep(3000);
                  }
                  else
                  {
                        printf("Message Receiving failed. Error: %d\n",GetLastError());
                  getch();
                  exit(1);
                  }
*/                     
                        
//Sending Message.   
               printf("Message:");
               scanf("%s",&temp_out);
               nsend=strlen(temp_out);
               outbuf=TEXT(temp_out);      
               printf("Sending Message of %d bytes.\n",nsend);
               success = 0;               
               success = WriteFile(myfile,outbuf, nsend, &nbytes, NULL);
               if ( success!=0)
                        {
                              printf("Message Sent successfully.\n");   
                              printf("Message: %s\n\n",outbuf);   
                              //CloseHandle(myfile);
                              //CloseHandle(mypipe);
                              DisconnectNamedPipe(mypipe);
                                    /*
                                          printf("Do you want to Continue To Reading(press 1 if YES):");
                                          int choice;
                                          scanf("%d",&choice);
                                          if(choice==1)
                                             {
                                             
                                                success = ReadFile(mypipe,inbuf, buffersize, &nbytes, NULL);
                                                if(success==1)
                                                {
                                                      printf("Message Received: %s\n",inbuf);
                                                      sleep(3000);
                                                }
                                                else
                                                      printf("Message Receiving failed. Error: %d\n",GetLastError());
                                             }
                                             else
                                             exit(1);
                                       */
                           }
         else
               {
               printf("Message Sending failed.\n");
               }
      }
      else
      {
            printf("Client not connected. ErrorCode: %d",GetLastError());
            getch();
      }
};
printf("GetLastError: %d\n",GetLastError());
 
getch();
   return 0;

Receiver.c:
C++
///--------------> Receiver.c
 
#include<stdio.h>
#include<stdlib.h>
#include<windows.h>
#include<tchar.h>
#define buffersize 500
int main()
{
HANDLE mypipe,myfile=NULL;
LPCTSTR pipe_name=TEXT("\\\\.\\pipe\\mynewmsngr");
LPVOID inbuf;
LPVOID outbuf = TEXT ("Default_Message.");
LPDWORD nbytes;
DWORD dwmode;
BOOL success=0;
printf("Program Started.\n");
 
///------------------> Opening Pipe
printf("GetLastError: %d\n",GetLastError());
while(success!=1)
{
myfile = CreateFile(pipe_name,                  //lpFileName
                              GENERIC_READ ,            //dwDesiredAccess
                              FILE_SHARE_WRITE,      //dwShareMode
                              NULL,                        //lpSecurityAttributes
                              OPEN_EXISTING,            //dwCreationDisposition
                              0,                              //dwFlagsAndAttributes
                              NULL);                        //hTemplateFile
                              
if (GetLastError()==0)
{
      printf("Handle Found Successfully.\n");
      success=1;
}
else if(GetLastError()==231)
{
         printf("Instances are Busy; Program will Wait 2 Secs and Then Try again.\n");
         WaitNamedPipe(pipe_name, 2000) ;                     
   }
   else if(GetLastError()==2)
   {
   printf("\nThe File not Found.\n");
   printf("EXIT");
         getch();
         exit(1);
   }
   else
   {
         printf("Error: %d\n",GetLastError()); 
         getch();
         exit(1);
   }
}
                              
      printf("File Found.\n"); 
      dwmode=PIPE_READMODE_MESSAGE; 
      success = 0;
      success = SetNamedPipeHandleState(myfile,
                                          &dwmode,
                                          0,
                                          0);
      if(success!=1)
      {
      printf("File mode not changed Successfully; ErrorCode: %d",GetLastError());
      getch();
      exit(1);                  
      }
      
   /* 
//---------------------> Writing Message.                  
      if ( WriteFile(myfile,outbuf, buffersize, nbytes, NULL) )
                  {
                        printf("Message Sent successfully.\n");   
                        printf("Message: %s\n\n",outbuf);   
                  }
      else
            {
                  printf("Message Sending Failed. Error: %d\n",GetLastError());
            }
   */      
      
//---------------------> Reading Message.      
      if( ReadFile(myfile,inbuf, buffersize, nbytes, NULL) )
            {
                  printf("Program is receiving data.\n");
                  printf("Message Received: %s\n",TEXT(inbuf));
                  
            }
      else
            {
                  printf("Message Receiving Failed. Error: %d\n",GetLastError());
            }
 
getch();
exit(1);         
return 0;   
}
Posted
Updated 27-Jul-11 21:53pm
v4
Comments
ThatsAlok 27-Jul-11 5:39am    
could you please let us know the what are the actual parameter are you passing in createfile api!
nk.dushila 28-Jul-11 2:07am    
Chk code on Solutions
Debojyoti Majumder 27-Jul-11 6:06am    
Can you share your code??
nk.dushila 28-Jul-11 2:07am    
Chk code on Solutions
Olivier Levrey 27-Jul-11 9:05am    
Please share your code if you want help.

 
Share this answer
 
Comments
nk.dushila 27-Jul-11 10:45am    
@Olevier
Thanks for your Reply

Yep
i already chkd that.
But it still creating problem.
:(
How about closing the input side of pipe (in server.c) before the writing to client and closing the output side of pipe (in client.c) before reading from the pipe.
 
Share this answer
 
Comments
nk.dushila 28-Jul-11 3:42am    
r u talking about CloseHandle()????
You shouldn't create the pipe from both client and server:

For server:
- call CreateNamedPipe (do not call CreateFile!!)
- call ConnectNamedPipe
- call ReadFile and WriteFile on the pipe handle
- call DisconnectNamedPipe
- call CloseHandle on the pipe handle

For client:
- call CreateFile to connect to the pipe (do not call CreateNamedPipe!!)
- call ReadFile and WriteFile on the pipe handle
- call CloseHandle on the pipe handle
 
Share this answer
 
Comments
nk.dushila 29-Jul-11 4:17am    
ohhk
so thats the problem.

Thanx
Olivier Levrey 29-Jul-11 4:19am    
You are welcome. You may accept this solution if it solved the problem.
If you have closed the pipe at the server end that may be the problem. Take a look at the sample code for server and client here[^] to check if you are doing it correctly.
 
Share this answer
 
Comments
nk.dushila 27-Jul-11 10:46am    
@Richard
Thanks for your Reply

Yeah, i know that i can create a problem
i didn't close pipe or even file handle.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900