H
Holger
Hi,
After having read quite a bit of postings on the topic it's still not
entirely clear to me.
I have working code that is a forever running client and which opens
just one connection (with blocking reads) to a server. Generally, it's
kind of a ping pong protocol, but the server also arbitrarily sends
data. I suppose that therefore I haven't run into a deadlock problem
yet of not sending often enough data that cause a response in return.
Here's the code frame:
#!/usr/bin/perl -w
use strict;
use IO::Socket;
use IO::Select;
# init snipped ...
$SIG{__DIE__} = \&SigDieHandler;
sub SigDieHandler
{
$socket->close if ( defined $socket );
$SIG{__DIE__} = 'DEFAULT';
exit( 0 );
}
my $socket = IO::Socket::INET->new( PeerAddr => $RemoteHost,
PeerPort => $RemotePort,
Proto => 'tcp',
Type => SOCK_STREAM )
or die "Could not connect to $RemoteHost:$RemotePort: $@\n";
my $data;
while ( defined $socket->recv( $data, 1028 ) ) {
# while processing $data ... {
print $socket $otherdatatosend;
# error handling snipped ...
# }
}
__END__
I've read to not use recv or sysread together with print on the same
socket. Does this apply to the entire socket or only per direction?
After all, the above works.
I'd like to convert the code into non-blocking call(s), but am a
little confused about the exact details, since I've only found
examples for the server side with listening sockets. Perhaps, I don't
see the wood for all the trees. I merely have a single connection, not
an entire pool.
So far I've come up with the following (btw, needs to work on win32,
too). But before changing the program I wanted to ask whether I'm on
the right track.
my $socket = IO::Socket::INET->new( PeerAddr => $RemoteHost,
PeerPort => $RemotePort )
or die "Could not connect to $RemoteHost:$RemotePort: $@\n";
my $select = IO::Select->new( $socket );
while ( 1 ) {
if ( $select->can_read( $timeout ) ) {
unless ( defined $socket->recv( $data, 1028 ) ) {
break;
}
else {
# process received $data
}
}
# do stuff, send data
}
$select->remove( $socket );
close $socket;
__END__
I also have a few questions:
What exact effects do values undef, 0, and e.g. 5 for $timeout have?
$select->can_read returns in this case either a list with one entry or
an empty list (on timeout). Correct?
Should I use sysread instead of recv?
Is the $select->remove necessary for cleanup?
Any thoughts on having the cleanup code in the die handler or not?
Best regards,
Holger
After having read quite a bit of postings on the topic it's still not
entirely clear to me.
I have working code that is a forever running client and which opens
just one connection (with blocking reads) to a server. Generally, it's
kind of a ping pong protocol, but the server also arbitrarily sends
data. I suppose that therefore I haven't run into a deadlock problem
yet of not sending often enough data that cause a response in return.
Here's the code frame:
#!/usr/bin/perl -w
use strict;
use IO::Socket;
use IO::Select;
# init snipped ...
$SIG{__DIE__} = \&SigDieHandler;
sub SigDieHandler
{
$socket->close if ( defined $socket );
$SIG{__DIE__} = 'DEFAULT';
exit( 0 );
}
my $socket = IO::Socket::INET->new( PeerAddr => $RemoteHost,
PeerPort => $RemotePort,
Proto => 'tcp',
Type => SOCK_STREAM )
or die "Could not connect to $RemoteHost:$RemotePort: $@\n";
my $data;
while ( defined $socket->recv( $data, 1028 ) ) {
# while processing $data ... {
print $socket $otherdatatosend;
# error handling snipped ...
# }
}
__END__
I've read to not use recv or sysread together with print on the same
socket. Does this apply to the entire socket or only per direction?
After all, the above works.
I'd like to convert the code into non-blocking call(s), but am a
little confused about the exact details, since I've only found
examples for the server side with listening sockets. Perhaps, I don't
see the wood for all the trees. I merely have a single connection, not
an entire pool.
So far I've come up with the following (btw, needs to work on win32,
too). But before changing the program I wanted to ask whether I'm on
the right track.
my $socket = IO::Socket::INET->new( PeerAddr => $RemoteHost,
PeerPort => $RemotePort )
or die "Could not connect to $RemoteHost:$RemotePort: $@\n";
my $select = IO::Select->new( $socket );
while ( 1 ) {
if ( $select->can_read( $timeout ) ) {
unless ( defined $socket->recv( $data, 1028 ) ) {
break;
}
else {
# process received $data
}
}
# do stuff, send data
}
$select->remove( $socket );
close $socket;
__END__
I also have a few questions:
What exact effects do values undef, 0, and e.g. 5 for $timeout have?
$select->can_read returns in this case either a list with one entry or
an empty list (on timeout). Correct?
Should I use sysread instead of recv?
Is the $select->remove necessary for cleanup?
Any thoughts on having the cleanup code in the die handler or not?
Best regards,
Holger