imtangchen  
日历
<2009年8月>
2627282930311
2345678
9101112131415
16171819202122
23242526272829
303112345
统计
  • 随笔 - 5
  • 文章 - 0
  • 评论 - 0
  • 引用 - 0

导航

常用链接

留言簿

随笔档案

搜索

  •  

最新评论

阅读排行榜

评论排行榜

 

 #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;
}

posted on 2009-08-25 23:46 我可能长大了 阅读(145) 评论(0)  编辑 收藏 引用

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


 
Copyright © 我可能长大了 Powered by: 博客园 模板提供:沪江博客