Hi,

Using perl-5.14.2 in Windows 7, I am trying following code using socketpair
to communicate between two process. The data is properly being communicated
but the socket on spawned process (testchild.pl) act in blocking mode using
sysread(..) irrespective of forcing it to non-blocking. I could confirm
that the socket is properly set to non-blocking based on the return value
from ioctlsocket(..). If I replace sysread(..) with recv(..) in testchild.pl,
I receive EWOULDBLOCK as expected. On debugging perl source code, I
realized that sysread(..) implementation check for IoTYPE(io) ==
IoTYPE_SOCKET and call recvfrom(..) or read(..) based  on the check. For
IO::Socket(..) created from windows osfhandle, the type is not set to
socket. Is there any way to force it? What's the right way to create
IO::Socket(..) object from windows osfhandle?

# testparent.pl

use Win32API::File;
use IO::Handle;
use Symbol qw(gensym);
use IO::Socket;
use Win32::Process;

sub win32_spawn_child {
  my $program  = shift;
  my $cmd  = shift;
  my $inherit = shift;

  if( ! Win32::Process::Create($last_process, $program, $cmd,
$inherit+, &CREATE_NO_WINDOW, ".") ) {
    print "$^E\n";
    return 0;
  }

  my $pid = $last_process->GetProcessID() || do { print "$^E\n"; };

  return $pid;
}


my ($child_socket, $parent_socket);

socketpair($child_socket, $parent_socket,
               Socket::AF_UNIX, Socket::SOCK_STREAM, Socket::PF_UNSPEC+)
        or die "Unable to create socketpair for transfer agent: $!";

$child_socket->autoflush(1);
$parent_socket->autoflush(1);

my $perl = "c:\\perl-5.14.2\\bin\\perl.exe";

my $handle = Win32API::File::GetOsFHandle($parent_socket);
my $handle1 = Win32API::File::GetOsFHandle($child_socket);

print syswrite($child_socket, "abcdef\n"), "\n";
win32_spawn_child($perl, "$perl c:\\temp\\JOBCON-2902\\testchild.pl
$h+andle $handle1", 1);
close $parent_socket;
$child_socket->blocking(1);

my $buf;
my $bytes_read = sysread($child_socket, $buf, 1024);
print $buf, "\n";
exit 0;
[download] 
<http://www.perlmonks.org/?abspart=1;displaytype=displaycode;node_id=1043296;part=1>

# testchild.pl

use IO::Socket;
use Win32API::File;
use Symbol qw(gensym);

my $buf;
my $handle = gensym();
my $handle1 = gensym();

Win32API::File::OsFHandleOpen($handle, $ARGV[0], "rw");
Win32API::File::OsFHandleOpen($handle1, $ARGV[1], "rw");
$handle1->close();
$handle = IO::Socket->new_from_fd($handle, "r+");
$handle->blocking(0);

#while (defined (my $bytes_read = sysread($handle, $buf, 1024))) {
while (defined (my $bytes_read = recv($handle, $buf, 1024, 0))) {
  if($bytes_read) {
    open FILE, ">> debug.txt";
    print FILE $bytes_read, "\n";
    print FILE $buf, "\n";
    close FILE;
  } else {
    open FILE, ">> debug.txt";
    print FILE "eof\n";
    close FILE;
    exit 0;
  }
}
if( $! == 10035 ) {
    open FILE, ">> debug.txt";
    print FILE "ewouldblock", "\n";
    close FILE;
}
print syswrite($handle, "abcdef\n"), "\n";

exit 0;

-Karthik
_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to