Hi monks,
I've read some nodes about using IO::Socket::INET with tcp/udp protocol and there have been lots of different methods of reading/writing to a open socket.

Also I've read that <$socket> should never be used to read from any socket, because it would use CRs to EOF the Packet.

I've seen some people using read(), write() and some other who used sysread(), syswrite(). Of course we also have $socket->recv() and $socket->send() from IO::Socket.

So I wonder which one is best to use for what cases and why. I've ordered the book "Network Programming with Perl" from lincoln stein but it'll take some weeks until it's here (I hope such questions are answered in that book).

I understood that when using UDP it doesn't make much sense to do something like while (<$socket>), because UDP uses DGRAM and no STREAM for sending/receiving data.

I've also tried to use $socket->recv() with TCP like this:
while (1) { $socket->recv($data, 1024); print $data; last if $data eq ''; }
but I don't think this is a good way, so I've used
while (read($socket, $data, 1024)) { print $data; }
So what's the savest/best way to read from sockets and why? Thanks for any answers/links in advantage who could help me to understand this a bit more.

greetings,

giant

In reply to IO::Socket::INET what to use to read/write from socket? by kodo

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.