2
votes

I am implementing a perl based UDP server/client model. Socket functions recv() and send() are used for server/client communication. It seems that send() takes the return address from the recv() call and I can only get the server to respond back to the client that sent the initial request. However, I'm looking for the server to send the data to all active clients instead of only the source client. If the peer_address and peer_port for each active client is known, how could I use perl send() to route packets to the specific clients? Some attempts:

foreach (@active_clients) {
  #$_->{peer_socket}->send($data);
  #$socket->send($_->{peer_socket}, $data, 0, $_->{peer_addr});
  #send($_->{peer_socket}, $data, 0, $_->{peer_addr});
  $socket->send($data) #Echos back to source client
}

Perldoc send briefly describes parameter passing. I have attempted to adopt this and the result is either 1) The client simply receives the socket glob object or 2) the packet is never received or 3) an error is thrown. Any examples of how to use send() to route to known clients would be much appreciated.

1

1 Answers

3
votes

Yes, just store the addresses some place and answer on them:

# receiving some packets
while (my $a = recv($sock, $buf, 8192, 0)) {
    print "Received $buf\n";
    push @addrs, $a;
    # exit at some point
}

# send the answers
for my $a (@addrs) {
    send($sock, "OK", 0, $a);
}