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!
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.