Read/Write IO on socket file descriptor

Discussion in 'Perl Misc' started by Patrick LeBoutillier, Jul 20, 2003.

  1. Hi all,

    I'm trying to perform read and write I/O on a socket file descriptor
    received for another process via a Unix Domain Socket. In trying to
    understand all this I came up with a small test script that is not
    working for me:


    use strict ;
    use IO::Socket::INET ;

    my $socket = new IO::Socket::INET(
    PeerAddr => 'www.perl.com',
    PeerPort => 80,
    Proto => 'tcp',
    ) ;

    my $rfd = fileno($socket) ;
    my $rfh = new IO::Handle->fdopen($rfd, "r") ;
    my $wfd = fileno($socket) ;
    my $wfh = new IO::Handle->fdopen($wfd, "w") ;

    print "$rfd $rfh $wfd $wfh\n" ;
    print $wfh "GET / HTTP/1.0\n\n" ;
    print "Sent GET...\n" ;
    my $line = <$rfh> ;
    print $line ;


    Should this work? It seems as though printing to $wfh
    does nothing, and then the <$rfh> is hanging.

    If this is not the way to go about this, what else can I do
    to perform read AND write I/O on a socket file descriptor?

    I'm using Perl 5.6.1 on Linux RH 7.1

    Thanks,

    Patrick LeBoutillier
    Patrick LeBoutillier, Jul 20, 2003
    #1
    1. Advertising

  2. (Patrick LeBoutillier) wrote in
    news::

    > Hi all,
    >
    > I'm trying to perform read and write I/O on a socket file descriptor
    > received for another process via a Unix Domain Socket. In trying to
    > understand all this I came up with a small test script that is not
    > working for me:


    I am no expert so please take what I say with a grain of salt.

    > use strict ;
    > use IO::Socket::INET ;
    >
    > my $socket = new IO::Socket::INET(
    > PeerAddr => 'www.perl.com',
    > PeerPort => 80,
    > Proto => 'tcp',
    > ) ;
    >
    > my $rfd = fileno($socket) ;
    > my $rfh = new IO::Handle->fdopen($rfd, "r") ;
    > my $wfd = fileno($socket) ;
    > my $wfh = new IO::Handle->fdopen($wfd, "w") ;


    $wfh->autoflush(1);

    > print "$rfd $rfh $wfd $wfh\n" ;
    > print $wfh "GET / HTTP/1.0\n\n" ;
    > print "Sent GET...\n" ;
    > my $line = <$rfh> ;
    > print $line ;



    Also, I am not sure why you are creating $rfh and $wfh. $socket can be read
    from and written to using regular Perl syntax:

    #! C:/Perl/bin/perl.exe

    use strict;
    use warnings;

    use IO::Socket::INET;

    my $socket = new IO::Socket::INET(
    PeerAddr => 'www.perl.com',
    PeerPort => 80,
    Proto => 'tcp');

    die "Cannot open connection: $!\n" unless $socket;

    print $socket "GET / HTTP/1.0\n\n";
    print "Sent GET...\n";
    my $line = <$socket>;
    close $socket;

    print $line, "\n";

    __END__

    Sinan.
    --
    A. Sinan Unur

    Remove dashes for address
    Spam bait: mailto:
    A. Sinan Unur, Jul 20, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Patrick LeBoutillier

    Read/Write IO on socket file descriptor?

    Patrick LeBoutillier, Jul 19, 2003, in forum: Perl
    Replies:
    0
    Views:
    3,056
    Patrick LeBoutillier
    Jul 19, 2003
  2. anon
    Replies:
    1
    Views:
    417
    Donn Cave
    Mar 5, 2004
  3. Rajarshi Guha
    Replies:
    0
    Views:
    347
    Rajarshi Guha
    Apr 7, 2004
  4. mbm

    [socket] test socket descriptor state

    mbm, Sep 27, 2007, in forum: C Programming
    Replies:
    2
    Views:
    422
    CBFalconer
    Sep 27, 2007
  5. Replies:
    19
    Views:
    1,255
    Nigel Wade
    Mar 15, 2012
Loading...

Share This Page