Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Read/Write IO on socket file descriptor

Reply
Thread Tools

Read/Write IO on socket file descriptor

 
 
Patrick LeBoutillier
Guest
Posts: n/a
 
      07-20-2003
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
 
Reply With Quote
 
 
 
 
A. Sinan Unur
Guest
Posts: n/a
 
      07-20-2003
http://www.velocityreviews.com/forums/(E-Mail Removed) (Patrick LeBoutillier) wrote in
news:(E-Mail Removed) om:

> 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
(E-Mail Removed)
Remove dashes for address
Spam bait: (E-Mail Removed)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
socket.unbind or socket.unlisten? - socket.error: (48, 'Addressalready in use') Laszlo Nagy Python 1 01-27-2009 05:05 PM
[socket] test socket descriptor state mbm C Programming 2 09-27-2007 02:48 PM
socket and threading ... bad file descriptor hg Python 1 09-14-2006 12:14 PM
how to read data from a socket given a file descriptor Rajarshi Guha Python 0 04-07-2004 04:10 PM
Read/Write IO on socket file descriptor? Patrick LeBoutillier Perl 0 07-19-2003 05:25 PM



Advertisments