如何使用bash创建套接字服务器并允许同一端口中的多个客户端?

Mar*_*one 3 linux networking tcp netcat

我有4个程序,以后会增加,这些程序必须连接到同一个ip:port才能同时发送和接收消息。

到目前为止,我已经打开了套接字,我还想保持程序和服务器之间的连接。

#!/bin/sh
nc -lvk 88.109.110.161 100 > port100.txt 2>&1
Run Code Online (Sandbox Code Playgroud)

Jde*_*eBP 8

nc不能并行处理多个连接的客户端,并且是此工作的错误工具。这项工作有很多合适的工具,包括:

  1. Bernstein tcpserver(原版或 djbwares)或 Hoffman tcpserver
    tcpserver -v -R -H -l 0 88.109.110.161 100 sh -c 'exec cat 1>&2' 2>&1 |
    cyclog port100/
    Run Code Online (Sandbox Code Playgroud)
  2. 我的tcpserver垫片:
    tcpserver -v 88.109.110.161 100 sh -c 'exec cat 1>&2' 2>&1 |
    cyclog port100/
    Run Code Online (Sandbox Code Playgroud)
  3. 我的 UCSPI-TCP 工具:
    tcp-socket-listen 88.109.110.161 100 tcp-socket-accept --verbose sh -c 'exec cat 1>&2' 2>&1 |
    cyclog port100/
    Run Code Online (Sandbox Code Playgroud)
  4. 贝尔科特s6-tcpserver4
    s6-tcpserver4 -v 2 88.109.110.161 100 sh -c 'exec cat 1>&2' 2>&1 |
    cyclog port100/
    Run Code Online (Sandbox Code Playgroud)
  5. Bercot s6-网络工具:
    s6-tcpserver4-socketbinder 88.109.110.161 100 s6-tcpserver4d -v 2 sh -c 'exec cat 1>&2' 2>&1 |
    cyclog port100/
    Run Code Online (Sandbox Code Playgroud)
  6. tcpsvd
    tcpsvd -v 88.109.110.161 100 sh -c 'exec cat 1>&2' 2>&1 |
    cyclog port100/
    Run Code Online (Sandbox Code Playgroud)
  7. 桑普森onenetd
    onenetd -v 88.109.110.161 100 sh -c 'exec cat 1>&2' 2>&1 |
    cyclog port100/
    Run Code Online (Sandbox Code Playgroud)

并且可以替换multilog, s6-log, svlogd, 或tinylogfor cyclog

进一步阅读