http://www.perlmonks.org?node_id=968688


in reply to UDP connection

Unlike some others in this thread, I don't think it's impossible to use UDP to send data in two directions: a heartbeat from the client to the server, and data from the server to the client. In fact, I don't see any particular difficulties -- with IP over 30 years old, we have learned to distinguish between source and destination addresses in IP packets (not that this ever was a problem).

You want to do something like the following (untested pseudo code):

... create sockets ... my @fileno = (fileno(SOCKET1), fileno(SOCKET2), fileno(SOCKET3), fileno(SOCKET4)); my ($rbits, $ebits); vec($rbits, $_, 1) = 1 for @fileno; vec($ebits, $_, 1) = 1 for @fileno; while (1) { my $time_to_next_heartbeat = ...; # How long till we have to send + a hb if ($time_to_next_heartbeat <= 0) { ... send heartbeats ... redo; } if (select (my $rbits = $rbits, undef, my $ebits = $ebits, $time_t +o_next_heartbeat)) { if ($ebits) { ... deal with errors ... } if ($rbits) { foreach my $fileno (@fileno) { if (vec($rbits, $filen0, 1)) { ... read data ... } } } } }
There's a much simpler solution though, and that may work for you: