I'm writing a script that uses Net:PcapUtils to capture traffic on an ethernet segment. I have the capture working fine, using the Net:PcapUtils::Loop function. Inside that loop, I want the script to stop periodically and listen for a UDP request from a client. The client sends a request like this: 198.59.115.4:34, and the script sends a response like this: 34 198.59.115.4:34, which is a count of how many times it has seen that IP:Port as a destination.

It works, sort of. It counts, it listens, it answers. The problem is this, if it does not receive a request, it seems to wait until it does, not counting in the meantime. I was hoping that an if statement would make it so that if there was no request, it would just continue, but it does not seem to work that way:

$count ++; if ($count == 500) { $MAXLEN = 1024; $PORTNO = 5151; $sock = IO:SOCKET::INET->new(LocalPort => $PORTNO, Proto => 'udp') or die "socket: $@"; if ($sock->recv($request, $MAXLEN)) { ... close($sock); } $count = 0; }
So I start up this script and send a request to it from the client. I get a response. If I don't send a request for an hour and then send one, the count is the same. If I just sit there and send mutliple requests, the count goes up.

Any idea how I can get this to work the way I want?

jgentry@swcp.com


In reply to Check for request from UDP client by Anonymous Monk

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.