[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Help-bash] bi-directional socket client / server example code?
From: |
Greg Wooledge |
Subject: |
Re: [Help-bash] bi-directional socket client / server example code? |
Date: |
Thu, 20 Jun 2013 08:28:02 -0400 |
User-agent: |
Mutt/1.4.2.3i |
On Thu, Jun 20, 2013 at 12:13:41PM +0000, adrelanos wrote:
> Do you know some example code using tcpserver or something similar
> (multiple clients)?
imadev:~$ tcpserver 0 2345 /bin/bash -c $'while read -r; do echo
"${REPLY%$\'\\r\'} to you too"; done'
address@hidden:~$ nc -q1 imadev 2345 <<< 'hello'
hello to you too
arc3:~$ telnet imadev 2345
Trying 10.76.173.78...
Connected to imadev.eeg.ccf.org.
Escape character is '^]'.
good-bye
good-bye to you too
wot
wot to you too
^]q
telnet> q
Connection closed.
In practice, for any non-trivial example, I would write a script and
use tcpserver 0 $port /my/script. The 0 means 0.0.0.0 or "bind to all
interfaces". If you only want the service to listen on localhost, which
is quite reasonable in many cases, then use localhost instead of 0.
It's important to strip carriage returns from each line of input. They
will be present some of the time, but not all of the time, depending
on the options selected by the client. E.g. nc strips them, but telnet
sends them.