Hi,
Using CPAN source of 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. Any idea what's going wrong with sysread(..) and how to make this to work with sysread(..)?
# 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;
# 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
In reply to Issues in using socketpair in Windows by kartlee05
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |