Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
You need to receive packets like this:
$peeraddr = $sock->recv($packet, $maxpacketsize); # 2048 should work fine as maxpacketetsize
You'll have to look into the details of the protocol to tell how many packets to expect. Packets may arrive out of order. This is not necessarily a problem--it appears that in the Gamespy protocol the packets contain key-value pairs which can be interpreted in any order and that these are never split between packets (although there may be more than on pair per packet). If you don't receive all the packets you expect within a certain period of time, try sending the query again. Don't try too many times, though.

UDP is not a connection-oriented protocol, so this:

if(! defined $sock->connected)
doesn't actually tell you if you've connected. It just returns the (binary) IP address/socket port address that you used for PeerAddr. It will return this even if there is no host at the given IP address. There is also no way to tell if a send/syswrite/print actually succeeds--that is, if the data actually reached the remote host.

For example, this

use IO::Socket; $p = IO::Socket::INET->new(PeerAddr => "1.1.1.1:7170", Proto => "udp") or die "socket: $!\n"; if ($p->connected) { print "connect ok\n"} if ($p->write("foo")) { print "send ok\n"}
will print:
connect ok send ok
You might want to look at this C# library for ideas, although the Perl string methods are much simpler than the byte buffer methods he uses in C#.

In reply to Re: Sending a low-level query using sockets? (A confused newbie) by Thelonius
in thread Sending a low-level query using sockets? (A confused newbie) by Anonymous Monk

Title:
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?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others examining the Monastery: (5)
    As of 2020-07-06 05:52 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found

      Notices?