Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

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

by Thelonius (Priest)
on Mar 01, 2007 at 22:15 UTC ( #602794=note: print w/replies, xml ) Need Help??


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

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: note [id://602794]
help
Chatterbox?
[Your Mother]: What a stupid, yet seductive super power. Control of biting insects.
[oiskuu]: Don't you think it's disturbing that the washing machine manual would say not to start it with pets inside?
[oiskuu]: So, how long did it take you to grow up?
[oiskuu]: Have you ever had your temperature taken from the other end?

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (12)
As of 2017-12-18 14:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What programming language do you hate the most?




















    Results (487 votes). Check out past polls.

    Notices?