Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Sending a low-level query using sockets? (A confused newbie)

by Anonymous Monk
on Mar 01, 2007 at 15:40 UTC ( #602704=perlquestion: print w/replies, xml ) Need Help??

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!

Replies are listed 'Best First'.
Re: Sending a low-level query using sockets? (A confused newbie)
by Fletch (Chancellor) on Mar 01, 2007 at 15:48 UTC

    Your code presumes that the other side will send a newline terminated response and then close the socket. Perhaps this is not the case (i.e. it's sending a response and waiting for another query; or it's sending a response but it doesn't contain a newline character (or your platform's newline character))?

Re: Sending a low-level query using sockets? (A confused newbie)
by EvanK (Chaplain) on Mar 01, 2007 at 15:55 UTC
    i cant really help with the socket issue (or whether sockets are suitable for your case), but you might next time want to include a link to the protocol specification. it might help those who are trying to assist you.

    Update: I think Fletch hit it on the head, it doesnt seem to end its messages with newlines, but rather with null characters (\0).

    Update 2: Also, my (limited) experience with game servers makes me curious...are you sure you have the right port? The game port (where you connect to play) is often different from the query port (where you connect to get server info).

    __________
    Systems development is like banging your head against a wall...
    It's usually very painful, but if you're persistent, you'll get through it.

      Whee!

      Can that be solved by setting the $/ to the correct character?

      my($s,$b,@c,@b)=qw(0 0 5C 5F 2F 20);while(int($s+.45)<2){$b[0].=chr(hex($c[int($s+.45)]));$b[1].=chr(hex($c[int($s+2.45)]));$s+=.55}while(1){print $b[$b];$b--;$b*=$b}#Fishnets
Re: Sending a low-level query using sockets? (A confused newbie)
by Thelonius (Priest) on Mar 01, 2007 at 22:15 UTC
    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#.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://602704]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2020-07-05 14:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?