Cap'n Steve has asked for the wisdom of the Perl Monks concerning the following question:

I've gotten started with this node (the POD for Socket.pm and IO::Socket are seriously lacking for newbies), and I've got the code to send data, but I'm still not sure how to receive the answer.

I just need to send some data to a specific UDP port and get a response. Do I even need a socket for this? Any help or a link to a very basic tutorial would be great.
  • Comment on I need simple socket tutorial (or any other way to connect via UDP).

Replies are listed 'Best First'.
Re: I need simple socket tutorial (or any other way to connect via UDP).
by davidrw (Prior) on Aug 16, 2005 at 22:43 UTC
Re: I need simple socket tutorial (or any other way to connect via UDP).
by Zaxo (Archbishop) on Aug 16, 2005 at 23:16 UTC

    To get the server's response, recv.

    The Perl Cookbook has good examples of using sockets.

    After Compline,
    Zaxo

      As a side note, why is the recv() subroutine set up so strangely? Passing the variable that will hold what the function returns reminds me of PHP and its "forced references". Wouldn't this code be much more intuitive and Perlish?

      $reponse = $socket->recv($length, $flags);

      And for that matter, how is this even done? I would think you'd at least need the name of the variable to set it from another package. Isn't passing an uninitialized variable the same as passing an empty string? I'm all confused, perhaps someone could explain what I think are the relevant bits of recv() in IO::Socket:
      my $sock = $_[0]; ${*$sock}{'io_socket_peername'} = recv($sock, $_[1]='', $len, $flags);
Re: I need simple socket tutorial (or any other way to connect via UDP).
by zentara (Cardinal) on Aug 17, 2005 at 12:41 UTC
Re: I need simple socket tutorial (or any other way to connect via UDP).
by kprasanna_79 (Hermit) on Aug 17, 2005 at 04:33 UTC
    Hi,
    Please check this link

    Update:-
    Since IO::Socket which is explained clearly and more in detail here in the above mentioned link i have used pm search and gave resultant page here. I think this will give u some idea to solve ur issues.
    -prasanna.k
Re: I need simple socket tutorial (or any other way to connect via UDP).
by Cap'n Steve (Friar) on Aug 18, 2005 at 04:20 UTC
    Thanks for all the great links, here's the code I have now, as a very basic example to anyone who might find this thread:
    use IO::Socket::INET; my $socket = IO::Socket::INET->new( 'PeerAddr' => '192.168.1.100', 'PeerPort' => 29900, 'Proto' => 'udp', 'Timeout' => 15 ); $socket->autoflush(1); $socket->send($query); $socket->recv(my $response, 1024 * 5); print $response;
    The only thing I'm worried about is that the first time I tried this, I was downloading something and the script just hung without returning anything. This is going to be going on a shared server, so I'm worried about hogging resources. Is there any way to guarantee that the script won't wait too long for the information?