Search code examples
bashsocketstcp

Want to create a TCP server


Hey I am trying to create a TCP server which communicate with a client(eg nc). The functionality I've been trying to achieve is very simple:

  1. echo: command: "ECHO " response: ""

  2. data storing: command: "SET " response: "!200" if no more values can be inputted give out response "!err"

3.data retrieval: command: "GET " output: "" if not found give out response "!error"

After using nc -l localhost 3000 i am not able to use any other command

EDIT: After seeing some downvotes i figured i was not clear with my problem,which is i am running this code


nc -l localhost 3000

while read -r cmd; do
  case $cmd in
    d) date ;;
    q) break ;;
    *) echo 'What?'
  esac
done

in the cmdline i am not able to write any thing. so i am not able to confirm if the d,q,* are working or not


Solution

  • If your nc supports it, you can use the '-e' option: put your loop in a new file handle_client.sh, then start the server with

    nc -l localhost 3000 -e ./handle_client.sh