Phoenix.pm: Net::Telnet

Scott Walters scott at illogics.org
Wed Jan 14 19:44:03 CST 2004


The remote side should disconnect when it is done sending data.
Perl insulates you from errors, so it is easy not to ever notice
that you've been disconnected. Do this:

  while(read $fh, my $stuff, 8192) {
     # process $stuff
  }

read() returns 0 on EOF or undef on error. There is also an eof() 
built-in that is only valid after the first time you read from a 
socket (don't do while(!eof($fh)) { read stuff.. }, use a do { } while 
instead).

On  0, Matt Alexander <m at pdxlug.org> wrote:
> 
> On Wed, 14 Jan 2004, Scott Walters wrote:
> 
> > There is an example of raw sockets on phoenix.pm.org website,
> > but IO::Socket::INET is about the right level of abstraction for
> > what you're doing. Clearly Net::Telnet is too much of the wrong
> > abstraction. Raw sockets are usually too little abstraction.
> >
> > >From the man page:
> >
> >               $sock = IO::Socket::INET->new(PeerAddr => 'www.perl.org',
> >                                             PeerPort => '80',
> >                                             Proto    => 'tcp');
> >
> > You can then print to $sock, $sock->print("foo!\n"), read from it,
> > read $sock, my $buffer, 8192, read lines from it, my $line = readline $sock,
> > and so on, just like a normal IO::Handle.
> 
> I ended up using raw sockets and I can now grab all the data, but I'm not
> sure how to disconnect after a certain period of time.  The device I'm
> connecting to doesn't give any indication of when it's done sending data.
> So I basically need to grab everything, set an overall time limit of say,
> 5 seconds, and then disconnect after that time limit.
> Any suggestions?
> Thanks,
> ~M



More information about the Phoenix-pm mailing list