Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
Hi dear monks , i try to make some kind of dhcp server with anyevent , here my test code :
#!/usr/bin/perl use Data::Dumper; use strict; use warnings; use EV; use Fcntl; use IO::Socket; use AnyEvent; $| = 1; $0 = 'DHCP manager'; my $pid; my %pids; my @pids; our %binds; my $serv; my $serv_ip = ''; socket($serv, PF_INET, SOCK_DGRAM, getprotobyname('udp')); fcntl($serv, F_SETFL, O_NONBLOCK); setsockopt($serv, SOL_SOCKET, SO_REUSEADDR, 1); my $new_child = sub { ### create socket for ipc my $child; my $parent; socketpair($child, $parent, AF_UNIX, SOCK_DGRAM, PF_UNSPEC); fcntl($child, F_SETFL, O_NONBLOCK); fcntl($parent, F_SETFL, O_NONBLOCK); $child->autoflush(1); $parent->autoflush(1); if (!defined($pid = fork())) {# print Dumper 'Cannot fork anymore'; die "cannot fork: $!"; } elsif ($pid) { # parent $pids{$pid}{'sock'} = $parent; $pids{$pid}{'on_finish'} = AnyEvent->child (pid => $pid, cb => + sub { ## when child die - start new one print Dumper 'CHILD Died'; new_child(); }); $pids{$pid}{'ev'} = AnyEvent->io (fh => $parent, poll => 'r', +cb => sub { my $buf; print Dumper $binds{'0019c632c637'}{'1'}{'001c238b8b02'}; }); push @pids , $pid; close $child; print Dumper "parent fork $pid"; } else { #child close $parent; undef %binds; $0 = 'DHCP worker'; $| = 1; my $fh = $child; my $w = AnyEvent->io (fh => $fh, poll => 'r', cb => sub { my $buf; recv($fh, $buf, 500, 0); send ($fh , $buf, 0); }); EV::loop(); die; } }; foreach (1..2){ &$new_child(); } bind ($serv, sockaddr_in('67', inet_aton($serv_ip))); listen($serv, SOMAXCONN); my $server_hdl = AnyEvent->io (fh => $serv, poll => 'r', cb => sub { my $buf; my $fromaddr = recv($serv, $buf, 500, 0) or print Dumper $!; return if ($!); return if (length($buf) < 236); my $hdl = shift @pids; my ($port, $addr) = unpack_sockaddr_in($fromaddr); $addr = inet_ntoa($addr); print Dumper "$$ Master get pocket from $addr"; send ($pids{$hdl}{'sock'} , $addr.' '.$buf , 0); push @pids , $hdl; }); print "Master listening\n"; $binds{'0019c632c637'}{'1'}{'001c238b8b02'}{'ip'} = ''; $binds{'0019c632c637'}{'1'}{'001c238b8b02'}{'router'} = ''; $binds{'0019c632c637'}{'1'}{'001c238b8b02'}{'mask'} = ''; EV::loop();
when i test this code - some times i get undef value on "print Dumper $binds{'0019c632c637'}{'1'}{'001c238b8b02'};" , can anyone help me ? ps: when i create keys in hash %binds before fork - all is perfect

In reply to anyevent + fork = problem by hithim

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others musing on the Monastery: (5)
    As of 2018-03-18 02:31 GMT
    Find Nodes?
      Voting Booth?
      When I think of a mole I think of:

      Results (228 votes). Check out past polls.