I see
this is for a socket listener.
I'm
trying to extract data from Lucent switch and put it in a sql
database.
Is
there a reason to use IO::Select instead of IO::Socket::INET is this
case?
I
would use Net::Telnet but it chokes on the crazy charactors sent back from the
switch (It's An ATT 4410 session)
My
specific problem is that sysread blocks if there is nothing waiting at the
socket.
Is
this expected behavior?
What is it supposed to do?
Again
a portion of my code.
$main::socket =
IO::Socket::INET->new
(
PeerAddr => "$address",
PeerPort => "$port",
Proto => "tcp",
Type => SOCK_STREAM,
Timeout => "10"
) or log_it ("Could not establish the TCP connection to $address:$port\n",1);
(
PeerAddr => "$address",
PeerPort => "$port",
Proto => "tcp",
Type => SOCK_STREAM,
Timeout => "10"
) or log_it ("Could not establish the TCP connection to $address:$port\n",1);
$main::socket->autoflush(1);
my ($time_out,
$text, $i, $rec, $answer,$nread);
$text = shift @_;
$time_out = shift @_;
$rec = '';
do
{
$nread = sysread($main::socket, $answer, 1024);
print "\nNread $nread Test = \"$text\"\n";
$rec .= $answer;
#print $answer;
return $rec if ($rec =~ /\Q$text\E/sgmi);
sleep 1;
$i++;
} while ($nread && $i<$time_out);
$text = shift @_;
$time_out = shift @_;
$rec = '';
do
{
$nread = sysread($main::socket, $answer, 1024);
print "\nNread $nread Test = \"$text\"\n";
$rec .= $answer;
#print $answer;
return $rec if ($rec =~ /\Q$text\E/sgmi);
sleep 1;
$i++;
} while ($nread && $i<$time_out);
-----Original
Message-----
From: Morgan, Steve H. [mailto:[EMAIL PROTECTED]]
Sent: Tuesday, March 06, 2001 4:29 PM
To: 'Goddard, Jeremy'; Active Perl (E-mail)
Subject: RE: Another Sockets question
From: Morgan, Steve H. [mailto:[EMAIL PROTECTED]]
Sent: Tuesday, March 06, 2001 4:29 PM
To: 'Goddard, Jeremy'; Active Perl (E-mail)
Subject: RE: Another Sockets question
Jeremy,You may want to take a look atIO::Select->selectHopes this helps...If you would like the complete code, just e-mail me.Here is an example used in a perl NT service ( This is only a stub). This code is in the startup section.$sock = OpenConnection();
$readable_handles = new IO::Select();
$readable_handles->add($sock);while(ContinueRun()) {
# The select() will block until a socket is ready to be read
($new_readable) = IO::Select->select($readable_handles,undef,undef,5);#print "Loop\n";# If it comes here, there is at least one handle to read from or
# write to.
foreach $temp_sock (@$new_readable) {
if ($temp_sock == $sock) {
$new_sock = $temp_sock->accept();
# Got Connection Request, So send user Header, Prompt and
# wait for input
$readable_handles->add($new_sock);print "Got a Connection...\n";
} else {
# It is an ordinary client socket, ready for reading
$bytes_read = sysread($temp_sock,$buf,1);
if ($buf) {
$command = GetUserInput($temp_sock,$buf);
if ($command) {
# Move Cursor to beginning of next line of terminal
sendcrnewline($temp_sock);
($shutdown,$connected) = ProcessCommand($command,$temp_sock);
sendcommandline($temp_sock);
}
} else {
# Client closed socket. We do the same here, and remove
# it from the readable_handles list
$connection_cnt --;
print "Closed Connection (".$connection_cnt.")...\n";
$readable_handles->remove($temp_sock);
delete $User_Info{$temp_sock};
close($temp_sock);
}
}
}}# Close the Listening Socket
close ($sock);
Stephen Morgan
SYSTEMS ENGINEER
Z-Tel Communications, Inc.
404.504.7384 Phone
404.237.1167 Fax
[EMAIL PROTECTED]
-----Original Message-----
From: Goddard, Jeremy [mailto:[EMAIL PROTECTED]]
Sent: Monday, March 05, 2001 1:55 PM
To: Active Perl (E-mail)
Subject: Another Sockets questionHi,
I'm trying to using non-blocking IO to get data.
But for some reason after all data is read the read blocks.
Any Ideas?
for ($i=0; $i<$time_out; $i++)
{
sleep 1;
$nread = sysread($main::socket, $answer, 1024);
$rec .= $answer;
print $answer;
return $rec if ($rec =~ /\Q$pattern\E/);
return 0 if ($nread==0);
}Thanks,
Jeremy

