in reply to Nonblocking read server

Suggestions:

  1. Use an Async package if you can.
  2. Do *not* do non-blocking.
  3. Only do one sysread per ->can_read (thus non-blocking not needed)
  4. sysread return 0 indicates closed connection.
  5. Don't worry about how much data can be read, just sysread some big size, and sysread will give you what it can.
  6. Forget ->atmark and ->eof.

Replies are listed 'Best First'.
Re^2: Nonblocking read server
by Carbonblack (Novice) on Aug 30, 2019 at 06:55 UTC

    Thank You very much for your advice!

    That was the answer i feared ;-) But it makes it a lot easier.

    I think, I'll avoid fork or Async etc. this time since i expect only a one way communication and i want to use as less resources as possible.

      Just to show you why I recommended an Async package, here's your problem done using my own Async::Tiny. It should be similarly short in any of the other Async packages.

      #!/usr/bin/perl use strict; use warnings; use Async::Tiny; use Path::Tiny; use constant PORT1 => 5000; my $connID; my $t = Async::Tiny->new; $t->addListenCallback( PORT1, sub { my $sock = shift; $t->addReadCallback($sock, \&process_message, $sock->peerhost, ++$co +nnID); $t->changeReadMode($sock, 'full'); }); $t->eventloop; sub process_message { my ($data, $peerhost, $id) = @_; my $filename = "rcv_${id}_$peerhost.txt"; path($filename)->spew_raw($data); print "[save process_message] wrote $filename\n"; }

      See - nice, simple, clean, short :)

      Where Async::Tiny is

        > my own Async::Tiny

        Any plans to CPANify it?

        map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]