I haven't used IO::Socket much, so this was interesting practice. Normally, I just use Socket, which is a far sight better than the Perl4 method using pack(). Hash-style named parameters are all the rage these days, and I'm not complaining. I really should port all my stuff over as soon as I can.#!/usr/bin/perl -w use strict; use IO::Socket; my ($host) = "www.perlmonks.org"; my ($port) = "http(80)"; my ($socket) = new IO::Socket::INET (PeerAddr => $host, PeerPort => $port, Proto => 'tcp'); die "Can't connect to $host:$port\n" unless $socket; my ($rfd) = ''; # Must be initialized by string, # not numeric my ($timeout) = 5.0; # 5s timeout my ($block_size) = 10_000_000; # Buffer "size" at ~10MB $|++; # Unbuffer STDOUT # Send a sample transaction via HTTP $socket->write ("GET / HTTP/1.0\r\nHost: $host\r\n\r\n"); $socket->flush(); # Wait loop to receive content while (1) { # Set the bit-flag for the socket you are waiting on = 1 vec ($rfd, $socket->fileno(), 1) = 1; # Wait for something to happen if (select ($rfd, undef, undef, $timeout) >= 0 && vec($rfd, $socket->fileno(), 1)) { # Something came in! my ($buffer); # Check what it is by calling read() on the socket my ($result) = $socket->read ($buffer, $block_size); # Print out what came in print $buffer; # Drop out of the loop if read() returns a zero # value, indicating EOF. last unless $result; } else { # Timed out on the select(), so bail out. last; } }
In reply to Re: Re: Re: how do I read from a socket one byte at a time?
by tadman
in thread how do I read from a socket one byte at a time?
by reyjrar
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |