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();
[ When i used WaitNamedPipe(), than it start waiting infinitely ]
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:
#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");
mypipe = CreateNamedPipe( pipe_name,
PIPE_ACCESS_DUPLEX ,
PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT,
PIPE_UNLIMITED_INSTANCES,
buffersize,
buffersize,
5000,
NULL);
if(mypipe!=NULL)
{
printf("Named pipe created successfully.\n");
}
else
{
printf("Program EXIT.\n");
sleep(5000);
exit(1);
}
myfile = CreateFile(pipe_name, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, 0, NULL);
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) {
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);
DisconnectNamedPipe(mypipe);
}
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:
#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");
printf("GetLastError: %d\n",GetLastError());
while(success!=1)
{
myfile = CreateFile(pipe_name, GENERIC_READ , FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
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);
}
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;
}