-
Notifications
You must be signed in to change notification settings - Fork 7
/
server.sh
executable file
·69 lines (59 loc) · 1.38 KB
/
server.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#/bin/sh
host_name=host
client_name=client
if [ $# -ge 1 ]; then
port=$1
else
port=9999
fi
input=/tmp/chat-receive-$port
output=/tmp/chat-sending-$port
rm -f $input
rm -f $output
mkfifo $input
mkfifo $output
clear_line() {
printf '\r\033[2K'
}
move_cursor_up() {
printf '\033[1A'
}
server() {
echo "Starting on port $port"
tail -f $output | nc -l -p $port > $input
echo server ending
}
receive() {
printf '%s: ' "$client_name" > $output
local message
while IFS= read -r message; do
clear_line
printf '\033[0;36m%s: \033[0;39m%s\n%s: ' "$client_name" "$message" "$host_name"
move_cursor_up > $output
clear_line > $output
printf '\033[0;37m%s: \033[0;39m%s\n%s: ' "$client_name" "$message" "$client_name" > $output
done < $input
echo receive ending
}
chat() {
printf '%s: ' "$host_name"
local message
while [ 1 ]; do
IFS= read -r message
clear_line > $output
printf '\033[0;36m%s: \033[0;39m%s\n%s: ' "$host_name" "$message" "$client_name" > $output
move_cursor_up
clear_line
printf '\033[0;37m%s: \033[0;39m%s\n%s: ' "$host_name" "$message" "$host_name"
done;
echo chat ending
}
read -r -p 'Enter username: ' host_name
server &
echo 'Waiting for client to join...'
printf 'Enter username: ' > $output
read -r client_name < $input
echo "$client_name has joined the chat"
echo "Joined $host_name's chat" > $output
receive &
chat