Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

socket help

by nagalenoj (Friar)
on Apr 23, 2010 at 12:14 UTC ( #836510=perlquestion: print w/ replies, xml ) Need Help??
nagalenoj has asked for the wisdom of the Perl Monks concerning the following question:

The following code isn't working. What is wrong?
use IO::Socket::INET; use IO::Select; my ($sckt_one, $sckt_two, $s); $sckt_one = IO::Socket::INET->new ( PeerAddr => "localhost", PeerPort=> 9000, Proto => "tcp", Type => SOCK_STREAM ); die "Could not create socket:1: $!\n" unless $sckt_one; $sckt_two = IO::Socket::INET->new ( PeerAddr => "localhost", PeerPort=> 9001, Proto => "tcp", Type => SOCK_STREAM ); die "Could not create socket:2: $!\n" unless $sckt_two; $s = IO::Select->new($sckt_one, $sckt_two); while(@ready = $s->can_read) { foreach $fh (@ready) { print <$fh>; } }

Comment on socket help
Download Code
Re: socket help
by nagalenoj (Friar) on Apr 23, 2010 at 12:16 UTC
    Sorry! Posted in curiosity. But, now I've found. It is because of
    print <$fh>;
    Need to do like this.
    $var = <fh>; print $var;

    When we do something like 'print <fh>' it reads till EOF and prints entire content. Since, I've tried in sockets, I wouldn't have received EOF. So, the above code didn't work as expected.

      FWIW, this could also be achieved by saying

      print scalar <$fh>;

      (the <> operator (aka readline) behaves differently depending on context — which by default is "list" with print)

      I'm glad you figured it out. For reference in the future, it helps a lot if you can specify what your expected results are, and what your actual results are, to help diagnose the problem.

      Good job on fixing it though!

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (6)
As of 2014-09-19 22:21 GMT
Find Nodes?
    Voting Booth?

    How do you remember the number of days in each month?

    Results (149 votes), past polls