Hi,
It seems that the 2nd iteration of Connect() is not blocking anymore. Is
this a correct behavior?
Run Win32API-File.pl first and run Win32-Pipe-Client.pl on another console.
Show quoted text
>perl Win32-Pipe.pl
0.024
Creating pipe 'My Named Pipe'
1:Waiting for a client to connect...blocking
Client sent us: Time on XXX is: Fri Sep 28 14:30:57 2012
Disconnecting...
2:Waiting for a client to connect...NOT blocking
3:Waiting for a client to connect...NOT blocking
4:Waiting for a client to connect...NOT blocking
5:Waiting for a client to connect...NOT blocking
6:Waiting for a client to connect...NOT blocking
7:Waiting for a client to connect...NOT blocking
8:Waiting for a client to connect...NOT blocking
9:Waiting for a client to connect...NOT blocking
10:Waiting for a client to connect...NOT blocking
11:Waiting for a client to connect...NOT blocking
Subject: | Win32API-File.pl |
use strict;
use Win32;
use Win32::API;
use Win32API::File qw( :Func );
my $PipeName = "My Named Pipe";
use constant {
PIPE_NAME => "//./pipe/X"
, PIPE_ACCESS_INBOUND => 0x00000001
, FILE_FLAG_FIRST_PIPE_INSTANCE => 0x00080000
, PIPE_TYPE_MESSAGE => 0x00000004
, PIPE_READMODE_MESSAGE => 0x00000002
};
my $NULL = 0;
# Note warning on LPSECURITY_ATTRIBUTES, but used only as NULL pointer
Win32::API->Import('kernel32'
,'HANDLE CreateNamedPipe( LPCTSTR lpName '
. ', DWORD dwOpenMode, DWORD dwPipeMode, DWORD nMaxInstances'
. ', DWORD nOutBufferSize, DWORD nInBufferSize'
. ', DWORD nDefaultTimeOut'
. ', LPSECURITY_ATTRIBUTES lpSecurityAttributes)' );
# Note warning on LPOVERLAPPED, but used only as NULL pointer.
Win32::API->Import('kernel32'
,'BOOL ConnectNamedPipe(HANDLE hNamedPipe'
. ', LPOVERLAPPED lpOverlapped )');
Win32::API->Import('kernel32'
,'BOOL DisconnectNamedPipe(HANDLE hNamedPipe)');
my $pipehandle = CreateNamedPipe( PIPE_NAME
, PIPE_ACCESS_INBOUND + FILE_FLAG_FIRST_PIPE_INSTANCE
, PIPE_TYPE_MESSAGE + PIPE_READMODE_MESSAGE
, 1 , 512 , 512 , 10000 , $NULL )
or die "Unable to create pipe:". Win32::GetLastError() ;
my( $res, $message);
my $bServerContinue = 1;
while( $bServerContinue ) {
print "Waiting for a client to connect...\n";
ConnectNamedPipe($pipehandle,$NULL)
or die "Connect failed: ". Win32::GetLastError();
$res = ReadFile($pipehandle,$message, 512, [], [] );
print "message: $message\n";
$bServerContinue = 0 if $message eq 'bb';
$res = DisconnectNamedPipe($pipehandle);
if( $message eq "exit" ) {
last;
}
}
$res = CloseHandle($pipehandle);
#http://www.perlmonks.org/?node_id=715874
#http://www.perlmonks.org/?node_id=726593
Subject: | Win32-Pipe-Client.pl |
use Win32::Pipe;
$PipeName = "\\\\localhost\\pipe\\My Named Pipe";
print "Connecting to $PipeName\n";
if( $Pipe = new Win32::Pipe( $PipeName ) )
{
my $Data = "Time on " . Win32::NodeName() . " is: " . localtime() . "\n";
print "\nPipe has been opened, writing data to it...\n";
$Pipe->Write( $Data );
$Pipe->Close();
}
else
{
print "Error connecting: " . Win32::FormatMessage( $Win32::Pipe::Error ) . "\n";
}