Yesterday I asked a question about catting a file over a UDP socket in bash.
The solution we came up was
netcat -c -w 1 -v -u -s 127.0.0.1 18.104.22.168 30001 < test.txt. This worked in the sense that it sent the packets, but there's a problem.
The source file isn't strictly a text file. It's actually a binary file -- the content is mostly text, with embedded non-printable characters and no
\n lineendings. Instead, the control character
^C (0x03) is used as a line delimiter.
netcat was sending packets, it would send as much as possible in a single UDP frame. But I want it to send one UDP frame per
^C-delimited mesasge in the source file.
For example, if my file consists of:
netcat would result in one UDP frame being sent. What I want is to send 3 messages:
Is there a way to accomplish this?
I have tried a number of possible solutions, but nothing's worked.
For one I've tried
sedding the source file to replace the
^C\n, but that had no effect:
sed 's/^C/^C\n\0/g' test.txt | netcat -n -vv -c -w 1 -v -u -s 127.0.0.1 22.214.171.124 30002
I also tried catting the files to
/dev/udp/ instead of using
netcat, but the results were similar.
cat test.txt > /dev/udp/126.96.36.199/30002
Finally I tried using
awk to print one line at a time and redirecting that to
/dev/udp, but the results were really the same.
It appears that both
cat > /dev/udp both buffer the input until it has a full frame, then sending the frame. That's not what I want.
Can I flush the udp buffer, or some other way send one UDP message per ^C-delimited message in the source file?