imtangchen  
日历
<2009年9月>
303112345
6789101112
13141516171819
20212223242526
27282930123
45678910
统计
  • 随笔 - 5
  • 文章 - 0
  • 评论 - 0
  • 引用 - 0

导航

常用链接

留言簿

随笔档案

搜索

  •  

最新评论

阅读排行榜

评论排行榜

 

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

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

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


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