You could probably do it with a named pipe.
mkfifo ncpipe
nc hostname port < ncpipe
# --- In a galaxy far, far away (another terminal) ---
somecommand > ncpipe
However there are some issues with this... like, if you take too long, the connection might time out or whatever.
It's also possible to do this bi-directionally... I've used this some time ago to forward a local telnet port, not sure if it still works:
mkfifo send receive
> send &
> receive &
nc 127.0.0.1 23 < receive > send &
telnet_pid=$!
nc host port < send > receive &
server_pid=$!
wait $server_pid
kill $telnet_pid
rm send receive
Anyway, you can do weird things with named pipes. However you also have to be careful with them... if there is an old process that has the pipe open, unexpected output can appear. Best to create a shiny new clean pipe for each task and delete when done.
Port forwards in particular are done more easily (and securely) with SSH, but it's not always available. If available in your scenario, it might be preferable to just do:
somecommand | ssh user@host somereceiver
# or the other way around
ssh user@host somecommand | somereceiver
while read l; do case "$l" in "!"*) ${l#\!};;*) echo "$l"; esac; done | netcat host port
. Any line of the form!program
will pipe the output of 'program' to the connection instead of writing the line. Change the!
to something else if it conflicts with commands you're sending to your shell.