How can I add message delimiters to a UDP stream socat is piping?

One possibility that does not involve forking is to use the socat verbose output rather than the data. My version of socat -v includes the length of the data in the verbose output, so you know where it ends. For example,

mkfifo mypipe
while sleep 3
do    printf "%sNONEWLINE" $RANDOM
done |
socat -u - UDP4:localhost:9999  &
socat -u -v UDP-RECV:9999 - >/dev/null 2>mypipe  &
cat -uv mypipe

will output before each data item (eg 9430NONEWLINE) a header starting > with the date and a length.

> 2018/07/28 10:29:33.965222  length=13 from=0 to=12
9430NONEWLINE> 2018/07/28 10:29:36.968335  length=14 from=13 to=26
26947NONEWLINE> 2018/07/28 10:29:39.971025  length=14 from=27 to=40
15126NONEWLINE

Use sed, which can append new line with a single command, to avoid the race condition when using SYSTEM:"cat; echo \"\n\"" output:

socat -u UDP-RECVFROM:9999,fork SYSTEM:"sed -e a\\\\"

Tags:

Socat