Read and write to the same netcat tcp connection

the linux netcat command reads and writes bits over a network connection
netcat multiple connections
netcat loop
test tcp connection
netcat connection timed out
netcat relay
netcat send command
netcat tricks

Say I write to a netcat connection:

tail -f ${file} | nc localhost 7050 | do_whatever | nc localhost 7050

what happens here is that we have two socket connections, to do some request/response. But that's not ideal for a few reasons.

What I am looking to do is reuse the same connection, to read from and write to.

Does anyone know how I can reuse just one netcat connection?

The correct way to do this in UNIX is to make use of a back pipe. You can do so as follows:

First, create a pipe: mknod bkpipe p

This creates a file named bkpipe of type pipe.

Next, figure out what you need to do. Here are two useful scenarios. In these, replace the hosts/addresses and port numbers with the appropriate ports for your relay.

To forward data sent to a local port to a remote port on another machine:

 nc -l -p 9999 0<bkpipe | nc remotehost 7000 | tee bkpipe

To connect to another machine and then relay data in that connection to another:

 nc leftHost 6000 0<bkpipe | nc rightHost 6000 | tee bkpipe

If you simply need to handle basic IPC within a single host, however, you can do away with netcat completely and just use the FIFO pipe that mknod creates. If you stuff things into the FIFO with one process, they will hang out there until something else reads them out.

Read and write to same netcat tcp connection, Using Netcat to Read, Write Data Across Your Network Testing a network connection or troubleshooting a data flow is a common job for the system a quick way to read and write data between two devices over a TCP/IP network. version 4.1.2) with the notebook's IP address and the same port number. so I am reading from the netcat connection. How can I write to that same connection? It should be two-way. Basically if a condition is met in the ql_conditional_release function, I want to respond/write back to the same socket connection. No idea how to do that though.

Using ncat is much easier and understandable for beginners as a one-liner ;)

prompt$> ncat -lk 5087 -c ' while true; do read i && echo [You entered:] $i; done'

Connect with telnet (or nc) to localhost port 5087, and everything you type echoes back to you ;) Use -lk option for listening and keeping/maintaining (multiple) connections.

You can make one bash script out of it like this, using back slashes but it invokes multiple bash, not cheap on resource usage:

#!/bin/bash
# title          : ncat-listener-bidirectional.sh
# description    : This script will listen for text entered by a client 
#                  like for instance telnet
#                  and echo back the key strokes
#
ncat -lk 5087 -c ' \
#!/bin/bash \
while true; do  \
  read i && echo [You entered:] $i; \
done'

bash - Read and write to same netcat tcp connection, Netcat is a simple but handy UNIX utility that reads and writes data across network Usually both commands point to the same binary file. In the given example, nc tries to connect to TCP port number 22 of the localhost  Job Skills Systems Administration Testing a network connection or troubleshooting a data flow is a common job for the system admin or developer. One tool they can take advantage of: the Netcat command line program, which provides a quick way to read and write data between two devices over a TCP/IP network.

Yeah, I think the simplest thing to do is use this method:

tail -f ${file} | nc localhost 7050 | do_whatever > ${file}

just write back into the same file (it's a 'named pipe').

As long as your messages are less than about 500 bytes, they won't interleave.

Using Netcat to Read, Write Data Across Your Network, Netcat is a featured networking utility that reads and writes data across network connections, using the TCP/IP protocol. At the same time, it's a feature-rich network debugging and exploration tool that can specify network  Netcat (or nc) is a command-line utility that reads and writes data across network connections, using the TCP or UDP protocols. It is one of the most powerful tools in the network and system administrators arsenal, and it as considered as a Swiss army knife of networking tools. Netcat is cross-platform, and it is available for Linux, macOS, Windows, and BSD.

Learning to Use netcat to its Full Potential, And, how to use it to send TCP/UDP packets to a remote server. Netcat is a featured networking utility which reads and writes data across network connections, using the TCP/IP protocol. At the same time, it is a feature-rich network debugging and exploration tool that can specify the network patameters while also  1.Create an initial socket to establish a connection from the server to the client. 2.Once connected, Netcat will automatically generate a second socket to transmit files from the server to the client and vice versa. (This is the really cool part).

How to Simulate a TCP/UDP Client Using Netcat, Netcat (or nc) is a command-line utility which reads and writes data across network connections, using the TCP or The procedure for creating an online chat between two or more hosts is the same as when transferring files. Netcat is a featured networking utility which reads and writes data across network connections, using the TCP/IP protocol. Designed to be a reliable "back-end" tool, Netcat can be used directly with other programs and scripts to send files from a client to a server and back.

Sending TCP/UDP packets using Netcat, By default, netcat operates by initiating a TCP connection to a remote host. output. nc: connect to domain.com port 1 (tcp) failed: Connection refused nc: connect On one machine, you can tell netcat to listen to a specific port for connections. Netcat is a great network utility for reading and writing to network connections using the TCP and UPD protocol. Netcat is often referred to as the Swiss army knife in networking tools and we will be using it a lot throughout the different tutorials on Hacking Tutorials.

Comments
  • if do_whatever receives a certain message, it will write back to a tcp server, using the second connection. The main problem is that when other processes want to communicate with this one, they will potentially see two connections, instead of one, to communicate with.
  • the only thing I know of is this technique: xmodulo.com/tcp-udp-socket-bash-shell.html
  • You might be able to do it using a coprocess. But this is not the kindof thing that shell scripts are good for. Why not use a language with a sockets API?
  • I have to google what coprocess is...I added an answer which I think should work fine, not sure why I didn't think of it earlier
  • gnu.org/software/bash/manual/html_node/Coprocesses.html
  • Not sure if it matters, but in my case, only one machine is being used, just localhost. I am using TCP for IPC here.
  • Well, you can do that... Or you could just use a FIFO pipe for the entire thing. I had the impression that you were using a nectay relay between hosts.
  • I've added a revision to that effect.
  • What does that have to do with the question? I thought this was about writing to the network connection, not the file.
  • the OP is about reading and writing to the same tcp connection
  • using the method in this answer, that has been realized
  • I see what you're doing. Since you can't read back from the network, you're writing to the file that tail -f is reading file.
  • yeah exactly - I got the answer from here: unix.stackexchange.com/questions/439081/…