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

1. How do I determine the port number if the kernal picked localport when creating a UDP socket with ActivePerl on Windows? Some things I've tried:
$sock_r = IO::Socket::INET->new(Proto => "udp", Reuse => '1', TimeOut => 10) or die "can't connect socket $!"; #$host_port = unpack_sockaddr_in($sock_r); #$host_port = sockaddr_in($sock_r->sockport); #$host_port = getsockname($sock_r->sockaddr); #($host_port, $host_ip) = unpack_sockaddr_in($sock_r->sockaddr +, AF_INET);

2. How do I send binary data thru a UDP socket? Seems the default is ASCII, and I'd like to use UDP socket to exchange SIP protocol messages (which are binary).

Replies are listed 'Best First'.
Re: UDP socket questions
by jasonk (Parson) on Mar 13, 2003 at 17:09 UTC

    You're overthinking the first part, the local port is stored in $sock_r->sockport, it doesn't require any manipulation or unpacking to get it.

    For the second part, the socket doesn't care what the data is, the default is not ascii, it's just bytes, if you send binary bytes, they will work just fine.


    We're not surrounded, we're in a target-rich environment!
      1. I get the following printed "Listening on host_port" on my command line after the following executes. Did I do something wrong with the $sock_r->sockport?
      $sock_r = IO::Socket::INET->new(Proto => "udp") or die "can't connect socket to port: $!"; $host_port = $sock_r->sockport; print "Listening on host_port $host_port\n";
      2. Here is the entire datagram, where I sent from 186.1.238.176:32770 the message: "Ftest 01234 4" to 186.1.228.4:5152. The command line and contents of the UDP payload are below, the datagram appears to be ASCII encoded data.
      print $sock_w "test 01234 $test"; 00 08 74 4A 64 A0 00 02 FD B1 41 C2 08 00 45 00 ..tJd.....A...E. 00 28 A7 6B 40 00 3D 11 4F A1 BA 01 EE B0 BA 01 .(.k@.=.O....... E4 04 80 02 14 20 00 14 98 46 74 65 73 74 20 30 ..... ...Ftest 0 31 32 33 34 20 34 1234 4
      Should I be writing to the socket differently to send binary data? If so, what is the syntax?
        "Ftest 01234 4" is an ascii string as your testing shows. If you want to try some non-ascii data in the quoted string try sending "\01\02\03" that will send non-ascii data (OK, these are in the ascii set, but are "control characters"). You can use pack to put "binary" data into a scalar.

        HTH, --john

        For the first question, you have to specify LocalAddr, that should resolve your problem. For example:
        use IO::Socket::INET; $sock_r = IO::Socket::INET->new(Proto => "udp", LocalAddr => "127.1") or die "can't connect socket to port: $!"; $host_port = $sock_r->sockport; print "Listening on host_port $host_port\n";


        For you second part, just as jasonk said UDP does not understand what ASCII is, or what binary is, it is just a stream of bytes. Encoding is controlled by your script. Absolutely nothing to with UDP, and UDP absolutely does not care that.

        As for syntax, you write binary data to socket in the same way you swrite ascii data to it. The socket does not know, and does not care what you are sending.