Skip to main content
added 221 characters in body
Source Link
Stéphane Chazelas
  • 586.8k
  • 96
  • 1.1k
  • 1.7k

bash's virtual /dev/tcp/host/port files (a ksh feature initially) can only be used to connect a socket to a TCP port.

If you want to do more, like creating a listening socket and spawning accepting sockets from that, that can't be done with redirection.

zsh has support for that via its zsh/net/tcp module and ztcp builtin.

zmodload zsh/net/tcp
handle-connection() (
  IFS= read -ru4 line || exit
  print -r "Got: $line"
  print -ru4 "Hello $line"
  ztcp -c 4
)
  
ztcp -l -d 3 1234 # create a TCP listening socket on port 1234 on fd 3
while ztcp -ad4 3 # accept connection on fd 4
do handle-connection & exec 4>&-
done

It's still limited in that you can't specify which address to bind on or the size of the listen queue, or shutdown only one direction, or set socket options... It also doesn't do UDP or SCTP (you can do Unix domain sockets though with the zsocket command).

If you want anything fancier or if you don't have zsh, you could use socat. For instance, here with bash exported functions:

handle_connection() {
  IFS= read -r line &&
    printf 'Hello %s\n' "$line"
}
export -f handle_connection
socat tcp-listen:1234,reuseaddr,fork 'exec:bash -c handle_connection'

With socat, the possibilities are endless, see the man page for details.

For instance, for the server to spawn a shell session:

socat tcp-listen:1234,reuseaddr,fork exec:bash,pty,ctty,setsid,stderr,sane

On the client side, you'd connect as:

socat -,raw,echo=0 tcp:host:1234

bash's virtual /dev/tcp/host/port files (a ksh feature initially) can only be used to connect a socket to a TCP port.

If you want to do more, like creating a listening socket and spawning accepting sockets from that, that can't be done with redirection.

zsh has support for that via its zsh/net/tcp module and ztcp builtin.

zmodload zsh/net/tcp
handle-connection() (
  IFS= read -ru4 line || exit
  print -r "Got: $line"
  print -ru4 "Hello $line"
  ztcp -c 4
)
  
ztcp -l -d 3 1234 # create a TCP listening socket on port 1234 on fd 3
while ztcp -ad4 3 # accept connection on fd 4
do handle-connection & exec 4>&-
done

It's still limited in that you can't specify which address to bind on or the size of the listen queue, or shutdown only one direction, or set socket options... It also doesn't do UDP or SCTP (you can do Unix domain sockets though with the zsocket command).

If you want anything fancier or if you don't have zsh, you could use socat. For instance, here with bash exported functions:

handle_connection() {
  IFS= read -r line &&
    printf 'Hello %s\n' "$line"
}
export -f handle_connection
socat tcp-listen:1234,reuseaddr,fork 'exec:bash -c handle_connection'

With socat, the possibilities are endless, see the man page for details.

bash's virtual /dev/tcp/host/port files (a ksh feature initially) can only be used to connect a socket to a TCP port.

If you want to do more, like creating a listening socket and spawning accepting sockets from that, that can't be done with redirection.

zsh has support for that via its zsh/net/tcp module and ztcp builtin.

zmodload zsh/net/tcp
handle-connection() (
  IFS= read -ru4 line || exit
  print -r "Got: $line"
  print -ru4 "Hello $line"
  ztcp -c 4
)
  
ztcp -l -d 3 1234 # create a TCP listening socket on port 1234 on fd 3
while ztcp -ad4 3 # accept connection on fd 4
do handle-connection & exec 4>&-
done

It's still limited in that you can't specify which address to bind on or the size of the listen queue, or shutdown only one direction, or set socket options... It also doesn't do UDP or SCTP (you can do Unix domain sockets though with the zsocket command).

If you want anything fancier or if you don't have zsh, you could use socat. For instance, here with bash exported functions:

handle_connection() {
  IFS= read -r line &&
    printf 'Hello %s\n' "$line"
}
export -f handle_connection
socat tcp-listen:1234,reuseaddr,fork 'exec:bash -c handle_connection'

With socat, the possibilities are endless, see the man page for details.

For instance, for the server to spawn a shell session:

socat tcp-listen:1234,reuseaddr,fork exec:bash,pty,ctty,setsid,stderr,sane

On the client side, you'd connect as:

socat -,raw,echo=0 tcp:host:1234
Source Link
Stéphane Chazelas
  • 586.8k
  • 96
  • 1.1k
  • 1.7k

bash's virtual /dev/tcp/host/port files (a ksh feature initially) can only be used to connect a socket to a TCP port.

If you want to do more, like creating a listening socket and spawning accepting sockets from that, that can't be done with redirection.

zsh has support for that via its zsh/net/tcp module and ztcp builtin.

zmodload zsh/net/tcp
handle-connection() (
  IFS= read -ru4 line || exit
  print -r "Got: $line"
  print -ru4 "Hello $line"
  ztcp -c 4
)
  
ztcp -l -d 3 1234 # create a TCP listening socket on port 1234 on fd 3
while ztcp -ad4 3 # accept connection on fd 4
do handle-connection & exec 4>&-
done

It's still limited in that you can't specify which address to bind on or the size of the listen queue, or shutdown only one direction, or set socket options... It also doesn't do UDP or SCTP (you can do Unix domain sockets though with the zsocket command).

If you want anything fancier or if you don't have zsh, you could use socat. For instance, here with bash exported functions:

handle_connection() {
  IFS= read -r line &&
    printf 'Hello %s\n' "$line"
}
export -f handle_connection
socat tcp-listen:1234,reuseaddr,fork 'exec:bash -c handle_connection'

With socat, the possibilities are endless, see the man page for details.