[rt.cpan.org #79924]

[email protected] ("nyet via RT")
Newsgroups perl.libwin32
Message-ID <[email protected]>
Fri Sep 28 17:33:53 2012: Request 79924 was acted upon.
Transaction: Ticket created by nyet
       Queue: Win32-Pipe
     Subject: (No subject given)
   Broken in: 0.024
    Severity: Critical
       Owner: Nobody
  Requestors: [email protected]
      Status: new
 Ticket <URL: https://rt.cpan.org/Ticket/Display.html?id=79924 >


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.

>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
Win32API-File.pl (application/octet-stream, 1.8 KB)
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
Win32-Pipe-Client.pl (application/octet-stream, 439 B)
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";
}
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.