I am trying to write a simple, at least what I thought was going to be, TCP client to talk a piece of telco equipment to do provisioning via a CGI frontend(because I am NOT going to be responsible for teaching 20 guys the TL1 command language). In short I am having trouble terminating my read on the socket after I issue commands. I see in alot of the docs to use while(defined($line = <$sock>)) or @lines = <$sock>. Thats doesn't seem to work. I tried using select and that hangs as well. How would I be able to terminate a read fromthe socket when the last line of data comes in? Is there a way to do this without matching a string? I seem to be losing the last characeter in my responses as well, or the last line. The command prompt on this system is a ";" and I never see it in the STDOUT stream but I do see it if I sniff the session. heres the code:
#!/usr/bin/perl -w use IO::Socket; use IO::Select; use strict; ##INITIALIZE VARS## my $host = ($ARGV[0] || 'localhost'); my $port = ($ARGV[1] || '5501'); my $cmd1 = "act-user::dacsmaster:jdv::passwd;\n"; my $cmd2 = "rtrv-crs-t1::all:jdv;\n"; ##CREATE SOCKET## my $sock = IO::Socket::INET->new(PeerAddr => "$host", PeerPort => "$port", Proto => 'tcp', Type => SOCK_STREAM ); die "Socket could not be created. Reason: $!\n" unless $sock; $sock->autoflush(1); ##WRITE TO SOCKET## my $length = length($cmd1); syswrite($sock,$cmd1,$length); ##READ FROM SOCKET## my $read = ''; # Initialise to an empty set # (NOTE: $read=0 is very wrong) vec($read, fileno($sock), 1) = 1; # Set the appropriate bit #vec($read, fileno(FILE2), 1) = 1; # And for another file... for(;;) { my $found = select($read, undef, undef, undef); print "while loop ran\n"; # Does FILE1 have data waiting? if (vec($read,fileno($sock), 1)) { sysread($sock,my $line,1024); print "LINE:"."$line\n"; } else { print "nothing to read\n!"; last; } } print "after for loop\n"; ##CLOSE SOCKET AND EXIT## close($sock) || die "$!"; exit;

In reply to socket reading... by jdv79

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.