#include<stdio.h>
#include<stdlib.h>
#include<windows.h>
#pragma comment( lib , "kernel32.lib" )
#define PIPE_NAME_SERVER_READ L"\\\\.\\Pipe\\Read"
#define PIPE_NAME_SERVER_WRITE L"\\\\.\\Pipe\\Write"
::HANDLE qqPipeHandleRead , qqPipeHandleWrite;
::DWORD WINAPI procRead( ::LPVOID lpParameter )
{
::printf( "Client reading is running!\n" );
::DWORD BytesRead;
::CHAR Buffer[ 1024 ];
while(1)
{
::printf( "Client reading is running!\n" );
if( ::ReadFile( qqPipeHandleRead , Buffer , sizeof(Buffer) , &BytesRead , NULL ) > 0 )
{
Buffer[ BytesRead ] = '\0';
::printf( "%s\t\t%d\n" , Buffer , BytesRead );
continue;
}//if
}//while
return 0;
}
::DWORD WINAPI procWrite( ::LPVOID lpParameter )
{
::printf( "Client writing is running!\n" );
::DWORD BytesWritten;
::CHAR Buffer[ 1024 ];
int count = 0;
while(1)
{
::printf( "Client writing is running!\n" );
::scanf( "%s" , Buffer );
count = 0;
for( int i=0; Buffer[i] != '\0' ; ++i )
++count;
if( ::WriteFile( qqPipeHandleWrite , Buffer , count , &BytesWritten , NULL ) == 0 )
{
::printf( "WirteFile failed with error %d !\n" , ::GetLastError() );
continue;
}//if
}//while
return 0;
}
int main()
{
::HANDLE hRead , hWrite;
::DWORD idRead , idWrite;
if( ::WaitNamedPipe( PIPE_NAME_SERVER_WRITE , NMPWAIT_WAIT_FOREVER ) == 0 )
{
::printf( "WaitNamedPipe failed with error %d !" , ::GetLastError() );
return -1;
}//if
if( ::WaitNamedPipe( PIPE_NAME_SERVER_READ , NMPWAIT_WAIT_FOREVER ) == 0 )
{
::printf( "WaitNamedPipe failed with error %d !" , ::GetLastError() );
return -1;
}//if
qqPipeHandleRead = ::CreateFile( PIPE_NAME_SERVER_WRITE , GENERIC_READ | GENERIC_WRITE , 0 , (LPSECURITY_ATTRIBUTES)NULL ,
OPEN_EXISTING , FILE_ATTRIBUTE_NORMAL , (HANDLE)NULL );
if( qqPipeHandleRead == INVALID_HANDLE_VALUE )
{
::printf( "CreateFile failed with error %d !" , ::GetLastError() );
return -1;
}//if
qqPipeHandleWrite = ::CreateFile( PIPE_NAME_SERVER_READ , GENERIC_READ | GENERIC_WRITE , 0 , (LPSECURITY_ATTRIBUTES)NULL ,
OPEN_EXISTING , FILE_ATTRIBUTE_NORMAL , (HANDLE)NULL );
if( qqPipeHandleWrite == INVALID_HANDLE_VALUE )
{
::printf( "CreateFile failed with error %d !" , ::GetLastError() );
return -1;
}//if
::printf( "Client is running!\n" );
hRead = ::CreateThread( NULL , 0 , procRead , NULL , 0 , &idRead );
if( hRead == NULL )
{
::printf( "CreateThread failed with error %d !\n" , ::GetLastError() );
return -1;
}//if
hWrite = ::CreateThread( NULL , 0 , procWrite , NULL , 0 , &idWrite );
if( hWrite == NULL )
{
::printf( "CreateThread failed with error %d !\n" , ::GetLastError() );
return -1;
}//if
while(1)
{
}//while
return 0;
}