0

I'm trying to make some client-server interface. Think of it as one-on-one chat.

What I want to achieve are two horizontal views. Bottom for my (server) input, top for client output and my input, both formatted (e.g. added timestamp). I'm making connection using netcat. I managed to split screen, format and print incoming data. What I lack is printing my input in top window along with sending it to the client. I'm using a named pipe. I start everything with temporary bare command screen -c screens. In future, everything will start from third script with parameters. I'm running 64-bit debian-based distro CrunchBang.

Maybe expect could solve the problem? Any help would be appreciated.

server.sh:

#!/bin/bash

pipe=/tmp/pipe
trap "rm -f $pipe" EXIT

if [[ ! -p $pipe ]]; then
    mkfifo $pipe
    #chmod +x $pipe
fi

nc localhost -lp 53656 >$pipe &

while true
do
    read message

    echo "$message" >$pipe
    clear
done

conv.sh:

#!/bin/bash
# conversation window

clear
pipe=/tmp/pipe

while true
do
    if read line <$pipe; then
        if [[ "$line" == "quit" ]]; then
            break
        fi

        NOW=$(date "+%H:%M")
        echo "($NOW) Client: $line"
    fi
done

screens config:

startup_message off

split
focus
screen ./server.sh
title "input"

focus top
screen ./conv.sh
title "conv"

focus bottom

window view

Mars
  • 867
  • 2
  • 13
  • 22
  • `nc -l` without `-k` will terminate at the first connection. If you want to keep it running you have to add `-k`. You are writing to and reading from pipe, what is netcat doing here? – alvits May 25 '14 at 16:02
  • I connect to it from other terminal with `nc localhost 53656`. From there I sent "hello. i am the client (sent from other terminal)" in the example shown on screenshot. Keeping a connection is not a problem at this moment. Once connected it stays open till one side disconnects. With current setup, I cannnot send from server to client. If I don't make netcat be a background process, I am able to send to client, but not output to top screen. And the bottom window is not being cleared after input, since it's raw netcat window. So I want both send to client and output to top screen. – Mars May 25 '14 at 16:22
  • Now I understand your post. You have output redirected to the pipe from netcat, I believe you want input redirected from pipe to netcat. That's what you should add into your background netcat. The only problem with using the same pipe is any consumer will read the data and another consumer will be left out. So I'd recommend creating 2 one-way pipes. – alvits May 25 '14 at 16:25
  • I've tried it before, but with no result. Do you know how it should be done? And how to wait on both pipes to print in the top window? Because `read` on one pipe blocks the other. – Mars May 25 '14 at 20:54
  • Let's say your pipes are named `send` and `receive`. Your background netcat will now be `nc -lp 53656 < $receive > $send &`. In your while loop you will read from $send and write to $receive, the reverse of netcat's. – alvits May 25 '14 at 21:28

0 Answers0