This kind of translation is usually very straight forward, I commented out some original code, and added mine immediately after.

I didn't test the code, and guess you may have a peer to test against. There might be mistakes, but you can certainly come back and discuss.

sub q3msg { my ($host, $port, $timeout, $msg) = @_; my $iaddr = gethostbyname(hostname()); my $sin = sockaddr_in(0, $iaddr); =document socket(SOCK, PF_INET, SOCK_DGRAM, getprotobyname('udp')) or die "s +ocket: $!\n"; bind(SOCK, $sin) or die "bind: $!\n"; =cut my $sock = new IO::Socket::INET(Proto => "udp", LocalAddr => $iadd +r, LocalPort => 0) or die "failed to create UDP socket"; my $hisaddr = inet_aton($host) or die "unknown host \"$host\"\n"; my $srvaddr = sockaddr_in($port, $hisaddr); #defined(send(SOCK, chr(255) x 4 . $msg, 0, $srvaddr)) or die "sen +d: $!\n"; defined(send($sock, chr(255) x 4 . $msg, 0, $srvaddr)) or die "sen +d: $!\n"; my ($rin, $rout); $rin = ""; vec($rin, fileno(SOCK), 1) = 1; if (select($rout=$rin, undef, undef, $timeout)) { #recv(SOCK, $_, 65507, 0) or die "recv: $!\n"; recv($sock, $_, 65507, 0) or die "recv: $!\n"; s/\033.//g; my @response = split /^/m; shift @response; return \@response; } else { return undef; } }

Update:

Actually you can take a look at those two nodes of mine: An internet garbage filter and My experience with Perl threading and the reason I think that I have rushed a little bit. The two pieces of code do the same thing, but the first one is threaded and uses raw socket, and the second one is not threaded and uses IO::Socket. By comparing those two pieces, you can get a good idea of the translation you wanted.

By looking at the title of your post, I saw you compared IO::Socket with "Socket", instead of "socket". I guess (100% pure guess, my guess could be totally wrong) that, you might thought socket is defined in module Socket. That is not true. Raw socket is not defined as object. Module Socket does not give any socket implementation, indeed it just defines a bunch of helper methods, such as inet_atoi etc., also a set of socket related constants.


In reply to Re: Translating Socket to IO::Socket; by pg
in thread Translating Socket to IO::Socket; by BUU

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.