http://qs321.pair.com?node_id=602704

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I need the ability to query an online gaming server, so I figured Perl would be a good candidate for the job. What i need to do is, using a minimal protocol (called gamespy2), I send a series of bytes to the target server on a specific port using udp, and it should give me a response back. Sounds simple, right? I got the impression that sockets would be suitable for this, but I have no experience with them, and am having trouble with this code:
use strict; use warnings; use Carp; use IO::Socket; use Smart::Comments; # hostname and port of server to query my $hostname = '88.191.48.146:27888'; ### Creating socket... my $sock = new IO::Socket::INET( PeerAddr => $hostname, LocalAddr => 'localhost', Proto => 'udp', ) or croak "can't bind: $@\n"; # die unless socket is verifiably connected if(! defined $sock->connected) { croak 'socket failed to connect'; } # build query string my $query_string = sprintf "\xFE\xFD\x00%s\xFF\xFF\xFF", 'RTFM'; ### Sending query string... $sock->print($query_string); ### Retrieving result... my $response; while(<$sock>) { $response .= $_; } ### Closing socket... $sock->close; ### We're done here, exit... exit;
It establishes the connection to the remote server just fine, and even sends the query data, but it hangs in the while loop after "Retrieving Result...", which leads me to believe I'm doing this incorrectly. When I connect to a host, it should be able to reply to me on the same socket I connected with, right?

Did I misunderstand some vital piece? Are sockets even the way to go? I would really appreciate any help or advice, thanks so much!