I'm working on a project that works with a line by line protocols (POP3 and SMTP). I'm running into a problem where IO::Select->can_read is reporting that a socket doesn't have anything on it when it does. This only happens when I'm reading a bunch of lines from the socket (retrieving a message). Here's a boiled down test example that should connect, get a specified message, and then quit. I should probably be keeping track of states to know what to send next, but this is just to figure out the can_read problem. It hangs on the same line all the time, but will print everything out if more data comes in on the socket.
#!/usr/bin/perl
use IO::Socket;
use IO::Select;
$user = shift || die "Usage: $0 user password server msgnum\n";
$pass = shift || die "Usage: $0 user password server msgnum\n";
$server = shift || die "Usage: $0 user password server msgnum\n";
$msgnum = shift || die "Usage: $0 user password server msgnum\n";
sub setgreen {
print "\033[00;32m";
}
sub setred {
print "\033[00;31m";
}
sub setnormal {
printf "\033[00m";
}
$dstsock = IO::Socket::INET->new(
PeerAddr => $server,
PeerPort => 110,
Proto => 'tcp');
$readables = IO::Select->new();
$readables->add($dstsock);
$notdone = 1;
while ($notdone) {
my @ready = $readables->can_read(0);
foreach $sock (@ready) {
$data = <$dstsock>;
if ($data) {
setred;
print "$data"; setnormal;
if ($data =~ /^\+OK Qpopper/) {
print $dstsock "user $user\r\n";
setgreen();
print "user $pass\r\n";
setnormal;
}
if ($data =~ /^\+OK Password/) {
print $dstsock "pass $pass\r\n";
setgreen();
print "pass $pass\r\n";
setnormal;
}
if ($data =~ /^\+OK $user/) {
print $dstsock "retr $msgnum\r\n";
setgreen();
print "retr $msgnum\r\n";
setnormal;
}
if ($data =~ /^\.$/) {
print $dstsock "quit\r\n";
setgreen();
print "quit\r\n";
setnormal;
}
} else {
$notdone = 0;
close $dstsock;
close $srcsock;
}
}
}