Why are you testing while ($bytes_read == 1024) and not simply while ($bytes_read), which would continue as long as at least one byte has been read...?

For some reason while ($bytes_read) never stops, it will just sit there forever. Everyone else seems to be able to use it fine, but I'm having trouble with it.

And I don't want to use LWP because I won't always necessarily be working with http, it's just what I'm using to test out the script for now

ikegami: The problem is that I don't necessarily know what protocols my server will be interacting with (could be http, ftp, torrents, anything really). What I'm doing is basically coding a socks5 proxy (of sorts). I have no control over whether the remote host is going to tell me how long the message is, or if it ends with a specific character or not

I've been looking at Michael Auerswald's perl socks5 implementation, and the relevant part of the code is this:

if ($client && (vec($eout, fileno($client), 1) || vec($rout, fileno +($client), 1))) { my $result = sysread($client, $tbuffer, 1024); if (!defined($result) || !$result) { return; } } if ($target && (vec($eout, fileno($target), 1) || vec($rout, fil +eno($target), 1))) { my $result = sysread($target, $cbuffer, 1024); if (!defined($result) || !$result) { return; } } while (my $len = length($tbuffer)) { my $res = syswrite($target, $tbuffer, $len); if ($res > 0) { $tbuffer = substr($tbuffer, $res); } else { retur +n; } } while (my $len = length($cbuffer)) { my $res = syswrite($client, $cbuffer, $len); if ($res > 0) { $cbuffer = substr($cbuffer, $res); } else { retur +n; } }
So he basically reads a set amount, sends it off, then goes back and reads again, and the delay which I need would be created by the sending of the new information to the next machine. This will work for what I need, but is it really all that reliable?

In reply to Re: Getting sysread to read the full packet by MediocreGopher
in thread Getting sysread to read the full packet by MediocreGopher

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.