in reply to Re: UDP socket questions
in thread UDP socket questions

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?

Replies are listed 'Best First'.
Re: Re: Re: UDP socket questions
by traveler (Parson) on Mar 13, 2003 at 18:49 UTC
    "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

Re: Re: Re: UDP socket questions
by pg (Canon) on Mar 13, 2003 at 20:24 UTC
    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.
      Thank you All so much!!

      Adding "LocalAddr" was all I needed and now $sock_r->sockport returns the listening port.

      Sending $hex hi thru my write socket is exactly what I need!

      # results in "4567" in datagram trace - WINNER!! my $hex_hi = pack("H*", "4567");