Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

Reading an incoming Data Stream..

by basicdez (Pilgrim)
on Feb 25, 2002 at 20:15 UTC ( #147367=perlquestion: print w/replies, xml ) Need Help??

basicdez has asked for the wisdom of the Perl Monks concerning the following question:

Greetings to all monks.

Please help me with this dilemma if you can. I am reading in a data stream through a socket and would like to read in the data stream beginning with "FOO" and ending with "END". I can read this in one line at a time, which works out perfectly for me, but I cannot get the reading in through this socket to stop when this hits END. What is happening is that I can see it reading in END, but the socket never closes and the listener continues to listen for more data through the socket. If you have any wisdom in this area I would appreciate your help.

Here is the data file.
"FOO" "localhost" "pd2x" "GOO" "Internal" "External" "Abbreviated" 00 50000 0 "ICK" "REQUEST" 0 "Yes" "No" "Absolutely" "END"
And here is the code I am using to read it in.
#!/usr/bin/perl -w # server use strict 'refs'; use strict 'subs'; use diagnostics; use strict; use sigtrap; use IO::Select; use IO::Socket::INET; $| = 1; ### Enable Autoflushing of I/O. my $sock = new IO::Socket::INET ( LocalAddr => localhost, LocalPort => 1000, Proto => 'tcp', Listen => 5, Reuse => 1, ); die "Could not create socket\n" unless $sock; my $buf = ' '; my $buffer = ' '; my $new_sock; while ($new_sock = $sock->accept()) { while (defined ($buf = <$new_sock>)) { if ($buf eq "\"END\"") { close ($sock); } else { print $buf; $buffer .= $buf; } } } print "$buffer \n";

peace, LOVE and ((code))


Replies are listed 'Best First'.
Re: Reading an incoming Data Stream..
by dragonchild (Archbishop) on Feb 25, 2002 at 21:08 UTC
    Better would be one of the two following snippets:
    while (defined ($buf = <$new_sock>)) { if ($buf =~ /"END"/) { } } ---- while (defined ($buf = <$new_sock>)) { chomp $buf; if ($buf eq '"END"') { } }
    The first does a regex, avoiding the newline issue. The second removes the trailing newlines, then does a string comparison.

    We are the carpenters and bricklayers of the Information Age.

    Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

Re: Reading an incoming Data Stream..
by dws (Chancellor) on Feb 25, 2002 at 20:33 UTC
    I don't see a chomp() in there anywhere, so I'll bet you have a newline hanging on to the end of the string. Try
    if ($buf eq "\"END\"\n") ^^
Re: Reading an incoming Data Stream..
by zengargoyle (Deacon) on Feb 26, 2002 at 00:57 UTC
    $| = 1; ### Enable Autoflushing of I/O.

    Should be...

    $| = 1; ### Enable Autoflushing of current STDOUT + filehandle

    Later on in your code development it's not going to turn on Autoflushing on your sockets. Just in case you didn't know. (i'm picky at the moment).

Re: Reading an incoming Data Stream..
by little (Curate) on Feb 26, 2002 at 17:02 UTC
    a small note aside:
    use strict 'refs'; use strict 'subs'; #later use strict;
    When you use strict without an import list all strict pragma variants will be applied, so it is equal of saying "use strict 'refs','subs','vars';" :-)

    Have a nice day
    All decision is left to your taste

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://147367]
Approved by root
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (1)
As of 2022-01-26 05:02 GMT
Find Nodes?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:

    Results (69 votes). Check out past polls.