Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Named Pipes

Status
Not open for further replies.

ADoozer

Programmer
Dec 15, 2002
3,487
AU
im trying to create a named pipe in one project, write to the pipe and have a second project read the data.

i have sample code which works, where the server creates the pipe and waits for the client to send IT data.

however im finding it hard to transmit from the server to client.

the pipe is being created successfully

Code:
hPipe = CreateNamedPipe ( lpszPipename, 
                                              PIPE_ACCESS_DUPLEX, // read/write access 
                                              PIPE_TYPE_MESSAGE | // message type pipe 
                                              PIPE_READMODE_MESSAGE | // message-read mode 
                                              PIPE_NOWAIT, // blocking mode 
                                              PIPE_UNLIMITED_INSTANCES, // max. instances 
                                              BUFSIZE, // output buffer size 
                                              BUFSIZE, // input buffer size 
                                              PIPE_TIMEOUT, // client time-out 
                                              NULL); // no security attribute

and writing to the pipe is also successful

Code:
HANDLE hFile;
char myName[32];
DWORD dwWrite;
BOOL flg;

hFile = CreateFile("\\\\.\\pipe\\BlackListPipe",GENERIC_WRITE,0, NULL, OPEN_EXISTING,0, NULL);

for (q=0 ;q<32;q++)
{
myName[q]=wsb[dwHdrLen+3+q];
}

flg=WriteFile(hFile, myName, strlen(myName), &dwWrite, NULL);
				
if (flg==FALSE)
{
//Error
}
				
CloseHandle(hFile);

however when i try and get the data in the client it fails (INVALID_HANDLE_VALUE)

Code:
CHAR chRequest[BUFSIZE]; 
DWORD cbBytesRead; 
HANDLE hFile;
BOOL flg;
DWORD dwWrite;
char szPipeUpdate[200];

hFile = CreateFile("\\\\.\\pipe\\BlackListPipe",GENERIC_READ,0, NULL,OPEN_EXISTING,0, NULL);

if(hFile == INVALID_HANDLE_VALUE)
{ 
printf("CreateFile failed for Named Pipe client\n" );
}
else
{
ReadFile (hFile, // handle to pipe 
                                              chRequest, // buffer to receive data 
                                              BUFSIZE, // size of buffer 
                                              &cbBytesRead, // number of bytes read 
                                              NULL); // not overlapped I/O 

chRequest[cbBytesRead] = '\0';
printf("Data Received: %s\n",chRequest);

FlushFileBuffers(hFile); 
DisconnectNamedPipe(hFile);

any help greatly appreciated.

(ps the working demo code can be found here
If somethings hard to do, its not worth doing - Homer Simpson
 
never mind, i think ive sussed it.

here is the client and server (very basic, do not run just single step)

server
Code:
#include <windows.h> 
#include <stdio.h>
#define BUFSIZE 1024
#define PIPE_TIMEOUT 5000

void main()
{
	LPTSTR lpszPipename = "\\\\.\\pipe\\SamplePipe"; 
    HANDLE hPipe; 
	BOOL flg;
    DWORD dwWrite;
    char szPipeUpdate[200];


	hPipe = CreateNamedPipe (	lpszPipename, 
								PIPE_ACCESS_DUPLEX,
                                PIPE_TYPE_MESSAGE | 
                                PIPE_READMODE_MESSAGE | 
                                PIPE_NOWAIT,
                                PIPE_UNLIMITED_INSTANCES,	// max. instances 
                                BUFSIZE,					// output buffer size 
                                BUFSIZE,					// input buffer size 
                                PIPE_TIMEOUT,				// client time-out 
                                NULL);						// no security attribute 

	if (hPipe == INVALID_HANDLE_VALUE) 
		return;

	ConnectNamedPipe(hPipe, NULL);

	for(;;)
	{
		sprintf(szPipeUpdate,"hello client, are you there");
		flg = WriteFile(hPipe, szPipeUpdate, strlen(szPipeUpdate), &dwWrite, NULL);
	}
	
}

Client
Code:
#include <windows.h> 
#include <stdio.h>
#define BUFSIZE 1024
#define PIPE_TIMEOUT 5000

void main()
{
	BOOL flg;
	DWORD cbBytesRead; 
	char szPipeIn[BUFSIZE];
	HANDLE hFile;

	hFile = CreateFile(	"\\\\.\\pipe\\SamplePipe", 
						GENERIC_READ,
                        0, 
						NULL, 
						OPEN_EXISTING,
                        0, 
						NULL);

	if(hFile != INVALID_HANDLE_VALUE)
	{
		for(;;)
		{
			PeekNamedPipe(hFile,NULL,NULL,NULL,&cbBytesRead,NULL);
			if (cbBytesRead!=0)
			{
				flg=ReadFile (	hFile, // handle to pipe 
						        szPipeIn, // buffer to receive data 
							    BUFSIZE, // size of buffer 
								&cbBytesRead, // number of bytes read 
								NULL); // not overlapped I/O 

				if (flg!=FALSE)
				{
					szPipeIn[cbBytesRead] = '\0';
					printf("Data Received: %s\n",szPipeIn);
					FlushFileBuffers(hFile); 
				}
			}
		}
	}
}

If somethings hard to do, its not worth doing - Homer Simpson
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top