#include "stdafx.h"
#include <windows.h>
#include <stdio.h>
#include <conio.h>
void main(void)
{
	HANDLE PipeHandle;
	DWORD BytesRead;
	CHAR buffer[256];
DWORD BytesWritten;
	if ((PipeHandle = CreateNamedPipe("\\\\.\\Pipe\\Wohlhabend3215",
		PIPE_ACCESS_DUPLEX, PIPE_TYPE_BYTE | PIPE_READMODE_BYTE, 1,
		0, 0, 1000, NULL)) == INVALID_HANDLE_VALUE)
	{
		printf("CreateNamedPipe failed with error %d\n",
			GetLastError());
		return;
	}

	printf("Server is now running\n");

	if (ConnectNamedPipe(PipeHandle, NULL) == 0)
	{
		printf("ConnectNamedPipe failed with error %d\n",
			GetLastError());
		CloseHandle(PipeHandle);
		_getch();
		return;
	}

	if (ReadFile(PipeHandle, buffer, sizeof(buffer),
		&BytesRead,  NULL) <= 0)
	{
		printf("ReadFile failed with error %d\n", GetLastError());

		CloseHandle(PipeHandle);
		_getch();
		return;
	}

printf("Read %d bytes\n",BytesRead);
     buffer[BytesRead]='\0';
//	printf("Wrote %d bytes", BytesRead);
	printf("\n");

printf(buffer);
printf("\n");
//	printf("%.*s\n", BytesRead, buffer);
	if (WriteFile(PipeHandle, "This is a snswer", 14, &BytesWritten, 
		NULL) == 0)
	{
		printf("WriteFile failed with error %d\n", GetLastError());
		CloseHandle(PipeHandle);
			_getch();
		return;
	}
printf("Wrote %d bytes", BytesWritten);
printf("\n");
	if (DisconnectNamedPipe(PipeHandle) == 0)
	{
		printf("DisconnectNamedPipe failed with error %d\n",
			GetLastError());
		_getch();
		return;
	}

	CloseHandle(PipeHandle);
	_getch();
}
