I have a file on my server that has HL7 messages spread out by a space. Example below. I am wanting to send one of these messages at a time in a continual loop until stopping the script with ctrl+c. I am pretty new to netcat but when I try to netcat directly using nc -l 10004 I get:

ncat: bind to :::10004: Address already in use. QUITTING.

There is a Mirth application on the appliance listening on this port already. I have no problem running the command on another appliance altogether sending it to this one if that is the only way but I need assistance sending to the IP using netcat and sending one message at a time with a timed gap between them.




The file I am pulling from has multiple messages with more than one line but they are separated by a line break. (space)

  • You probably can batch these instead of sending one at a time if you agreed on a low level protocol to wrap the messages. Commented Jun 10, 2015 at 20:12
  • I'm wanting to send them through one at a time that way I can see how they are processing within our system. By keeping them flowing through one at a time say every 3 seconds I can make changes and see how they affect processing.
    – Jeff
    Commented Jun 10, 2015 at 21:01

1 Answer 1


If you have the Mirth application already listening on port 10004 then you can't really have netcat also listening on that same port (the -l flag denotes listen). Perhaps you intended to write to the port rather than listen on it:

# Connect once and write many messages
( while :; do echo MSH....; sleep 3; done ) | nc -vvv localhost 10004


# Connect for each message
( while :; do echo MSH.... | nc -vvv localhost 10004; sleep 3; done )

Be aware that there are several different implementations of netcat. Every implementation seems to handle a different subset of flags...

Based on the new information in your question, here's another possible solution:

while IFS= read -r LINE || test -n "$MESG"
    if test -n "$LINE"
        # Build the message block
        test -z "$MESG" && MESG="$LINE" || MESG=$(printf "%s\n%s" "$MESG" "$LINE")

    # Send the message block to the service
    echo "$MESG" | nc -vvv localhost 10004
    sleep 3
done < /path/to/your/messagefile.txt
  • I tried while :; do echo HL7.txt | nc -vvv localhost 10004; sleep 3; done because I have the different HL7 messages in this file but it sends HL7.txt not what's inside. To verify this I sent it to another port and listened on this port. Each HL7 message is different but is separated by a space so I am hoping to send each individually and in a loop.
    – Jeff
    Commented Jun 11, 2015 at 11:30
  • @Jeff you have a file containing several lines, one line per HL7 message? Or several files each containing one message? Or something different? You didn't mention this in your Question so please update it, and then I can update my Answer to support your new information. Commented Jun 11, 2015 at 12:06
  • I updated the question thanks for any assistance!
    – Jeff
    Commented Jun 11, 2015 at 12:42
  • @Jeff answer updated to incorporate the new information Commented Jun 11, 2015 at 16:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .