I have been trying to make the following code work without success. It is a simple thread waiting on a UDP port. I have lots of possible devices that may send packets to the port which are then simply placed in a file (not included here). I have verified that the UDP socket is created (netstat -a) and that the server is the sending the packets to the PC (Wireshark). I ran a Windows program that listened on the same UDP and it worked okay.

I am running Strawberry perl, v 5.12. I am having two problems.

The first is that the can_read never fires except for timeouts. If I take it out, the recv blocks and never completes. As noted above, the sending is working and the port is open. I print out the handles in the function and the one I added is present. I also found that if I give a timeout = 0, can_read returns immediately and the whole routine loops rapidly. The documenation says a value of 0 results in a blocking call.

The second is that when I set $thread_stop (to kill the thread), Perl crashes when I execute the return. The routine I use to kill the thread, sets thread_stop and then does a join on the thread. I am using Visual Studio as my editor and it gets invoked with a runtime exception.

Can someone please look at the following code and tell me what I have done wrong? It looks fine to me and I have been fighting it for days. Thanks

sub listen_thread { $udp_sock = IO::Socket::INET->new(PeerAddr => inet_ntoa(INADDR_BR +OADCAST), Proto => udp, LocalPort => 48888) or die "socket: $@"; my $s = new IO::Select($udp_sock); print "listen thread running\n"; #make sure the handle is registered @ready = $s->handles(); print "handles = \n"; foreach $r (@ready) { print "han: $r\n"; } #enter loop to receive packets while (1) { @ready = $s->can_read(5); print "waiting handles\n"; foreach $r (@ready) { print "rd $r\n"; } #see if we should terminate the thread if ($thread_stop == 1) { print "listen thread terminated\n"; $udp_sock->close; return 1; } # see if something is waiting print "th - $ready[0] - $udp_sock\n"; if ($ready[0] == $udp_sock) { print "th-r-wait\n"; $t = $udp_sock->recv($newmsg, 1400,0); print "th-r-done $t\n"; } else { print "listen thread timeout\n"; } } }

In reply to Problems with INET, Select, and Win32 by bob0206

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.