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!
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, details, 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, summary, 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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.