kodo has asked for the wisdom of the Perl Monks concerning the following question:

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

Replies are listed 'Best First'.
Re: IO::Socket::INET what to use to read/write from socket?
by {NULE} (Hermit) on May 29, 2002 at 15:54 UTC
    Hi,

    To avoid buffered read and writes use sysread and syswrite. There is information on these in the IO::Socket and IO::Select perldoc. The big advantage of these is that you can do non-blocking access. The down-side is that they are a hair more complex.

    An excellent source of all the networking that is Perl is Lincoln Stein's excellent Network Programming with Perl. You can figure this stuff out with just the perldoc, though.

    Good luck,
    {NULE}
    --
    http://www.nule.org

    P.S. I'm at a training class all week so I don't have much opportunity to research my posts as well as I normally do. Forgive me for not providing links to all of the things that I mention here.

Re: IO::Socket::INET what to use to read/write from socket?
by choocroot (Friar) on May 29, 2002 at 11:39 UTC
    I'm not an expert in Perl networking but I would say that you can use <$socket> for simple text/line based protocols (like basic HTTP client/server), and use read($socket, $data, $bufsize) for "binary" protocols and to get more control on the flow of information with non blocking I/O for example ...
    BTW, I'm also waiting for comments from experienced monks on this subject :)
      Well, I've used <$socket> calls for 'semi-binary' protocols: i've had to interface with a server that sends messages wrapped in \02 .. \03 characters, so I just set $/ to "\03" and read complete messages using <$socket>

      This still only works when you can use blocking calls (i.e. request/response protocols) though.

      -- Joost downtime n. The period during which a system is error-free and immune from user input.