1

I have written an echoclient and echoserver network program so that messages written on the client side can be echoed to the client by the echoserver program. Echoclient is an interactive program that waits for the user to enter a message. It then sends the message, gets the response, and then again waits for a new message from the user. Now I would like to write a script shell so that I can send a new message to the server every 100 microseconds. But I don't know how to pass messages to the echoclient program. Any thought on that?

#connect to the server ...
./echoclient SERVER_IP_ADDR SERVER_PORT

i=1
while [ 1 ]
do
   #send "message i" to the server. how to do it?
   usleep 100  
   let i++ 
done
2
  • How accurate do you need your timing? You probably need to write this in something faster. At least Python if not C. Commented Mar 11, 2011 at 19:59
  • Timing is not much important. I just want to gradually increase the pressure on the server so that I can find out how many active clients can be simultaneously supported in my network (which is pretty much a large network). Commented Mar 13, 2011 at 4:08

2 Answers 2

2

EDIT: First answer was way off - misread the question. The below mechanism should work.

mkfifo /tmp/echostuff
./echoclient SERVER_IP_ADDR SERVER_PORT < /tmp/echostuff &

exec 3>/tmp/echostuff
let i=1
while true
do
   echo "message $1" >&3
   sleep 0.0001
   let i+=1
done
Sign up to request clarification or add additional context in comments.

1 Comment

@aminfar: That syntax opens a new file descriptor for writing to /tmp/echostuff. Then when I echo >&3 I write to the file
1

Instead of a fifo (which Erik showed), a simple pipe should work too, like this (code stolen from Erik's anwser):

let i=1
while true
do
   echo "message $i"
   sleep 0.001
   let i+=1
done |
./echoclient SERVER_IP_ADDR SERVER_PORT

This way the loop runs in a subshell, though, so you can't access the new value of $i after the loop (it's still 1).

Another way would be to run the echoclient as a bash coprocess, and connect its input to the output of the echo command. (If you want to see the result, too, it would get more complicated.)

3 Comments

The code you wrote works well. Thanks! the only thing is that the echo command (line 4) prints the message on the client side which is not what I want. The "while" outputs are piped to the echoclient program, but the "echo" output is printed out on the screen.
Did you try it? In my try, the pipe after done catches everything which is output to stdout inside the loop, including the echos. (Of course, I don't have your echoclient, so I used a grep 5 instead - and it only outputs lines with 5.)
Sorry, I was making a mistake. The code works like a charm. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.