Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

The question you've asked is two-part -- you want to both send and recieve a UDP message. chromatic addressed the former already, so I'll supply the latter. Here's a short script, using IO::Socket, which sends a UDP packet to some server, and then listens and prints out one (and one only) UDP packet that it gets in response.

#!/usr/bin/perl -w $|++; use strict; use IO::Socket; my $response = IO::Socket::INET->new(Proto=>"udp",LocalPort=>2424) or die "Can't make UDP server: $@"; my $message = IO::Socket::INET->new(Proto=>"udp",PeerPort=>4242,PeerAd +dr=>"") or die "Can't make UDP socket: $@"; $message->send("Ping!"); my ($datagram,$flags); $response->recv($datagram,42,$flags); print "Got message from ", $response->peerhost,", flags ",$flags || "n +one",": $datagram\n";

One thing to note is that we make the server to catch the response before we send out our query -- that way, the response can't "fall between the cracks," so to speak; it can't come back before we have the server up and ready. Another thing to note is that the "42" in there is the maximum length of the packet that the server will read. So if you get more data than this, it'll get truncated. Thus, you might want to make this rather big if you don't know how much data you'll be getting.

Just for kicks, I'll leave the above-mentioned computer ( running a UDP server on port 4242 so y'all can send it messages ad nauseum. ;> Here are the basics of the "server" that I'm running -- except the return UDP packet that you'll actually get may be more interesting than just "PONG!" Tip: Run it more than once. ;>

Update: I took it down, so you'll have to send UDP messages to yourself if you want to test it.

#!/usr/bin/perl -w $|++; use strict; use IO::Socket; my $server = IO::Socket::INET->new(LocalPort=>4242,Proto=>"udp") or die "Can't create UDP server: $@"; my ($datagram,$flags); while ($server->recv($datagram,42,$flags)) { my $ipaddr = $server->peerhost; print "Oooh -- udp from $ipaddr, flags ",$flags || "none",": $datagr +am\n"; my $response = IO::Socket::INET->new(Proto=>"udp",PeerHost=>$ipaddr, +PeerPort=>2424); $response->send("PONG!"); }

perl -pe '"I lo*`+$^X$\"$]!$/"=~m%(.*)%s;$_=$1;y^+*`^ ve^#$&V"+@( NO CARRIER'

In reply to Re: Simple UDP example anyone? by Chmrr
in thread Simple UDP example anyone? by Anonymous Monk

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 the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others taking refuge in the Monastery: (10)
    As of 2021-01-26 12:29 GMT
    Find Nodes?
      Voting Booth?