#include<stdio.h>
#include<stdlib.h>
#include<windows.h>
#pragma comment( lib , "kernel32.lib" )
#define PIPE_NUM 10
#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( "Server reading is running!\n" );
::DWORD BytesRead;
::CHAR Buffer[ 1024 ];
while(1)
{
::printf( "Server 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( "Server writing is running!\n" );
::DWORD BytesWritten;
::CHAR Buffer[ 1024 ];
int count = 0;
while(1)
{
::printf( "Server 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;
qqPipeHandleRead = ::CreateNamedPipe( PIPE_NAME_SERVER_READ ,
PIPE_ACCESS_DUPLEX ,
PIPE_TYPE_BYTE | PIPE_READMODE_BYTE ,
PIPE_NUM , 0 , 0 , 1000 , NULL );
if( INVALID_HANDLE_VALUE == qqPipeHandleRead )
{
::printf( "CreateNamedPipeW failed with error %d !\n" , ::GetLastError() );
return -1;
}//if
qqPipeHandleWrite = ::CreateNamedPipe( PIPE_NAME_SERVER_WRITE ,
PIPE_ACCESS_DUPLEX ,
PIPE_TYPE_BYTE | PIPE_READMODE_BYTE ,
PIPE_NUM , 0 , 0 , 1000 , NULL );
if( INVALID_HANDLE_VALUE == qqPipeHandleWrite )
{
::printf( "CreateNamedPipeW failed with error %d !\n" , ::GetLastError() );
return -1;
}//if
::printf( "Server 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
if( ::ConnectNamedPipe( qqPipeHandleRead , NULL ) == 0 )
{
::printf( "ConnectNamedPipe failed with error %d !\n" , ::GetLastError() );
}//if
if( ::ConnectNamedPipe( qqPipeHandleWrite , NULL ) == 0 )
{
::printf( "ConnectNamedPipe failed with error %d !\n" , ::GetLastError() );
}//if
while(1)
{
}//while
return 0;
}