I'll assume you meant can_write (as the title of your question says) instead of can_read (as the body says).

The answer is that you simply don't know how much you can write. Do a syswrite of the stuff you want to write and then check the returncode to see how much actually got written. Remove that many bytes from the front of your buffer and if anything is still left, you'll have to check for can_write again before trying to write the rest. In principle you should have been able to write at least one byte, but on some operating systems it's possible that the available internal bufferspace got reused by the time you do the actual syswrite and the write can then block if the socket is blocking. That's why you should set your sockets to non-blocking even if you write a select-driven program, so that you'll get errno EAGAIN in that case (I never work on windows, but I believe there you would get EWOULDBLOCK instead. On UNIX these two are normally #defined to be equal).

(updated) It's also possible that the socket got closed (or half-closed) in the mean time, in which case you will get SIGPIPE when you try to write, and an EPIPE errno (which you'll only see if the program catches, blocks or ignores the signal). Beyond that you should consider every error return except EAGAIN or EINTR as fatal to your socket.

Notice that something like POE will take care of all these details for you.


In reply to Re: Filehandles and can_write, actual writable amount? by thospel
in thread Filehandles and can_write, actual writable amount? by BUU

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.