How to write a echo server bash script using tools like nc, echo, xargs, etc capable of simultaneously processing requests from multiple clients each with durable connection?
The best that I've came up so far is
nc -l -p 2000 -c 'xargs -n1 echo'
but it only allows a single connection.
TCP Echo Server In the TCP Echo server , we create a socket and bind to a advertized port number. After binding , the process listens for incoming connections. Then an infinite loop is started to process the client requests for connections.
TCP: The target port number of the TCP echo server. This port in decimal is 9001. UDP: The target port number of the UDP echo server.
One is bash builtin and the second one is an external command. NOTE: Always builtin version takes precedence over external command. Use the type command to get the path information about the echo command. To get the list of options supported for the echo command, use the help option.
In the main method, an initial server message will be displayed: public class SimpleEchoServer { public static void main (String [] args) { System.out.println ("Simple Echo Server"); ... } } The remainder of the method's body consists of a series of try blocks to handle exceptions.
A Bash web server can be created using the nc or netcat, the networking utility: This Bash statement echo’s to port 8080, the output is an HTTP header with the file content length defined. The cat command is used to show the HTML file.
By default when you run the echo command new line character is automatically appended at the end. If you want to suppress this behavior use -n flag. By using the -E flag, the echo statement will treat all backslash-escaped characters as plain text.
If you use ncat instead of nc your command line works fine with multiple connections but (as you pointed out) without -p.
ncat -l 2000 -k -c 'xargs -n1 echo'
ncat is available at http://nmap.org/ncat/.
P.S. with the original the Hobbit's netcat (nc) the -c flag is not supported.
Update: -k (--keep-open) is now required to handle multiple connections.
Here are some examples. ncat simple services
TCP echo server
ncat -l 2000 --keep-open --exec "/bin/cat"
UDP echo server
ncat -l 2000 --keep-open --udp --exec "/bin/cat"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With