Named Pipe in NT

[Back]

// serverpipe.cpp : Defines the entry point for the console application.
//

#include <windows.h>
#include <process.h>
#include <stdio.h>

HANDLE hPipe;
int Buffer_in;
int count;

int main(int argc, char* argv[])
{
    hPipe = CreateNamedPipe("\\\\.\\pipe\\muller", //this machine
    PIPE_ACCESS_INBOUND,
    PIPE_TYPE_BYTE | PIPE_WAIT, 
    10, 0, sizeof(Buffer_in), 
   10000, // timeout in millseconds
    NULL); // security descriptor

if(INVALID_HANDLE_VALUE == hPipe) 
{
   printf("Server Pipe not created\n");
   exit(0);
}
else
   printf("Successful in creating server pipe\n");
// wait of a  connection.
while ( !ConnectNamedPipe(hPipe, (LPOVERLAPPED) NULL)); printf("Client has connected\n");
for(int i=0; i<10; i++) 
{
ReadFile(hPipe,
(LPVOID) &Buffer_in, (DWORD) sizeof(Buffer_in), (LPDWORD) &count, (LPOVERLAPPED) NULL);
printf("revieved %d\n", Buffer_in);
}

printf("press 'c' to quit\n");
while( toupper(getchar()) != 'C');
CloseHandle(hPipe);
return 0;
}

// clientpipe.cpp : Defines the entry point for the console application.
//

#include <windows.h>
#include <process.h>
#include <stdio.h>

HANDLE hPipe;

const int BUFSIZE = 10;
int          Buffer_out;
int          count;

int main(int argc, char* argv[])
{
  hPipe = CreateFile("\\\\.\\pipe\\muller", // this machine 
  GENERIC_WRITE, 0,
  NULL, OPEN_EXISTING,
  FILE_ATTRIBUTE_NORMAL,
  (HANDLE) NULL);

if(INVALID_HANDLE_VALUE == hPipe) 
{
 printf("Server Pipe not found\n");
 goto done;
}
else
  printf("Successful in finding server pipe\n");

for(Buffer_out=0; Buffer_out< BUFSIZE; Buffer_out++)
{
 printf("sending %d\n", Buffer_out);
    WriteFile(hPipe, 
                &Buffer_out, sizeof(Buffer_out), 
                 (LPDWORD) &count, NULL);
}

printf("%d integers written, press 'c' to quit\n");
CloseHandle(hPipe);
done:
while( toupper(getchar()) != 'C');

return 0;
}