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

I've modified Msg.pm to use UDP and it's almost working. The last snag seems to be that when I use sysread($sock, $buf, 4) to get the length of the message sysread returns the appropriate 4 bytes but the remainder of the message appears to have been read and discarded. Where/Why did my message body vanish?
17Mar06
Thats it, I was stuck thinking TCP while using UDP and with UDP a read of any size slurps the entire message body regardless of the length you give to recv/sysread I switch to grabing the whole messages with recv and I'm taking it apart with no trouble. TNX.

Replies are listed 'Best First'.
Re: Reading UDP with sysread
by Fletch (Bishop) on Mar 15, 2006 at 15:04 UTC

    It's been a while and I don't have my copy of Stevens handy, but I'm fairly sure that reads on a UDP socket preserve packet boundaries. You've got to read an entire packet at once, otherwise there'd be no way to determine where the contents of one message ended and the next began (remember UDP is an unordered datagram protocol, not a stream). Read the entire packet into a buffer then pass that buffer around and extract what you're interested from it as you need it.

Re: Reading UDP with sysread
by ikegami (Patriarch) on Mar 15, 2006 at 16:45 UTC
    UDP is not a streaming protocol. Use recv instead of sysread.